Mapping multiple locations with Google Maps JavaScript API v3 and Geocoding API

后端 未结 3 1842
离开以前
离开以前 2020-12-01 10:16

I\'m using Google Maps JavaScript API v3 to generate a map with multiple locations/markers. I only have the address for these locations, not the coordinates, so I\'m using t

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-01 10:29

    This is a duplicate of google map info window multiple addresses via xml, just not an exact duplicate. The geocoder is asynchronous, so when the geocoder callback runs, the value of address is that from the end of the loop for all the calls.

    The answer is the same: The simplest solution is to use function closure to associate the call to the geocoder with the returned result:

    function geocodeAddress(locations, i) {
        var title = locations[i][0];
        var address = locations[i][1];
        var url = locations[i][2];
        geocoder.geocode({
            'address': locations[i][1]
        },
    
        function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var marker = new google.maps.Marker({
                    icon: 'http://maps.google.com/mapfiles/ms/icons/blue.png',
                    map: map,
                    position: results[0].geometry.location,
                    title: title,
                    animation: google.maps.Animation.DROP,
                    address: address,
                    url: url
                })
                infoWindow(marker, map, title, address, url);
                bounds.extend(marker.getPosition());
                map.fitBounds(bounds);
            } else {
                alert("geocode of " + address + " failed:" + status);
            }
        });
    }
    

    Working fiddle

    code snippet:

    var locations = [
      ['Location 1 Name', 'New York, NY', 'Location 1 URL'],
      ['Location 2 Name', 'Newark, NJ', 'Location 2 URL'],
      ['Location 3 Name', 'Philadelphia, PA', 'Location 3 URL']
    ];
    
    var geocoder;
    var map;
    var bounds = new google.maps.LatLngBounds();
    
    function initialize() {
      map = new google.maps.Map(
        document.getElementById("map_canvas"), {
          center: new google.maps.LatLng(37.4419, -122.1419),
          zoom: 13,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        });
      geocoder = new google.maps.Geocoder();
    
      for (i = 0; i < locations.length; i++) {
    
    
        geocodeAddress(locations, i);
      }
    }
    google.maps.event.addDomListener(window, "load", initialize);
    
    function geocodeAddress(locations, i) {
      var title = locations[i][0];
      var address = locations[i][1];
      var url = locations[i][2];
      geocoder.geocode({
          'address': locations[i][1]
        },
    
        function(results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
            var marker = new google.maps.Marker({
              icon: 'http://maps.google.com/mapfiles/ms/icons/blue.png',
              map: map,
              position: results[0].geometry.location,
              title: title,
              animation: google.maps.Animation.DROP,
              address: address,
              url: url
            })
            infoWindow(marker, map, title, address, url);
            bounds.extend(marker.getPosition());
            map.fitBounds(bounds);
          } else {
            alert("geocode of " + address + " failed:" + status);
          }
        });
    }
    
    function infoWindow(marker, map, title, address, url) {
      google.maps.event.addListener(marker, 'click', function() {
        var html = "

    " + title + "

    " + address + "

    View location

    "; iw = new google.maps.InfoWindow({ content: html, maxWidth: 350 }); iw.open(map, marker); }); } function createMarker(results) { var marker = new google.maps.Marker({ icon: 'http://maps.google.com/mapfiles/ms/icons/blue.png', map: map, position: results[0].geometry.location, title: title, animation: google.maps.Animation.DROP, address: address, url: url }) bounds.extend(marker.getPosition()); map.fitBounds(bounds); infoWindow(marker, map, title, address, url); return marker; }
    html,
    body,
    #map_canvas {
      height: 100%;
      width: 100%;
      margin: 0px;
      padding: 0px
    }
    
    

提交回复
热议问题