谷歌地图上的标记标题

时间:2018-12-15 12:27:22

标签: javascript geocoding

我正在尝试从地址开始在地图上放置标记。有用。 虽然每个标记都有一个标题,并且应该是实际地址,但所有标记都具有相同的标题 我该如何解决?

        <script src="https://maps.googleapis.com/maps/api/js?key=MYAPIKEY
    "></script>
        <script>
          // locations 
            var addo;
            var indirizzi = [
            ['Via'],
            ['Via Padova 175 - 20127 Milano (MI)'],
            ['via della Chiesa Rossa n. 69 - 20142 Milano (MI)'],
            ['Via Lucilio Gaio n. 5 - 20151 Milano (MI)'],
            ['VIA EMANUELE ODAZIO 6 - 20147 Milano (MI)'],
            ['VIA PRIVATA GIOVANNI BATTISTA PRANDINA 11 - 20128 Milano (MI)'],
            ['Via della Capinera n. 5-6 Milano (MI)'],
            ['Via Privata Uberto DellOrto n. 11 - 20161 Milano (MI)'],
            ['Via Michele Lessona n. 5 - 20157 Milano (MI)'],
            ['via Fulvio Testi n. 58 Milano (MI)'],
            ['via Giulio e Corrado Venini n. 90 Milano (MI)'],
            ];

  function initialize() {

    var geo = new google.maps.Geocoder;
    var bangalore = { lat: 45.46, lng: 9.19 };

    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 12,
      center: bangalore
    });


     for (i = 0; i < indirizzi.length; i++) {
     addo=indirizzi[i][0];

     geo.geocode({'address':indirizzi[i][0]},function(results, status){
       addMarker(results[0].geometry.location, map, addo);
     })
    }
  }
  // Adds a marker to the map.
  function addMarker(location, map, titolo) {

    var marker = new google.maps.Marker({        
      position: location,
      title: titolo,
      map: map
    });
  }
  google.maps.event.addDomListener(window, 'load', initialize);
</script>

请注意,我已经删除了我的个人API密钥。

1 个答案:

答案 0 :(得分:0)

尝试这个可能对您有所帮助。

  var locations = [
          ['Bondi Beach', -33.890542, 151.274856, 4],
          ['Coogee Beach', -33.923036, 151.259052, 5],
          ['Cronulla Beach', -34.028249, 151.157507, 3],
          ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
          ['Maroubra Beach', -33.950198, 151.259302, 1]
        ];

        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 10,
          center: new google.maps.LatLng(-33.92, 151.25),
          mapTypeId: google.maps.MapTypeId.ROADMAP
        });

        var infowindow = new google.maps.InfoWindow();

        var marker, i;

        for (i = 0; i < locations.length; i++) {  
          marker = new google.maps.Marker({
            position: new google.maps.LatLng(locations[i][1], locations[i][2]),
            map: map
          });

          google.maps.event.addListener(marker, 'click', (function(marker, i) {
            return function() {
              infowindow.setContent(locations[i][0]);
              infowindow.open(map, marker);
            }
          })(marker, i));
        }