您好我需要从Google Geocode打印long和lat值。我使用:
检索了xml$url ="http://maps.googleapis.com/maps/api/geocode/xml?address=se18lu&sensor=false";
$result = simplexml_load_file($url);
并返回以下内容:
SimpleXMLElement Object
(
[status] => OK
[result] => SimpleXMLElement Object
(
[type] => postal_code
[formatted_address] => Bankside, London Borough of Lambeth, London SE1 8LU, UK
[address_component] => Array
(
[0] => SimpleXMLElement Object
(
[long_name] => SE1 8LU
[short_name] => SE1 8LU
[type] => postal_code
)
[1] => SimpleXMLElement Object
(
[long_name] => Bankside
[short_name] => Bankside
[type] => Array
(
[0] => sublocality
[1] => political
)
)
[2] => SimpleXMLElement Object
(
[long_name] => London
[short_name] => London
[type] => Array
(
[0] => locality
[1] => political
)
)
[3] => SimpleXMLElement Object
(
[long_name] => London Borough of Lambeth
[short_name] => London Borough of Lambeth
[type] => Array
(
[0] => administrative_area_level_3
[1] => political
)
)
[4] => SimpleXMLElement Object
(
[long_name] => Greater London
[short_name] => Gt Lon
[type] => Array
(
[0] => administrative_area_level_2
[1] => political
)
)
[5] => SimpleXMLElement Object
(
[long_name] => United Kingdom
[short_name] => GB
[type] => Array
(
[0] => country
[1] => political
)
)
)
[geometry] => SimpleXMLElement Object
(
[location] => SimpleXMLElement Object
(
[lat] => 51.5034227
[lng] => -0.1080750
)
[location_type] => APPROXIMATE
[viewport] => SimpleXMLElement Object
(
[southwest] => SimpleXMLElement Object
(
[lat] => 51.5020958
[lng] => -0.1094971
)
[northeast] => SimpleXMLElement Object
(
[lat] => 51.5047938
[lng] => -0.1067991
)
)
[bounds] => SimpleXMLElement Object
(
[southwest] => SimpleXMLElement Object
(
[lat] => 51.5032242
[lng] => -0.1087301
)
[northeast] => SimpleXMLElement Object
(
[lat] => 51.5036654
[lng] => -0.1075661
)
)
)
)
)
一切都很好。我现在需要从xml中反转long和tat值吗? 我如何仅打印/显示这些值“lat”???:例如
result->几何性质 - >位置 - > LAT
答案 0 :(得分:1)
就像你指出的那样:
$url ="http://maps.googleapis.com/maps/api/geocode/xml?address=se18lu&sensor=false";
$result = simplexml_load_file($url);
echo $result->result->geometry->location->lat;
有时您必须将simpleXML对象强制转换为字符串。然后您可以使用:
echo (string) $result->result->geometry->location->lat;
或者
echo $result->result->geometry->location->lat->__toString();
答案 1 :(得分:0)
您需要单独访问每个项目,而不是一次性完成。但是,您需要确保将值转换为字符串,int,float或w / e,因为简单xml元素的默认值是使每个项目成为SimpleXmlObject。因此,对于几何体的位置lat项,您可以这样做:
echo "Geometry Location's Lat: ".((string)$result->result->geometry->location->lat);
剩下的lat的情况也是如此
答案 2 :(得分:0)
这是我使用的内容,部分基于this answer。
function get_geocode_arr($address)
{
$arr_location = array();
$google_api_request_key = 'YOUR_KEY_HERE';
$address = str_replace (" ", "+", urlencode($address));
$fullurl = "https://maps.googleapis.com/maps/api/geocode/json?address=$address&sensor=true&key=$google_api_request_key";
$string = file_get_contents($fullurl); // get json content
$json_res = json_decode($string, true); //json decoder
foreach ($json_res['results'][0]['address_components'] as $component)
{
switch ($component['types']) {
case in_array('street_number', $component['types']):
$arr_location['street_number'] = $component['long_name'];
break;
case in_array('route', $component['types']):
$arr_location['street'] = $component['long_name'];
break;
case in_array('sublocality', $component['types']):
$arr_location['sublocality'] = $component['long_name'];
break;
case in_array('locality', $component['types']):
$arr_location['locality'] = $component['long_name'];
break;
case in_array('administrative_area_level_2', $component['types']):
$arr_location['admin_2'] = $component['long_name'];
break;
case in_array('administrative_area_level_1', $component['types']):
$arr_location['admin_1'] = $component['long_name'];
break;
case in_array('postal_code', $component['types']):
$arr_location['postal_code'] = $component['long_name'];
break;
case in_array('country', $component['types']):
$arr_location['country'] = $component['long_name'];
break;
}
}
$arr_location['location_latitude'] = $json_res['results'][0]['geometry']['location']['lat'];
$arr_location['location_longitude'] = $json_res['results'][0]['geometry']['location']['lng'];
$arr_location['formatted_address'] = $json_res['results'][0]['formatted_address'];
return $arr_location;
}