我有一个包含地址的String php变量。让我们说$ adress ='Universitätsring1,1010Wien'。
有没有办法将此变量转换为Google地图坐标并将其保存在其他变量中?
并且真的不知道如何使用地理编码。
谢谢!
答案 0 :(得分:0)
是的,您可以使用 Google地图API地理编码
来完成此操作地理编码是转换地址的过程(如“1600” Amphitheatre Parkway,Mountain View,CA“)进入地理坐标 (如纬度37.423021和经度-122.083739),您可以使用 将标记放在地图上或定位地图。
例如:
https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=YOUR_API_KEY
它将返回JSON或XML,如:
{
"results" : [
{
"address_components" : [
{
"long_name" : "1600",
"short_name" : "1600",
"types" : [ "street_number" ]
},
{
"long_name" : "Amphitheatre Pkwy",
"short_name" : "Amphitheatre Pkwy",
"types" : [ "route" ]
},
{
"long_name" : "Mountain View",
"short_name" : "Mountain View",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Santa Clara County",
"short_name" : "Santa Clara County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "California",
"short_name" : "CA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "94043",
"short_name" : "94043",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
"geometry" : {
"location" : {
"lat" : 37.4224764,
"lng" : -122.0842499
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 37.4238253802915,
"lng" : -122.0829009197085
},
"southwest" : {
"lat" : 37.4211274197085,
"lng" : -122.0855988802915
}
}
},
"place_id" : "ChIJ2eUgeAK6j4ARbn5u_wAGqWA",
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
答案 1 :(得分:0)
您可以使用Google地理编码API
您需要在google开发者控制台中注册应用程序,然后您可以使用您的地址发送请求,例如
https://maps.googleapis.com/maps/api/geocode/json?address=Universitätsring 1, 1010 Wien&key=YOUR_API_KEY
阅读本指南,应该有所帮助。
https://developers.google.com/maps/documentation/geocoding/start
答案 2 :(得分:0)
是的,您可以使用地理编码提供的xml,可以将地址作为参数,基于您可以得到lat和long:
$adress = 'Universitätsring 1, 1010 Wien';
$adress = urlencode($adress);
$url = 'http://maps.googleapis.com/maps/api/geocode/xml?address=' . $adress . '&sensor=true';
$xml = simplexml_load_file($url);
$status = $xml->status;
if ($status == 'OK') {
$latitude = $xml->result->geometry->location->lat;
$longitude = $xml->result->geometry->location->lng;
}
如果你想将它作为json或数组,你可以转换xml:
$adress = 'Universitätsring 1, 1010 Wien';
$adress = urlencode($adress);
$url = 'http://maps.googleapis.com/maps/api/geocode/xml?address=' . $adress . '&sensor=true';
$xml = simplexml_load_file($url);
$json = json_encode($xml);
$array = json_decode($json, true);
if ($array['status'] == 'OK') {
$latitude = $array['result']['geometry']['location']['lat'];
$longitude = $array['result']['geometry']['location']['lng'];
echo $latitude;
echo $longitude;
}