Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

restful service getValues -- I cant get any values


Go to End


3 Posts   1516 Views

Avatar
theoldlr

Community Member, 103 Posts

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!

Avatar
martimiz

Forum Moderator, 1391 Posts

10 January 2011 at 12:53am

Edited: 10/01/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

Avatar
theoldlr

Community Member, 103 Posts

12 January 2011 at 3:21am

Edited: 12/01/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?