【发布时间】:2014-04-29 23:11:35
【问题描述】:
我有一个应用程序将 twitter 用户的位置存储在数据库中(来自 API GET/search tweets - location)。然而这里的问题是它需要用户在他的个人资料中输入的任何内容。我的数据库中已包含的示例数据:
All over the world & London
Boca Raton, Florida
Sunway Damansara, Malaysia
Munich
Brazil
40.769549,-73.993696
Long Island City, NY USA
United States
USA-Japan
如您所见,存在很大差异(+垃圾信息,例如“位置:在我的世界中”),当我尝试处理这些数据时,这会产生一个大问题。我想要做的是有一个脚本,它将实际解析值并将每一行更新为一个国家(例如,慕尼黑将更改为德国,纽约可能更改为美国 - 因为它只是一个原型应用程序,而不是产品)。我在 PHP & MySQL 中做所有事情。
对于如何解决这个问题有什么建议吗?
更新
我找到了这个例子,这有点像我想做的,但它并不适用于每个值。例如,当我进入伦敦或雅典时,它显示美国,但是当我进入慕尼黑时,它显示德国......知道为什么吗?
<?php
// Get STATE from Google GeoData
function reverse_geocode($address) {
$address = str_replace(" ", "+", "$address");
$url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false";
$result = file_get_contents("$url");
$json = json_decode($result);
foreach ($json->results as $result)
{
foreach($result->address_components as $addressPart) {
if((in_array('locality', $addressPart->types)) && (in_array('political', $addressPart->types)))
$city = $addressPart->long_name;
else if((in_array('administrative_area_level_1', $addressPart->types)) && (in_array('political', $addressPart->types)))
$state = $addressPart->long_name;
else if((in_array('country', $addressPart->types)) && (in_array('political', $addressPart->types)))
$country = $addressPart->long_name;
}
}
if(($city != '') && ($state != '') && ($country != ''))
$address = $city.', '.$state.', '.$country;
else if(($city != '') && ($state != ''))
$address = $city.', '.$state;
else if(($state != '') && ($country != ''))
$address = $state.', '.$country;
else if($country != '')
$address = $country;
// return $address;
return "$country/$state/$city";
}
// Usage: In my case, I needed to return the State and Country of an address
$myLocation = reverse_geocode("Athens");
echo "$myLocation";
?>
实际上在美国优先搜索。所以它显示的是美国的雅典和伦敦,而不是希腊和英国。但有人怎么能改变呢?有没有可能?
【问题讨论】:
-
我想知道您是否可以将它提供给 Google Maps API 之类的东西,它会告诉您(如果有效,第一个和最后一个不是)原产国是什么。 (虽然从技术上讲,第一个在 Google 地图搜索中返回伦敦。)
-
好吧,开始尝试使用 google maps API 并发现了这个:example 它实际上适用于某些地方,但并非适用于所有地方。例如,如果我进入雅典或伦敦,它会显示美国,但是当我进入慕尼黑时,它会显示德国.. 我想知道为什么.. 我将通过添加来更新我的问题
-
您应该在问题中包含您的代码。
-
@JaredFarrish 刚刚包含了我发现并尝试在我的情况下进行调整的代码。