检查特定地理位置(纬度和经度)是否属于';纽约';是否


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

我想知道一个特定的地理位置是否属于"美国纽约",以根据位置显示不同的内容。我只知道相应位置的纬度和经度的详细信息,有人知道处理这种情况的解决方案吗。

工作演示

使用javascript和jquery:工作演示-只需按页面顶部的"run"即可。

雅虎的GEO API

不久前,我使用雅虎的GEO API做了类似的事情。您可以使用以下YQL查询查找特定纬度和经度的位置:-

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

您可以在这里看到YQL控制台中返回的XML

要从javascript.php代码中获取此XML,可以将查询作为get字符串传递,如:-

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

这将只返回可以使用jquery的parseXML()方法解析的XML

Jquery代码示例

下面是一些javascript示例,可以做您想要做的事情:-

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