【问题标题】:Google Map geocoder get long and lat谷歌地图地理编码器变得又长又宽
【发布时间】:2015-07-14 05:15:48
【问题描述】:

我正在尝试通过 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/

【问题讨论】:

  • `有些东西不工作`??控制台有什么错误吗?
  • @Umair 这里是 JS 小提琴jsfiddle.net/qe9soL4o/5
  • @Umair 我的意思是地图没有显示
  • 好的让我看看

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


【解决方案1】:

你正在使用MarkerWithLabel,但你没有包含它

在 head 中包含这两个文件

&lt;script src="https://maps.googleapis.com/maps/api/js?v=3.exp&amp;signed_in=true"&gt;&lt;/script&gt;

&lt;script src="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerwithlabel/src/markerwithlabel.js"&gt;&lt;/script&gt;

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

【讨论】:

  • 很高兴听到......看着你的小提琴......我学到了一个新东西,将萤火虫添加到小提琴输出......谢谢:P
猜你喜欢
  • 1970-01-01
  • 2012-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多