Javascript 谷歌地图地理编码器和标记器不工作

Javascript 谷歌地图地理编码器和标记器不工作,javascript,google-maps,google-maps-api-3,google-maps-markers,geocode,Javascript,Google Maps,Google Maps Api 3,Google Maps Markers,Geocode,我试图在for循环中获取一个地址,获取该地址的纬度和经度,并使用lat/long值创建一个标记 我将地址转换为lat/long,但无法使用“新标记”来获取值 有什么建议吗 var latitude; var longitude; var myLatLng; for (i = 0; i < locations.length; i++) {

我试图在for循环中获取一个地址,获取该地址的纬度和经度,并使用lat/long值创建一个标记

我将地址转换为lat/long,但无法使用“新标记”来获取值

有什么建议吗

                var latitude;
                var longitude;
                var myLatLng;

                for (i = 0; i < locations.length; i++) {

                    var geocoder = new google.maps.Geocoder();
                    var BuisAddress = locations[i][1];
                    geocoder.geocode({ 'address': BuisAddress }, function (results, status) {
                        if (status == google.maps.GeocoderStatus.OK) {
                            latitude = results[0].geometry.location.lat();
                            longitude = results[0].geometry.location.lng();
                            }
                        });


                    myLatLng = new google.maps.LatLng(latitude, longitude);
                    marker = new google.maps.Marker({
                        position: myLatLng,
                        map: map
                    });
您可以尝试这种方法(我添加了一个标记向量来管理向量集合)

var纬度;
var经度;
var Mylatng;
myMarker=[]
对于(i=0;i
如果有人想知道我是怎么做到的,就在这里

function geocodeAddress(locations, i) {
  var title = locations[i][0];
  var address = locations[i][1];
  var url = locations[i][2];
  var latitude;
  var longitude;
  geocoder.geocode({
      'address': locations[i][1]
    },

    function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
            latitude = results[0].geometry.location.lat();
            longitude = results[0].geometry.location.lng();
        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,
          latitude: latitude,
          longitude: longitude
        })
        infoWindow(marker, map, title, address, url, latitude, longitude);
        bounds.extend(marker.getPosition());
        map.fitBounds(bounds);
      } else {
        alert("geocode of " + address + " failed:" + status);
      }
    });
}

function infoWindow(marker, map, title, address, url, latitude, longitude) {
  google.maps.event.addListener(marker, 'click', function() {
      var html ="html code here...";
    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;
}

地理编码器是异步的。您需要在回调函数中使用返回的结果。相关问题:相关问题:您看过任何相关问题吗?关于第一个问题,我看到了更多代码。我只注意到标记无法在geocode回调函数之外设置。然后,您有一个用于多个位置的循环和一个为每个标记显示信息窗口的侦听器。对吗?我已经更新了答案,添加了一个标记向量来管理标记的集合。我希望这对你有用。谢谢geocodezip,我查看了你发布的第二个相关问题,我找到了一种方法来完成这一切!
    var latitude;
    var longitude;
    var myLatLng;

    myMarker  = []
    for (i = 0; i < locations.length; i++) {

        geocoder.geocode({ 'address': BuisAddress }, function (results, status) {
                                if (status == google.maps.GeocoderStatus.OK) {
                                        latitude = results[0].geometry.location.lat();
                                        longitude = results[0].geometry.location.lng();
                                        myLatLng = new google.maps.LatLng(latitude, longitude);
                                        marker[i] = new google.maps.Marker({
                                            position: myLatLng,
                                            map: map
                                        });
                                    }
                                });

        google.maps.event.addListener(marker[i], 'click', (function(marker[i], i) {
                                return function() {
                                    var str = encodeURI(locations[i][1]).replace(",", "");
                                    var address = str.replace(/%20/g, "+");
                                    var infowindow = new google.maps.InfoWindow();
                                }
        }));

    }
function geocodeAddress(locations, i) {
  var title = locations[i][0];
  var address = locations[i][1];
  var url = locations[i][2];
  var latitude;
  var longitude;
  geocoder.geocode({
      'address': locations[i][1]
    },

    function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
            latitude = results[0].geometry.location.lat();
            longitude = results[0].geometry.location.lng();
        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,
          latitude: latitude,
          longitude: longitude
        })
        infoWindow(marker, map, title, address, url, latitude, longitude);
        bounds.extend(marker.getPosition());
        map.fitBounds(bounds);
      } else {
        alert("geocode of " + address + " failed:" + status);
      }
    });
}

function infoWindow(marker, map, title, address, url, latitude, longitude) {
  google.maps.event.addListener(marker, 'click', function() {
      var html ="html code here...";
    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;
}