2013-11-26 42 views
0

我正在解析google map api以获取latlong表单地址。我得到了xml成功。如何从xml响应中获取latlong。用于Xml解析的PHP curl

我的回答是

SimpleXMLElement对象([状态] => OK [结果] => SimpleXMLElement对象([型] =>数组([0] =>本地[1] =>政治)的formatted_address ] =>新德里,印度[address_component] => Array([0] => SimpleXMLElement Object([long_name] => New Delhi [short_name] => New Delhi [type] => Array([0] => (1)=>政治))[1] => SimpleXMLElement对象([long_name] => West Delhi [short_name] => West Delhi [type] => Array([0] => administrative_area_level_2 [1] => political ))[2] => SimpleXMLElement Object([long_name] => Delhi [short_name] => DL [type] => Array([0] => administrative_area_level_1 [1] => political))[3] => ([long_name] =>印度[short_name] => IN [类型] =>数组([0] => country [1] => political)))[geometry] => SimpleXMLElement Object([location] => SimpleXMLElement Object([lat] => 28.6353080 [lng] => 77.2249600)[location_type] => APPROXIMATE [viewport] => SimpleXMLElement Object([southwest] => SimpleXMLElement Object([lat] => 28.4010669 [lng] => 76.8396999)[northeast] => SimpleXMLElement Object([lat] => 28.8898159 [lng] => 77.3418146 )] [bounds] => SimpleXMLElement Object([southwest] => SimpleXMLElement Object([lat] => 28.4010669 [lng] => 76.8396999)[northeast] => SimpleXMLElement Object([lat] => 28.8898159 [lng] => 77.3418146)))))

我想要得到的几何性质 - >位置 - >经纬度值

帮我从这个问题

由于事先

回答

2

它与DOM + XPath的更容易,所述DOMXpath :: evaluate()方法可以读取出的XML的标量值:

$xml = <<<'XML' 
<GeocodeResponse> 
<status>OK</status> 
<result> 
    <geometry> 
    <location> 
    <lat>37.4217550</lat> 
    <lng>-122.0846330</lng> 
    </location> 
    <location_type>ROOFTOP</location_type> 
    <viewport> 
    <southwest> 
    <lat>37.4188514</lat> 
    <lng>-122.0874526</lng> 
    </southwest> 
    <northeast> 
    <lat>37.4251466</lat> 
    <lng>-122.0811574</lng> 
    </northeast> 
    </viewport> 
    </geometry> 
</result> 
</GeocodeResponse> 
XML; 

$dom = new DOMDocument(); 
$dom->loadXml($xml); 
$xpath = new DOMXpath($dom); 

$lat = $xpath->evaluate('number(/GeocodeResponse/result/geometry/location/lat)'); 
$lng = $xpath->evaluate('number(/GeocodeResponse/result/geometry/location/lng)'); 

var_dump($lat, $lng); 

输出:

float(37.421755) 
float(-122.084633)