我被要求找到特定地点的经纬度。用户将提供该位置的地址。我该如何获得经纬度?非常感谢您的任何建议。
我被要求找到特定地点的经纬度。用户将提供该位置的地址。我该如何获得经纬度?非常感谢您的任何建议。
为了将地址文本转换成经纬度坐标对,您需要进行所谓的地理编码。通常,这方面的良好服务都有一定的配额或付费订阅模式,但您可以在以下网址找到一些免费的API:
http://www.programmableweb.com/news/7-free-geocoding-apis-google-bing-yahoo-and-mapquest/2012/06/21
进一步在Google上搜索,你会找到其他提供商,因为我已经提供了关键词:地理编码。许多人都有JavaScript、PHP、C#和其他实现示例,可以帮助你入门。
class Google
{
private function __construct() { }
public static function getGoogleJson($address)
{
$apiLink = 'http://maps.google.com/maps/api/geocode/json?sensor=false&language=ru&address=' . urlencode($address);
$tmp = @json_decode(file_get_contents($apiLink));
if (!$tmp) {
return new stdClass;
} else {
return $tmp;
}
}
public static function getGeoByAddress($address)
{
$addr = self::getGoogleJson($address);
if (isset($addr->results[0]->geometry->location)) {
return (array)$addr->results[0]->geometry->location;
} else {
return ['lat' => 0, 'lng' => 0];
}
}
}
注意!Google每天使用此API有一定限制。