0

Hey Guys,

I am having some trouble...
I have the following XML from Google:

<GeocodeResponse>
<status>OK</status>
<result>
<type>street_address</type>
<formatted_address>
1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA
</formatted_address>
<address_component>
<long_name>1600</long_name>
<short_name>1600</short_name>
<type>street_number</type>
</address_component>
<address_component>
<long_name>Amphitheatre Pkwy</long_name>
<short_name>Amphitheatre Pkwy</short_name>
<type>route</type>
</address_component>
<address_component>
<long_name>Mountain View</long_name>
<short_name>Mountain View</short_name>
<type>locality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Santa Clara</long_name>
<short_name>Santa Clara</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
<address_component>
<long_name>California</long_name>
<short_name>CA</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>94043</long_name>
<short_name>94043</short_name>
<type>postal_code</type>
</address_component>
<geometry>
<location>
<lat>37.4231054</lat>
<lng>-122.0823988</lng>
</location>
<location_type>ROOFTOP</location_type>
<viewport>
<southwest>
<lat>37.4217564</lat>
<lng>-122.0837478</lng>
</southwest>
<northeast>
<lat>37.4244544</lat>
<lng>-122.0810498</lng>
</northeast>
</viewport>
</geometry>
</result>
</GeocodeResponse>

However I ONLY want to get the lat and lng from the <location> tag
I have the following code but it doesnt work?

$xml = simplexml_load_file("http://maps.googleapis.com/maps/api/geocode/xml?address=".$final_addr."&sensor=false");

foreach($xml->GeocodeResponse->result as $item){
    echo "Lat: " . $item->geometry->location->lat . " Long: " . $item->geometry->location->lng;
}

Help Please!
Dan

3
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by veedeoo
0

Hi,

You can also try this code.. if it does not work in your server settings, you need to use cURL to fetch the xml file off google. For now, let's try this first.

This is just one of the three methods we can parse xml DATA... Pretty much the lazy approach, but it is more of a direct fetch. The foreach loop is at the minimum, unlike other methods where loops are within loops.

<?php

$final_addr = '1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA';
$someXML = ("http://maps.googleapis.com/maps/api/geocode/xml?address=".$final_addr."&sensor=false");

  $doc = new DOMDocument();

  ## if this one does not work in your server environment, you MUSt use cURL to fetch someXML

  $doc->load($someXML);
  $thisMap = $doc->getElementsByTagName( "GeocodeResponse" );
  foreach( $thisMap as $address )
  {
  $latValue = $address->getElementsByTagName( "lat" );
  $lat = $latValue->item(0)->nodeValue;
  $longValue = $address->getElementsByTagName("lng");
  $long = $longValue->item(0)->nodeValue;

  echo "<p>Lat: ".$lat."</p>";
  echo "<p>Long: ".$long."</p>";
  }
  ?>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.