1
votes

I am using Google Maps API for reverse lookup of an address, specifically Country, City and ZIP code. The problem I have is that with geocoder when you input a specific lat lng you get a range of results based on the address detail accuracy. For example if you click on the street you get address detail accuracy of 8 and the address looks like this; "street, city, country code" or "street, city zip code, country code". And if you click on another location you can get address detail accuracy of 5 which returns; city zip code, country code.

All I am looking for is the the city, zip code and country. Is there any way to force Google maps to return address detail accuracy of 5 all the time or to return the elements broken up into individual parts i.e. city, zip code and country.

This is the code I am using to get the information:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml"> 
  <head> 
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
    <title>Google Maps JavaScript API Example:  Reverse Geocoder</title> 
    <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAMkjr3Dq_jK6GSFYDFzaHTBRoJ0TBOI_XK4bTNi9dL2l04KlxphRNL_k0peOib9IHF6T2KwlVmOb6uQ" type="text/javascript"></script> 
    <script type="text/javascript">

        var map;
        var geocoder;
        var address;

        function initialize() {

            var zoom = 10;

            map = new GMap2(document.getElementById("map_canvas"));
            map.setCenter(new GLatLng(40.730885, -73.997383), zoom);
            map.setUIToDefault();
            GEvent.addListener(map, "click", getAddress);
            geocoder = new GClientGeocoder();
        }

        function getAddress(overlay, latlng) {
            if (latlng != null) {
                address = latlng;
                geocoder.getLocations(latlng, showAddress);
            }
        }

        function showAddress(response) {
            map.clearOverlays();
            if (!response || response.Status.code != 200) {
                alert("Status Code:" + response.Status.code);
            } else {
                place = response.Placemark[0];
                point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
                marker = new GMarker(point);
                map.addOverlay(marker);

                var message = '<b>orig latlng:</b>' + response.name + '<br/>' +
                                '<b>latlng:</b>' + place.Point.coordinates[1] + "," + place.Point.coordinates[0] + '<br>' +
                                '<b>Status Code:</b>' + response.Status.code + '<br>' +
                                '<b>Status Request:</b>' + response.Status.request + '<br>' +
                                '<b>Address:</b>' + place.address + '<br>' +
                                '<b>Accuracy:</b>' + place.AddressDetails.Accuracy + '<br>' +
                                '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode;

                marker.openInfoWindowHtml(message);
            }
        }


    </script> 
  </head> 

  <body onload="initialize()"> 
    <div id="map_canvas" style="width: 800px; height: 600px"></div> 
  </body> 
</html>

Any help would be greatly appreciated. My last resort would be to parse the address string but it feels like an overkill. There must be a cleaner way.

I am currently looking at the getLocations() method maybe parsing the JSON result could yield the fields I am looking for.

Thanx.

1

1 Answers

1
votes

You'll need to handle differing accuracy levels in your app...the API can't do everything for you ;). I agree that it would be nice if you could dictate to the geocoder which accuracy level you'd like, it's just not there at this point. Maybe google will implement it in the future but, until then, you'll have to parse the results in your app. This post has a breakdown of the different accuracy values: Picking the most accurate geocode