Search code examples
javascriptgoogle-mapsgeocodepostal-code

Google map api geocoder.geocode return the same value


I want to get latitude and longtitude from multi postcode, but when I loop through postcode array to call geocoder.geocode method, it returns the same latitude and longtitude value. My source code is below

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

The result of above source code is

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

In above result output, the latitude and longtitude of 2nd, 3rd are the same values, the latitude and longtitude of 5th, 6th also are the same value. I don't know why, please help me


Solution

  • Sorry, I think my postcode data is in correct. After I updated the postcode data, the problem is solved

    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"}
    

    ];