我有一個包含地址的字符串php變量。可以說$ adress ='Universitätsring1,1010 Wien'。將字符串轉換爲Google座標
有沒有辦法將這個變量轉換成Google地圖座標並將它們保存在其他變量中?
真的不知道如何使用地理編碼。
謝謝!
我有一個包含地址的字符串php變量。可以說$ adress ='Universitätsring1,1010 Wien'。將字符串轉換爲Google座標
有沒有辦法將這個變量轉換成Google地圖座標並將它們保存在其他變量中?
真的不知道如何使用地理編碼。
謝謝!
是的,你可以,使用地理編碼提供的XML,可以把地址作爲參數的基礎上,你可以得到lat和長:
$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;
}
是的,你可以通過使用谷歌地圖API地址解析
天氣預報做到這一點是將地址(如「1600 劇場百匯,山景,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"
}
是的,我去過那裏:)但我有很長的一個代碼,我的地圖插件至極包含即 VAR的MapOptions = { 中心:新google.maps .LatLng(49.2 692714,14.38580539999998), ..這些座標怎麼能被鏈接中的thos替換? – muzzie
追加你的地址與上面的網址,如 address = $ your_address,並使用curl命中鏈接,它將返回json或xml作爲迴應 –
您可以使用谷歌地理編碼API。
您需要在谷歌開發者控制檯註冊應用程序,然後您可以發送請求與您的地址,如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
哇,真好,謝謝。之所以問的是我有我的地圖插件長的js代碼包含即 VAR的MapOptions = { 中心:新google.maps.LatLng(40.2692714,12.38580539999998), ..現在我可以很容易地更換座標由$緯度和$經度..但我認爲這不是地理編碼應該工作的方式,是嗎? :D – muzzie
太棒了。樂意效勞。 – Ionut
@ muzzie,如果你想得到一個位置,它就是這樣工作的。它沒有錯。在你的情況下,你在PHP中獲取值並將它們發送到JavaScript我想用AJAX。您也可以使用Javascript來獲取該位置的座標,但也許您必須使用PHP,因爲您從服務器端收到該地址。 – Ionut