简体   繁体   English

Googlemap v3反向地理编码

[英]Googlemap v3 reverse geocoding

using googlemap v3 reverse geocoding samplesource make this source 使用googlemap v3反向地理编码samplesource制作此源代码

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();

i call function codeLatLng(); 我调用函数codeLatLng(); last line on code 代码的最后一行

so call function codeLatLng() and alert message "call codeLatLng() 1 所以调用函数codeLatLng()和警告消息“call codeLatLng()1

but doesn't call "call codeLatLng() 2" and code doesn't work 但不会调用“call codeLatLng()2”并且代码不起作用

what is wrong in my code? 我的代码有什么问题?

what is wrong in my code? 我的代码有什么问题?

Your are executing codeLatLng before your map and the geocoder variable are initialized (initialize runs when the DOM has finished loading, codeLatLng runs immediately). 您正在地图和地理编码器变量初始化之前执行codeLatLng(在DOM加载完成后初始化运行,codeLatLng立即运行)。

This would work better: 这会更好:

    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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM