Google地图地理编码无法正常工作(无标记)

时间:2014-06-29 07:04:18

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

我有一个网页可以找到纬度,经度并获得该位置的标记。我使用谷歌地图。

我的网页从用户输入,地址1和地址2获取2个地址,并调用codeAddress()

<div id="panel">
  <input id="address1" type="textbox" value="">
  <input id="address2" type="textbox" value="">
  <input type="button" value="find!" onclick="codeAddress()">
</div>

这是我的JavaScript代码:

var map;
var address1 = document.getElementById('address1').value;
var address2 = document.getElementById('address2').value;

function initialize() {
    var latlng = new google.maps.LatLng(-7.275920, 112.791871);
    var mapOptions = {
        zoom: 12,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
}

function codeAddress() {
    var gc = google.maps.Geocoder();
    gc.geocode({
        'address': address1
    }, function (res1, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            gc.geocode({
                'address': address2
            }, function (res2, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    new google.maps.Marker({
                        position: res1[0].geometry.location,
                        map: map
                    });
                    new google.maps.Marker({
                        position: res2[0].geometry.location,
                        map: map
                    });
                }
            });
        }
    });
}

当我点击按钮find时,我没有得到标记。任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

像这样修改codeAddress函数:

function codeAddress() {
    var gc = new google.maps.Geocoder(); // notice new keyword
    initialize(); // Calling initialize. If you skip it, maps aren't loading
    gc.geocode({
        'address': address1
    }, function(res1, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            gc.geocode({
                'address': address2
            }, function(res2, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    new google.maps.Marker({
                        position: res1[0].geometry.location,
                        map: map
                    });
                    new google.maps.Marker({
                        position: res2[0].geometry.location,
                        map: map
                    });
                }
            });
        }
    });

确保两个输入都有一些值来测试它。

演示:http://jsfiddle.net/lotusgodkk/GCu2D/213/

答案 1 :(得分:0)

  • 在函数运行时从表单更新地址变量

    function codeAddress() {
      var address1 = document.getElementById('address1').value;
      var address2 = document.getElementById('address2').value;
    
  • 检查状态!=确定

    } else alert("Geocode failed of " + address1 + ", status=" + status);
    
  • 在Google地图Geocoder构造函数之前使用“new”

    var gc = new google.maps.Geocoder();
    
  • 在显示新标记之前删除现有标记,以使标记不会在地图上累积

    if (marker1 && marker1.setMap) marker1.setMap(null);
    

完整代码:

    var map = null;
    var marker1 = null;
    var marker2 = null;
    var gc = new google.maps.Geocoder();

    function initialize() {
      var latlng = new google.maps.LatLng(-7.275920, 112.791871);
      var mapOptions = {
        zoom: 12,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      }
      map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
      codeAddress();
    }

    function codeAddress() {
      var address1 = document.getElementById('address1').value;
      var address2 = document.getElementById('address2').value;
      gc.geocode({
        'address': address1
      }, function (res1, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            gc.geocode({
                'address': address2
            }, function (res2, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    if (marker1 && marker1.setMap) marker1.setMap(null);
                    marker1 = new google.maps.Marker({
                        position: res1[0].geometry.location,
                        map: map
                    });
                    if (marker2 && marker2.setMap) marker2.setMap(null);
                    marker2 = new google.maps.Marker({
                        position: res2[0].geometry.location,
                        map: map
                    });
                } else alert("Geocode failed of " + address2 + ", status=" + status);
            });
        } else alert("Geocode failed of " + address1 + ", status=" + status);
      });
    }
    google.maps.event.addDomListener(window, 'load', initialize);

working fiddle