Google Maps API in Vue

This explores the initialization of map, adding and clearing of markers in the map in a Vue page.

index.html

<script src='https://maps.googleapis.com/maps/api/js?key=AIzaSyAOVYRIgupAurZup5y1PRh8Ismb1A3lLao&callback=initMap'
        async defer></script>

In index.html, load the Google Maps JavaScript API

Map.vue

<template>

<template>
  <html>
    <body>
      <div id="map"></div>
      <button id="clearMarkers" @click="clearMarkers">Clear</button>
    </body>
  </html>
</template>

Then, create a <div> container and name the id attribute as "map" in the Vue page.

<style>

Add the required CSS styling.

<script>

Initialize a variable called map and set it as null

Initialize an array that contains the data of places. Then, call the initMap and addMarkers methods.

  1. Method Purpose: The initMap method is designed to initialize a Google Map with default settings.

  2. Create Map Instance: It creates a new instance of a Google Map.

  3. Target Map Element: The map instance is associated with the HTML element having the ID "map." This requires an HTML element with the specified ID to be present in the DOM.

  4. Set Initial Center: The initial center of the map is set to latitude 0 and longitude 0, representing the geographical center of the Earth.

  5. Set Initial Zoom Level: The initial zoom level of the map is set to 2, which is quite zoomed out, showing a large portion of the globe.

  6. Initialize Empty Markers Array: An empty array is initialized to hold markers. This implies that the application may add markers to the map later on.

  1. Method Purpose: The addMarkers method is responsible for adding markers to the map based on the provided data.

  2. Check Data Validity: It checks if the provided data is not empty or null before proceeding with adding markers.

  3. Reinitialize Map: It reinitializes the map using the initMap method.

  4. Markers Array Initialization: An array called markers is initialized to hold marker objects.

  5. Marker Click Listener: For each place in the data array, a marker is created. A click listener is added to each marker.

  6. Info Window Handling: When a marker is clicked, it closes the currently open info window (if any) and creates a new info window with dynamically generated content, including the title, latitude, and longitude.

  7. Map Centering: The map is centered on the location of the first marker and set to a zoom level of 10.

  8. Marker Clustering: A MarkerClusterer instance is created to manage the markers, and all markers are added to the marker cluster.

  9. Save Markers to Map Object: The markers are saved to the map object for future reference.

  10. Reinitialize Map for Empty Data: If the data is empty or null, it reinitializes the map without markers.

  1. Method Purpose: The clearMarkers method is designed to clear markers from the map.

  2. Reinitialize Map: It calls the initMap method, which reinitializes the map with default settings.

  3. Effect of Reinitialization: This effectively removes all existing markers from the map, providing a way to clear the map of any previously added markers.

  4. Use Case: This method is typically used in response to a user action, such as clicking a "Clear" button, to reset the map and remove any markers that may have been added.

Download

file-download
6KB

Last updated