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

时间:2015-07-14 05:15:48

标签: javascript php google-maps google-maps-api-3

我试图通过for循环来获取城市的长期和纬度。

现在我完成了我的编码,但仍然由于地理编码器是一个异步功能,一些东西不起作用(地图没有加载,标记没有显示)。使用地理编码器的输出我试图在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 个答案:

答案 0 :(得分: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>

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

相关问题