Google map api geocoder.geocode返回相同的值

时间:2017-02-26 02:32:36

标签: javascript google-maps geocode postal-code

我希望从多邮政编码获得纬度和经度,但是当我遍历邮政编码数组以调用geocoder.geocode方法时,它会返回相同的纬度和经度值。我的源代码在

之下
    var postcodeList = [
    {postcode: "ON N6A 1A1", name: "1. Lawson Cafe"},
    {postcode: "ON N6J 2J8", name: "2. LA Cafe"},
    {postcode: "ON N6J 1H6", name: "3. Frangos COffee Shop"},
    {postcode: "ON N6C 3P4", name: "4. Cortado"},
    {postcode: "ON N6B 2K9", name: "5. Walker's Restaurant"},
    {postcode: "ON N6B 1L4", name: "6. Kambie Chinese Restaurant"},
    {postcode: "ON N6C 4M8", name: "7. Curry's Restaurant"}
];
var googleMap;
function initialize() {
    googleMap = new google.maps.Map(document.getElementById('map'), {
        zoom: 15,
        mapTypeControl: false,
        center: new google.maps.LatLng(51.509865, -0.118092),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    for (i = 0; i < postcodeList.length; i++) {
        searchAddress(postcodeList[i].postcode);
    }
}

function searchAddress(postcode) {
    var geocoder = new google.maps.Geocoder();
    var location;

    geocoder.geocode({'address': postcode}, function(results, status) {

            if (status == google.maps.GeocoderStatus.OK) {

                    console.log("PostCode: " + postcode);
                    console.log("  Latitude: " + results[0].geometry.location.lat());
                    console.log("  Longtitude: " + results[0].geometry.location.lng());
            } else {
                    console.log("Geocode was not successful for the following reason: " + status);
            }
    });
}

以上源代码的结果是

PostCode: ON N6A 1A1
   Latitude: 42.9975524
   Longtitude: -81.25463660000003
PostCode: ON N6J 2J8
   Latitude: 42.9535959
   Longtitude: -81.27941229999999
PostCode: ON N6J 1H6
   Latitude: 42.9535959
   Longtitude: -81.27941229999999
PostCode: ON N6C 3P4
   Latitude: 42.9730003
   Longtitude: -81.2540343
PostCode: ON N6B 2K9
   Latitude: 42.9818077
   Longtitude: -81.23811510000002
PostCode: ON N6B 1L4
   Latitude: 42.9818077
   Longtitude: -81.23811510000002
PostCode: ON N6C 4M8
   Latitude: 42.9711174
   Longtitude: -81.2363939

在上述结果输出中,第2个,第3个的纬度和经度是相同的值,第5个,第6个的纬度和经度也是相同的值。 我不知道为什么,请帮帮我

1 个答案:

答案 0 :(得分:1)

抱歉,我认为我的邮政编码数据是正确的。 更新邮政编码数据后,问题就解决了

var postcodeList = [
{postcode: "ON N6A1A1", name: "1. Lawson Cafe"},
{postcode: "ON N6J2J8", name: "2. LA Cafe"},
{postcode: "ON N6J1H6", name: "3. Frangos COffee Shop"},
{postcode: "ON N6C3P4", name: "4. Cortado"},
{postcode: "ON N6B2K9", name: "5. Walker's Restaurant"},
{postcode: "ON N6B1L4", name: "6. Kambie Chinese Restaurant"},
{postcode: "ON N6C4M8", name: "7. Curry's Restaurant"}

];