Siva
Siva

Reputation: 1133

Check whether a particular geo location (latitude and longitude) belongs to 'New York' or not?

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.

Upvotes: 0

Views: 888

Answers (1)

rgvcorley
rgvcorley

Reputation: 2933

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');

});

Upvotes: 2

Related Questions