首页 文章

谷歌 Map 地理编码器变长和拉特

提问于
浏览
1

我试图通过for循环获得城市的长期和纬度 .

现在我已经完成了我的编码,但仍然由于地理编码器是一个异步函数,一些东西不起作用(Map没有加载,标记没有显示) . 与地理编码器的输出我试图在MAP上显示标记 .

function codeAddress(address, map, callback) {
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        var latLng = new google.maps.LatLng(results[0].geometry.location.lat().lat,results[0].geometry.location.lng());
                    var markerObj = new MarkerWithLabel({
                    position: latLng,
                    title:name,
                    labelContent: name,
                    labelClass:'marker-labels',
                    icon:markerImg
                });
        markerObj.setMap(map);
        console.log(latLng);
        return callback(map, latLng);
      } else {
        console.log('Geocode was not successful for the following reason: ' + status);
      }
    });
  }
    function initialize() {
        var mapOptions = {
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

    map = new google.maps.Map(document.getElementById('googleMap'),mapOptions);

    if ('' != markersAddress) {
    for (var x=0; x<markersAddress.length; x++) {
        var address = markersAddress[x].address;
        var name = markersAddress[x].name;
        codeAddress(address, map, function(map, latLng) {})
    }
    }
}

google.maps.event.addDomListener(window, 'load', initialize);

JS小提琴http://jsfiddle.net/qe9soL4o/5/

1 回答

  • 3

    您正在使用 MarkerWithLabel 但尚未包含它

    包括这两个文件

    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>

    <script src="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerwithlabel/src/markerwithlabel.js"></script>

    请注意,这些文件的顺序很重要

相关问题