繁体   English   中英

从数组在Google地图上放置多个标记

[英]placing multiple markers on google map from array

我试图将多个标记放置在由数组提供的地图上。 现在只有我的初始点荷载(NYC)。

var geocoder;
var map;
var markersArray = [];

//plot initial point using geocode instead of coordinates (works just fine)
  function initialize() {
    geocoder = new google.maps.Geocoder();
     latlang = geocoder.geocode( { 'address': 'New York City'}, function(results, status) { //use latlang to enter city instead of coordinates 
            if (status == google.maps.GeocoderStatus.OK) {
                map.setCenter(results[0].geometry.location);
                marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location
                });
            markersArray.push(marker);
            }
            else{
            alert("Geocode was not successful for the following reason: " + status);
            }
        });
    var myOptions = {
        center: latlang, zoom: 5, mapTypeId: google.maps.MapTypeId.SATELLITE,
        navigationControlOptions: {
            style: google.maps.NavigationControlStyle.SMALL
        }
    };
     map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);

  }

///////////////////////////////////////////////////////////
//Everything below this line is for attempting to plot the markers

  var locationsArray = ['Pittsburgh','Chicago', 'Atlanta'];

  function plotMarkers(){
for(var i = 0; i < locationsArray.length; i++){
  codeAddresses(locationsArray[i]);
}
  }

  function codeAddresses(address){
    geocoder.geocode( { 'address': address}, function(results, status) { 
            if (status == google.maps.GeocoderStatus.OK) {
                map.setCenter(results[0].geometry.location);
                marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location
                });
            //markersArray.push(marker); 
            }
            else{
            alert("Geocode was not successful for the following reason: " + status);
            }
  });
  }

您实际上并plotMarkers在上面的代码段中的任何地方调用plotMarkers 当我添加到initialize的末尾(定义了映射之后)时,它的效果很好! http://jsfiddle.net/T5aKE/

       ...
         map = new google.maps.Map...
         plotMarkers();
       ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM