Search code examples
javascriptgoogle-maps-api-3mapsgoogle-geocoder

Google Geocoder returns the same set of lat/lng for several different addresses. Lat/Lng are the coordinates of the City or Township and not address


I am sending the google.geocoder several addresses, but the values in the results[0].geometry.location are all the same. I believe I have accounted for the asynchronous nature of the call using a call back. When I add alerts to see the values returned, the addresses passed in the geocoder.geocode( { 'address': addr }... are all correct, the status returned is "ok", but the lat/long are the same for every call. I am not very well versed in JavaScript, and am new to the .net environment, so any help would be greatly appreciated.

This code worked perfectly from 4/1/2012 until some time near December or early 2013. Has something changed with the Google API? I have looked at google's website but cannot find anything.

Here is my initial call:

    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>

<script type="text/javascript">   

 var geocoder;
 var map;
 var directionsDisplay;
 var directionsRenderer;
 var startPoint;
 var endPoint;

 function initialize() 
 {
  geocoder = new google.maps.Geocoder();

   codeAddress();
   var myOptions = 
   {
    zoom: 12,
    mapTypeId: google.maps.MapTypeId.ROADMAP
   }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);  

   var trafficLayer = new google.maps.TrafficLayer();
   trafficLayer.setMap(map);

   setMarkers(map);

   google.maps.event.addListener(map, 'click', function(event) {
   dirMarker(event.latLng);
   startPoint = event.latLng;
      });
  }


 function codeAddress() 
 {
   var address =  document.getElementById("<%=hCVT.ClientID%>").value;

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

     if (status == google.maps.GeocoderStatus.OK) {
      alert("Status: " + status + "res from CODE ADDRESS -- " +  results[0].geometry.location);  //TO REMOVE
     map.setCenter(results[0].geometry.location);
    } else {
      alert("Geocode of CVT was not successful for the following reason: " + status);
     }
   });
 }

The function to set the markers and info window information (I have removed some of the code that seemed irrelevant to the question)

    function setMarkers(map) 
    {
     // Add markers to the map

      var shape = {
      coord: [1, 1, 1, 20, 18, 20, 18 , 1],
      type: 'poly'
     };

  var places = new Array([,,,,]);
     var xx = String; 
     xx = document.getElementById("<%=addys.ClientID%>").value;
     var placeholder = xx.split(",");

     var latlng;


     var i = 0;
     for(var y = 0; y < (placeholder.length / 5 - 1); i=i+5)
     {

     places[y, 0] = placeholder[i];
     places[y, 1] = placeholder[i+1]; //Unit Status
     places[y, 2] = placeholder[i+2]; // Long - not used
     places[y, 3] = placeholder[i+3]; // Zindex
     places[y, 4] = placeholder[i+4]; // HTML for information window

     addr = places[y,0];
     ustat = places[y,1];   
     zind = places[y,3];
     iwdata = places[y,4];

     getLatLong(addr, iwdata, ustat, zind, function(latlng, addr, iwdata, ustat, zind)   {






var marker = new google.maps.Marker({
     position: latlng,
     map: map,
     html: iwdata,
     icon: pinImage,
     shadow: pinShadow,
     shape: shape,
     title: addr,
     zIndex: parseInt(places[y,3])
     });

   var infowindow = new google.maps.InfoWindow({
           content: iwdata}); 



  });

   y = y + 1;
  }

   }

The function where I believe the problem lies follows. Since there were several addresses being sent to google, I added a built in timeout so as to not exceed the limitations. Again, this all worked for about 8 months and has suddenly stopped. Where several markers used to show on the map, now there is one marker which is seeming over written because the lat/lng return is the same as the initial call in the codeAddress() function. In an effort to find a solution I have been adding alerts to show me the return values. I was getting confused with the bugzilla values as they would take me over to the google javascript which was completely over my head.

     function getLatLong(addr, iwdata, ustat, zind, callback){

       geocoder.geocode( { 'address': addr}, function(results, status){       
        if (status == google.maps.GeocoderStatus.OK){                       
          callback(results[0].geometry.location, addr, iwdata, ustat, zind);
            } else if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {

             window.setTimeout(function() {self.getLatLong(addr, iwdata, ustat, zind, callback);
             },500);
            } else {
            alert("Address Geocode failure: " + addr + " ==== " + status + "Y value: " + zind + " res ---" + res);
            }
    });
    }

Can anyone help me with this?


Solution

  • Well, after some long hours of staring at this code trying to figure it out, I finally stumbled on something using smartly placed alerts. Apparently the addr variable contained "'" and the geocoder was not able to provide lat/lng data as a result. Adjusted this line:

    addr = places[y,0].replace(/'/g, "");
    

    All works now.