I want to use the geocoder without geofield and openlayers. But it seems to be impossible to get to the geocoded data in the object.

My code looks like this:

$geoloc = geocoder('google', 'Berlin');
var_dump($geoloc);

var_dump outputs the following:

object(Point)#128 (4) { 
  ["position":"Point":private]=> array(2) { 
    [0]=> float(13.40629) 
    [1]=> float(52.524268) 
  } 
  ["geom_type":protected]=> string(5) "Point" 
  ["geos":"Geometry":private]=> NULL 
  ["srid":protected]=> NULL 
} 

I tried literally everything to use the lat and lng floats as seperated data. But the only way i was able to output the data was through var_dump. I even converted it to an array, but still couldn't use the lat and lng data, to center a map for example.
How do I use this object?? I'm deeply frustated.

Comments

michaelfavia’s picture

Status: Active » Closed (fixed)

This is more a generic php question but Im happy to help:

As you can see form the var_dump() you posted the Geoloc object youre dumping is an object of type "Point". This object is defined in the geophp library (hat tip to patrick). The code for this class is here:

https://github.com/phayes/geoPHP/blob/master/lib/geometry/Point.class.php

More specifically you might be interested in:
$x = $geoloc->x();
$y = $geoloc->y();

or

$coords = $geoloc->getCoordinates();

Please reopen with any questions.