0

I want to find out whether a particular geo location belongs to the 'New York, US' or not to show different content based on the location. I just have the corresponding location's latitude and longitude details, does anybody knows a solution to handle this scenario.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Siva
  • 1,123
  • 2
  • 14
  • 25
  • Google maps geocoding service would do the trick. – ScottE Feb 17 '12 at 11:44
  • thanks Mark, your comment gave a an idea to try out something even though I have not used the polygon algorythm... – Siva Feb 21 '12 at 05:39
  • thanks Scott, it would be great if you provide a snippet of code to handle this scenario using the Google maps geocoding. – Siva Feb 21 '12 at 05:42

1 Answers1

2

Working demo

using javascript and jquery:- Working demo - just press 'run' at the top of the page.

Yahoo's GEO API

I did something similar to this a while back using yahoo's GEO API. You can look up the locality of a specific lattitude and longitude with the following YQL query:-

select locality1 from geo.places where text="40.714623,-74.006605"

You can see the XML that is returned in the YQL console here

To get this XML from your javascript/php code you can pass the query as a GET string like:-

http://query.yahooapis.com/v1/public/yql?q=[url encoded query here]

This will return just the XML which you can parse using jquery's parseXML() method

Example Jquery code

Here is some example javascript to do what you're after:-

// Lat and long for which we want to determine if in NY or not
var lat = '40.714623';
var long = '-74.006605';

// Get xml fromyahoo api
$.get('http://query.yahooapis.com/v1/public/yql', {q: 'select locality1 from geo.places where text="' + lat + ',' + long + '"'}, function(data) {

// Jquery's get will automatically detect that it is XML and parse it
// so here we create a wrapped set of the xml using $() so we can use
// the usual jquery selecters to find what we want   
$xml = $(data);

// Simply use jquery's find to find 'locality1' which contains the city name
$city = $xml.find("locality1").first();

// See if we're in new york
if ($city.text() == 'New York')
    alert(lat + ',' + long + ' is in new york');
else
    alert(lat + ',' + long + ' is NOT in new york');

});
rgvcorley
  • 2,883
  • 4
  • 22
  • 41
  • thanks rgvcorley, actually I am using SimpleGeo API for this check which may not be 100% perfect, your solution using Yahoo GEO API is looking cool with minimal coding. I will try to use this, thanks a lot... – Siva Feb 21 '12 at 05:49
  • do you have any idea to handle this scenario using Google Maps geocoding? – Siva Feb 21 '12 at 05:50
  • @Siva Yes it's as simple as using `https://maps.googleapis.com/maps/api/geocode/xml` as the get url and `latlng : '40.714623,-74.006605',sensor: 'false'` as the parameters. Then you'd just need to find a node with 'New York' to check ([click here to see result form Google API](https://maps.googleapis.com/maps/api/geocode/xml?latlng=40.714623,-74.006605&sensor=false)). See [here](http://code.google.com/apis/maps/documentation/geocoding/) for details on google API. Could you select my answer - I've just started on SO and would much appreciate it :) – rgvcorley Feb 23 '12 at 14:50