Geocode returning weird results

362 views
Skip to first unread message

Brian Saxberg

unread,
Apr 8, 2011, 10:08:31 AM4/8/11
to google-maps-ap...@googlegroups.com
I am doing some testing of the geocode api using the example on building a dealer locator based on PHP/MySQL.

I am sitting in the USA and when I test with a "bad" zip code, say 99999.  I do not get a ZERO_RESULTS status code.  I get an OK with this lat/lng returned (35.342412, -79.77566300000001).  This puts me way out in the pacific ocean.

Here is the code in question, which works beautifully if you enter in something that it can find.  I am trying to figure out how to handle not getting put out into the ocean when it should be zero results returned?

   function searchLocations() {
     var address = document.getElementById("addressInput").value;
     var geocoder = new google.maps.Geocoder();
     geocoder.geocode({address: address}, function(results, status) {
       if (status == google.maps.GeocoderStatus.OK) {
        searchLocationsNear(results[0].geometry.location)
       } else {
         alert(address + ' was not found');
       }
     });
   }

Chris Broadfoot

unread,
Apr 9, 2011, 5:40:42 AM4/9/11
to google-maps-ap...@googlegroups.com
See here:

Remember the geocoder is not a zip code service, it's for geocoding addresses.

(35.342412, -79.77566300000001) isn't in the Pacific Ocean, anyway, it's in North Carolina.


--
You received this message because you are subscribed to the Google Groups "Google Maps API Web Services" group.
To post to this group, send email to google-maps-ap...@googlegroups.com.
To unsubscribe from this group, send email to google-maps-api-web-...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-maps-api-web-services?hl=en.



--
http://twitter.com/broady

Brian Saxberg

unread,
Apr 11, 2011, 8:57:05 AM4/11/11
to google-maps-ap...@googlegroups.com
I see your link returns somewhat of a valid address. 

Here is my javascript function that is hitting the geocode API.


   function searchLocations() {
     var address = document.getElementById("addressInput").value;
     var geocoder = new google.maps.Geocoder();
     geocoder.geocode({address: address}, function(results, status) {
       if (status == google.maps.GeocoderStatus.OK) {
        searchLocationsNear(results[0].geometry.location)
       } else {
         alert(address + ' was not found');
       }
     });
   }

function searchLocationsNear(center) {
     clearLocations();
     circle.setMap(null);
       
     var radius = document.getElementById('radiusSelect').value;
     var searchUrl = 'genxml.php?lat=' + center.lat() + '&lng=' + center.lng() + '&radius=' + radius;
     downloadUrl(searchUrl, function(data) {
        var customIcons = {
          ALL: {icon: 'images/goog/star.png'},
          ADL: {icon: 'images/goog/yellow.png'},
          ADP: {icon: 'images/goog/blue.png'},
          SUN: {icon: 'images/goog/red.png'},
          IDL: {icon: 'images/goog/black.png'}
        };         
       var xml = parseXml(data);
       var markerNodes = xml.documentElement.getElementsByTagName("marker");
       var bounds = new google.maps.LatLngBounds();
       for (var i = 0; i < markerNodes.length; i++) {
         var name = markerNodes[i].getAttribute("name");
         var address = markerNodes[i].getAttribute("address");
         var distance = parseFloat(markerNodes[i].getAttribute("distance"));
         var latlng = new google.maps.LatLng(
              parseFloat(markerNodes[i].getAttribute("lat")),
              parseFloat(markerNodes[i].getAttribute("lng")));
        var icon = customIcons[markerNodes[i].getAttribute("type")] || {};
             
         createOption(name, distance, i);
         createMarker(latlng, name, address, icon);
         bounds.extend(latlng);
       }
       map.fitBounds(bounds);
      // alert (map.getCenter.toString());
       locationSelect.style.visibility = "visible";
       locationSelect.onchange = function() {
         var markerNum = locationSelect.options[locationSelect.selectedIndex].value;
         google.maps.event.trigger(markers[markerNum], 'click');
       };
      // Add a Circle overlay to the map.
      var mapCenter = '(' + center.lat() + ',' + center.lng() + ')';
      var circleOptions = {
        center: center,
        map: map,
        clickable: false,
        radius: (radius * 1609.344),
        fillColor: "#FF0000",
        fillOpacity: 0.1,
        strokeColor: "#FF0000",
        strokeOpacity: 1.0,
        strokeWeight: 3,
        zIndex: 300};     
     
        circle.setOptions(circleOptions);
   
      });
    }

I will see if I can get my page on an internet facing site and link it so you can see it in action.  Im trying to play with it to see what the users will see if I don't find a dealer in their "radius".  I have more work to do on this to make it more robust.

Thank you for the help!

Chris Broadfoot

unread,
Apr 11, 2011, 6:39:45 PM4/11/11
to google-maps-ap...@googlegroups.com
Why don't you just check for the postal_code type? Example:
Reply all
Reply to author
Forward
0 new messages