Okay so I've never worked with SimpleXML before and I'm having a few problems
Here's my PHP:
map.api.php
$location = $_SESSION['location'];
$address = $location; // Business Location
$prepAddr = str_replace(' ','+',$address);
$feedUrl="http://nominatim.openstreetmap.org/search?q=". $prepAddr ."&format=xml&addressdetails=1&polygon=1";
$sxml = simplexml_load_file($feedUrl);
foreach($sxml->attributes() as $type){
$lat = $type->place['lat'];
$long = $type->place['lon'];
}
And here's an example of the XML table I'm working from.
<searchresults timestamp="Thu, 28 Jan 16 14:16:34 +0000" attribution="Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright" querystring="M27 6bu, 149 Station road, Swinton, Manchester" polygon="true" exclude_place_ids="65827001" more_url="http://nominatim.openstreetmap.org/search.php?format=xml&exclude_place_ids=65827001&accept-language=en-US,en;q=0.8&polygon=1&addressdetails=1&q=M27+6bu%2C+149+Station+road%2C+Swinton%2C+Manchester">
<place place_id="65827001" osm_type="way" osm_id="32861649" place_rank="26" boundingbox="53.5122168,53.5190893,-2.3402445,-2.3331231" lat="53.5156919" lon="-2.3368185" display_name="Station Road, Newtown, Salford, Greater Manchester, North West England, England, M27 4AE, United Kingdom" class="highway" type="secondary" importance="0.5">
<road>Station Road</road>
<suburb>Newtown</suburb>
<town>Salford</town>
<county>Greater Manchester</county>
<state_district>North West England</state_district>
<state>England</state>
<postcode>M27 4AE</postcode>
<country>United Kingdom</country>
<country_code>gb</country_code>
</place>
</searchresults>
I want to select the "lat" and "lon" attributes from <place>, but when I echo $lat
and $long
they are empty, why?
Okay so I fixed this myself.
Instead of running everything through the foreach
loop (as shown below):
foreach($sxml->attributes() as $type){
$lat = $type->place['lat'];
$long = $type->place['lon'];
}
I just directly got the attribute values and stored them in a variable:
$lat = $sxml->place->attributes()->lat;
$long = $sxml->place->attributes()->lon;
This then returned an error/warning: Warning: main() [function.main]: Node no longer exists
By using isset
and checking if the value exists first, you can get around this.
if (isset($sxml->place))
{
$lat = $sxml->place->attributes()->lat;
$long = $sxml->place->attributes()->lon;
}