21295 Posts in 5734 Topics by 2602 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 785 Views |
-
restful service getValues -- I cant get any values

8 January 2011 at 8:42am
I'm trying to use restful service with google maps to get the latitude and longitude of a given address.
function onBeforeWrite(){
$map = new RestfulService("http://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false", 0);
$xmlResponse=$map->request()->getBody();
//$coordinates = $map->getValues($xmlResponse,"location");
//$lat = $map->getValues($xmlResponse,"GeocodeResponse_result_geometry_location","lat");
//$long = $map->getValues($xmlResponse,"result_geometry_location","lng");parent::onBeforeWrite();
}
None of the commented getValues() above returns anything but null--obviously I've tried several different ways. The xml response is along the lines of (full response here):
<GeocodeResponse>
<status>OK</status>
<result>
<a lot of address components the same level as geometry></a lot of address components the same level as geometry>
<a lot of address components the same level as geometry></a lot of address components the same level as geometry>
<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>What am i doing wrong?
Thank you! -
Re: restful service getValues -- I cant get any values

10 January 2011 at 12:53am Last edited: 10 January 2011 12:54am
I' got this to work using the RestfulService::searchValue() method instead of getValues(). This is a little testversion I did in my Page_Controler (using your location
):
function RestfulServiceTest() {
$map = new RestfulService("http://maps.googleapis.com/maps/api/geocode/xml");
$params = array(
'address' => '1600 Amphitheatre Parkway, Mountain View, CA',
'sensor' => 'false'
);
$map->setQueryString($params);
$xmlResponse = $map->request()->getBody();
$node = '/GeocodeResponse/result/geometry/location/';
$lattitude = $map->searchValue($xmlResponse, $node . 'lat');
$longitude = $map->searchValue($xmlResponse, $node . 'lng');return "Lattitude: {$lattitude }, longitude: {$longitude}<br />";
}Hope this helps,
Martine -
Re: restful service getValues -- I cant get any values

12 January 2011 at 3:21am Last edited: 12 January 2011 3:21am
Thanks, martimiz!
That worked perfectly. For the sake of understanding, anyone have an idea why the getValues() wasn't working besides some of the wrong syntax i was using? (still doesnt work with correct syntax) I saw working examples on the forum (though they were not looking for values buried so deep in the xml) that used getValues()--Something about the way google formats the response maybe?
| 785 Views | ||
|
Page:
1
|
Go to Top |

