我试图通过将地名(例如伦敦)转换为lang和lat值来检索long和lat值。 我正在使用此API: http://maps.googleapis.com/maps/api/geocode/xml?address=
这是请求:
maps.googleapis.com/maps/api/geocode/xml?address=london&sensor=false
这是它给我的XML文档:
<?xml version="1.0" encoding="UTF-8"?>
<GeocodeResponse>
<status>OK</status>
<result>
<type>locality</type>
<type>political</type>
<formatted_address>London, UK</formatted_address>
<address_component>
<long_name>London</long_name>
<short_name>London</short_name>
<type>locality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>England</long_name>
<short_name>England</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>United Kingdom</long_name>
<short_name>GB</short_name>
<type>country</type>
<type>political</type>
</address_component>
<address_component>
<long_name>London</long_name>
<short_name>London</short_name>
<type>postal_town</type>
</address_component>
<geometry>
<location>
<lat>51.5081289</lat>
<lng>-0.1280050</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>51.3968770</lat>
<lng>-0.3841238</lng>
</southwest>
<northeast>
<lat>51.6191098</lat>
<lng>0.1281138</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>51.3849401</lat>
<lng>-0.3514684</lng>
</southwest>
<northeast>
<lat>51.6723432</lat>
<lng>0.1482710</lng>
</northeast>
</bounds>
</geometry>
</result>
<result>
<type>locality</type>
<type>political</type>
<formatted_address>London, ON, Canada</formatted_address>
<address_component>
<long_name>London</long_name>
<short_name>London</short_name>
<type>locality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Middlesex County</long_name>
<short_name>Middlesex County</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Ontario</long_name>
<short_name>ON</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Canada</long_name>
<short_name>CA</short_name>
<type>country</type>
<type>political</type>
</address_component>
<geometry>
<location>
<lat>42.9793980</lat>
<lng>-81.2461380</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>42.8245170</lat>
<lng>-81.3908520</lng>
</southwest>
<northeast>
<lat>43.0732450</lat>
<lng>-81.1063880</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>42.8245170</lat>
<lng>-81.3908520</lng>
</southwest>
<northeast>
<lat>43.0732450</lat>
<lng>-81.1063880</lng>
</northeast>
</bounds>
</geometry>
</result>
<result>
<type>locality</type>
<type>political</type>
<formatted_address>London, KY, USA</formatted_address>
<address_component>
<long_name>London</long_name>
<short_name>London</short_name>
<type>locality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Laurel</long_name>
<short_name>Laurel</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Kentucky</long_name>
<short_name>KY</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>United States</long_name>
<short_name>US</short_name>
<type>country</type>
<type>political</type>
</address_component>
<geometry>
<location>
<lat>37.1289771</lat>
<lng>-84.0832646</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>37.0797589</lat>
<lng>-84.1262620</lng>
</southwest>
<northeast>
<lat>37.1522600</lat>
<lng>-84.0359570</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>37.0797589</lat>
<lng>-84.1262620</lng>
</southwest>
<northeast>
<lat>37.1522600</lat>
<lng>-84.0359570</lng>
</northeast>
</bounds>
</geometry>
</result>
<result>
<type>locality</type>
<type>political</type>
<formatted_address>London, OH 43140, USA</formatted_address>
<address_component>
<long_name>London</long_name>
<short_name>London</short_name>
<type>locality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Madison</long_name>
<short_name>Madison</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Ohio</long_name>
<short_name>OH</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>United States</long_name>
<short_name>US</short_name>
<type>country</type>
<type>political</type>
</address_component>
<address_component>
<long_name>43140</long_name>
<short_name>43140</short_name>
<type>postal_code</type>
</address_component>
<geometry>
<location>
<lat>39.8864493</lat>
<lng>-83.4482529</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>39.8592800</lat>
<lng>-83.4789230</lng>
</southwest>
<northeast>
<lat>39.9217860</lat>
<lng>-83.3899969</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>39.8592800</lat>
<lng>-83.4789230</lng>
</southwest>
<northeast>
<lat>39.9217860</lat>
<lng>-83.3899969</lng>
</northeast>
</bounds>
</geometry>
</result>
</GeocodeResponse>
我正在使用PHP解析XML。 这是我的代码:
$geoResponce = new SimpleXMLElement($response);
$geoLocatonLat = $geoResponce->result[0]->geometry->location->lat;
但我只是得到空值 你能告诉我哪里出错吗?感谢
此外: 这是我的var_dump($ geoResponce);
ssobject(SimpleXMLElement)#108 (2) {
["status"]=>
string(2) "OK"
["result"]=>
object(SimpleXMLElement)#120 (5) {
["type"]=>
string(5) "route"
["formatted_address"]=>
string(32) "Eldonian Way, Liverpool L3 6, UK"
["address_component"]=>
array(8) {
[0]=>
object(SimpleXMLElement)#121 (3) {
["long_name"]=>
string(12) "Eldonian Way"
["short_name"]=>
string(12) "Eldonian Way"
["type"]=>
string(5) "route"
}
[1]=>
object(SimpleXMLElement)#122 (3) {
["long_name"]=>
string(9) "Liverpool"
["short_name"]=>
string(9) "Liverpool"
["type"]=>
array(2) {
[0]=>
string(8) "locality"
[1]=>
string(9) "political"
}
}
[2]=>
object(SimpleXMLElement)#123 (3) {
["long_name"]=>
string(9) "Liverpool"
["short_name"]=>
string(9) "Liverpool"
["type"]=>
array(2) {
[0]=>
string(27) "administrative_area_level_3"
[1]=>
string(9) "political"
}
}
[3]=>
object(SimpleXMLElement)#124 (3) {
["long_name"]=>
string(9) "Liverpool"
["short_name"]=>
string(9) "Liverpool"
["type"]=>
array(2) {
[0]=>
string(27) "administrative_area_level_2"
[1]=>
string(9) "political"
}
}
[4]=>
object(SimpleXMLElement)#125 (3) {
["long_name"]=>
string(7) "England"
["short_name"]=>
string(7) "England"
["type"]=>
array(2) {
[0]=>
string(27) "administrative_area_level_1"
[1]=>
string(9) "political"
}
}
[5]=>
object(SimpleXMLElement)#126 (3) {
["long_name"]=>
string(14) "United Kingdom"
["short_name"]=>
string(2) "GB"
["type"]=>
array(2) {
[0]=>
string(7) "country"
[1]=>
string(9) "political"
}
}
[6]=>
object(SimpleXMLElement)#127 (3) {
["long_name"]=>
string(4) "L3 6"
["short_name"]=>
string(4) "L3 6"
["type"]=>
array(2) {
[0]=>
string(18) "postal_code_prefix"
[1]=>
string(11) "postal_code"
}
}
[7]=>
object(SimpleXMLElement)#128 (3) {
["long_name"]=>
string(9) "Liverpool"
["short_name"]=>
string(9) "Liverpool"
["type"]=>
string(11) "postal_town"
}
}
["geometry"]=>
object(SimpleXMLElement)#129 (4) {
["location"]=>
object(SimpleXMLElement)#130 (2) {
["lat"]=>
string(10) "53.4189242"
["lng"]=>
string(10) "-2.9926240"
}
["location_type"]=>
string(16) "GEOMETRIC_CENTER"
["viewport"]=>
object(SimpleXMLElement)#131 (2) {
["southwest"]=>
object(SimpleXMLElement)#133 (2) {
["lat"]=>
string(10) "53.4171964"
["lng"]=>
string(10) "-2.9940368"
}
["northeast"]=>
object(SimpleXMLElement)#134 (2) {
["lat"]=>
string(10) "53.4208640"
["lng"]=>
string(10) "-2.9913388"
}
}
["bounds"]=>
object(SimpleXMLElement)#132 (2) {
["southwest"]=>
object(SimpleXMLElement)#134 (2) {
["lat"]=>
string(10) "53.4171964"
["lng"]=>
string(10) "-2.9934545"
}
["northeast"]=>
object(SimpleXMLElement)#133 (2) {
["lat"]=>
string(10) "53.4208640"
["lng"]=>
string(10) "-2.9919211"
}
}
}
["partial_match"]=>
string(4) "true"
}
}
enter code here
enter code here
enter code here
答案 0 :(得分:2)
试试这个
$geoResponce = simplexml_load_string($response);
$geoLocatonLat = $geoResponce->result[0]->geometry->location->lat;
它将打印51.5081289
你可以用lng元素重复它
答案 1 :(得分:0)
以下是Json从谷歌接收数据的功能
// The Google Geocoding API url used to get the JSON data
var geocodingAPI = "http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true";
$.getJSON(geocodingAPI, function (json) {
// Set the variables from the results array
var address = json.results[0].formatted_address;
// console.log('Address : ', address);
var latitude = json.results[0].geometry.location.lat;
// console.log('Latitude : ', latitude);
var longitude = json.results[0].geometry.location.lng;
// console.log('Longitude : ', longitude);
// Set the table td text
$('#address').text(address);
$('#latitude').text(latitude);
$('#longitude').text(longitude);
});
您可以在网站的任何位置调用此功能