0

I'm working with SimpleGeo API and I need to get place's latitude/longitude by ZIP code or city name. I know there is a plenty of questions in SO about getting coordinates by ZIP code, and there are many nice databases for it. But are they something that correlates closely with SimpleGeo's data? I don't want, having a precise city name/ZIP code that exists in SimpleGeo's database, to get to nowhere just because my database differs from SG's one.

Alternatively, I can move completely to Foursquare API, if it provides such data, but this is less preferable.

So, my questions are:

  1. Can I get latitude/longitude by ZIP code or city name through SimpleGeo (or at least Foursquare)?
  2. If not, can I use other databases (e.g. MaxMind or Geonames) to get same results? Should I care about differences between databases at all?
ffriend
  • 27,562
  • 13
  • 91
  • 132

2 Answers2

1

Yes, you can use the SimpleGeo Context APIs to do this. Call the SimpleGeo context endpoint with the city name or zip code as the address parameter like this: http://api.simplegeo.com/1.0/context/address.json?address=98006

The response has the latitude and longitude embedded in the "query" part of the JSON blob returned:
{ "query":{ "latitude":40.745717, "longitude":-73.988121 ... }, ... }

Hope this helps.

Thanks,
-Nikhil

SlickNik
  • 26
  • 2
0

In case it helps... Here's how you can use google's geocoder to get the lat/lng for zipcode. Say you have an HTML input with the id 'location' (uses the jqueryui autocomplete widget):

// insert a div for autocomplete results
$("#location").after('<div id="location-results"></div>');

$("#location").autocomplete({
    appendTo:$("#location-results"),
    minLength: 3,
    open: acOpen,
    select: acSelect,
    close: acClose,
    source: acSource
});

function acOpen(event, ui){
// no op, but put code here to handle results list open if you need it
}

function acSelect(event, ui){
    // fired when user selects a result from the autocomplete list
    // assuming you are using the json2.js library for json parsing/decoding
    alert(JSON.stringify(ui.item));
    // the lat/lng is in ui.item.latLng.lat() and ui.item.latLng.lng()
    // ui.item.latLng is of type google.maps.LatLng (see v3 api)
}

function function acClose(event, ui){
// no op, but put code here to handle results list close if you need it
}

function acSource(request, response) {
    // implements the autocomplete source for the widget
    geocoder.geocode( {'address': request.term }, function(results, status) {
        if(undefined!==results){
            var postal_code='';
            var city = ''; 
            var state = '';
            var country = '';
            response($.map(results, function(item) {
              $.each(item.address_components, function(item_i, item_v){
                $.each(item_v.types, function(type_i, type_v){
                  switch(type_v){
                    case "locality": case "administrative_area_level_3":
                      city = item_v.long_name;
                    break;
                    case "administrative_area_level_2":
                    break;
                    case "street_number":
                    break;
                    case "route":
                    break;
                    case "administrative_area_level_1":
                  state = item_v.long_name;
                break;
                case "country":
                  country = item_v.long_name;
                break;
                case "postal_code":
                  postal_code = item_v.long_name;
                break;
              }
        });
          });
              // the object that gets returned and is ui.item on select event
          return {
            label   : item.formatted_address,
            value   : item.formatted_address,
            latLng  : item.geometry.location,
            country     : country, 
                state       : state, 
                city        : city,  
                postal_code : postal_code,
            source  : "google"
          };
        }));
        }
      }
    }
}

google's geocoder can geocode zipcodes, no problem.

Michael Reed
  • 2,362
  • 23
  • 15
  • It's important to note that Google's geocoder CAN NOT be used without including a GMAP alongside it (it's against the TOS). And no, you can't just use `display="none"` on the map's div... – tylerl Aug 28 '11 at 05:05