【问题标题】:Googlemap v3 reverse geocodingGooglemap v3 反向地理编码
【发布时间】:2013-11-21 13:07:44
【问题描述】:

使用 googlemap v3 反向地理编码示例源制作此源

var map;
        var geocoder;
        var marker;
        function initialize() {
            geocoder = new google.maps.Geocoder();
            var mapOptions = {
                zoom : 14,
                center : new google.maps.LatLng(30, 30)
            };
            map = new google.maps.Map(document.getElementById('map-canvas'),
                    mapOptions);
        }

        function codeLatLng() {
              var latlng = new google.maps.LatLng(30, 30);
              alert("call codeLatLng() 1");
              geocoder.geocode({'latLng': latlng}, function(results, status) {
                  alert("call codeLatLng() 2");
                if (status == google.maps.GeocoderStatus.OK) {
                  if (results[1]) {
                    map.setZoom(11);
                    marker = new google.maps.Marker({
                        position: latlng,
                        map: map
                    });
                    infowindow.setContent(results[1].formatted_address);
                    infowindow.open(map, marker);
                  } else {
                    alert('No results found');
                  }
                } else {
                  alert('Geocoder failed due to: ' + status);
                }
              });
            }


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

我调用函数codeLatLng();代码的最后一行

所以调用函数codeLatLng()和警告信息“call codeLatLng() 1

但不调用“call codeLatLng() 2”且代码不起作用

我的代码有什么问题?

【问题讨论】:

  • 错误控制台中有任何消息吗?
  • 没有控制台干净没有错误
  • 我会尝试从数据处理函数中取出一些代码,只留下警报。另外,我会在数据处理函数之后放置另一个警报,但仍然在 codeLatLng() 函数中。

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


【解决方案1】:

我的代码有什么问题?

您在地图和地理编码器变量初始化之前执行 codeLatLng(初始化在 DOM 完成加载时运行,codeLatLng 立即运行)。

这样会更好:

    var map;
    var geocoder;
    var marker;
    function initialize() {
        geocoder = new google.maps.Geocoder();
        var mapOptions = {
            zoom : 14,
            center : new google.maps.LatLng(30, 30)
        };
        map = new google.maps.Map(document.getElementById('map-canvas'),
                mapOptions);

        // map and geocoder initialized
        codeLatLng();
    }

    function codeLatLng() {
          var latlng = new google.maps.LatLng(30, 30);
          alert("call codeLatLng() 1");
          geocoder.geocode({'latLng': latlng}, function(results, status) {
              alert("call codeLatLng() 2");
            if (status == google.maps.GeocoderStatus.OK) {
              if (results[1]) {
                map.setZoom(11);
                marker = new google.maps.Marker({
                    position: latlng,
                    map: map
                });
                infowindow.setContent(results[1].formatted_address);
                infowindow.open(map, marker);
              } else {
                alert('No results found');
              }
            } else {
              alert('Geocoder failed due to: ' + status);
            }
          });
        }


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

【讨论】:

  • 感谢我认为地理编码器在 google.maps.event.addDomListener(window, 'load', initialize) 中的初始值;
  • 地理编码器在 onload 函数中被初始化。但这在页面加载之前不会运行,在 codeLatLng 在您的原始代码中运行之后。
猜你喜欢
  • 1970-01-01
  • 2012-01-28
  • 2012-06-08
  • 2011-09-27
  • 1970-01-01
  • 2015-10-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多