0

I'm wanting to reverse geocode coordinate to get the address using Geocoder-php. I'm able to get the Address Collection using $geo = app('geocoder')->reverse($lat, $lng)->get().

According to these docs found on github for Geocoder-php you can retrieve the properties form the collection, such as street name by using $geo->getStreetName(), the city by using $geo->getCity(), the country by using $geo->getCountry(), etc, but there are no methods for returning the State or Province.

How do I grab the state or province from the collection?

I can see the province when I use $geo->getAdminLevels()->get(). Which returns

AdminLevelCollection {#900 ▼
  -adminLevels: array:2 [▼
    1 => AdminLevel {#901 ▼
      -level: 1
      -name: "Saskatchewan"
      -code: "SK"
    }
    2 => AdminLevel {#902 ▼
      -level: 2
      -name: "Division No. 16"
      -code: "Division No. 16"
    }
  ]
}

But I am unable to grab the province (Saskatchewan).

(I am using Laravel Geocoder for a Laravel project.)

EDIT:

$geo->getAdminLevels()->get(1) give me this

AdminLevel {#690 ▼
  -level: 1
  -name: "Saskatchewan"
  -code: "SK"
}

But $geo->getAdminLevels()->get(1)->name; give me this error

Undefined property: Geocoder\Provider\GoogleMaps\Model\GoogleAddress::$getAdminLevels
xslibx
  • 4,249
  • 9
  • 35
  • 52

2 Answers2

1

The solution to this is to call it this way;

$geo->getAdminLevels()->get(1)->getName();

get() in this context takes a value of the index you wish to return.

You might want to check that the getAdminLevels() and get(1) return values if they may sometimes return nulls. You can do this by implementing something like !empty($geo->getAdminLevels())

Simon R
  • 3,732
  • 4
  • 31
  • 39
  • If that doesn't work; it may be $geo->getAdminLevels()->adminLevels->get(); – Simon R Apr 11 '19 at 22:32
  • `$geo->getAdminLevels()->get();` throws an error`Too few arguments to function Geocoder\Model\AdminLevelCollection::get(), 0 passed in /path/to/my/Controller.php exactly 1 expected` and `$geo->getAdminLevels()->adminLevels->get();` throws an error `Cannot access private property Geocoder\Model\AdminLevelCollection::$adminLevels` – xslibx Apr 12 '19 at 05:39
  • Try $geo->getAdminLevels()->get(1); Does this give you anything? If it does try $geo->getAdminLevels->get(1)->name; – Simon R Apr 15 '19 at 20:08
  • Thanks for your effort. Still getting an error. Take a look at my updated question. – xslibx Apr 16 '19 at 04:42
  • OK name is a private property on the AdminLevel class - you'll need to do $geo->getAdminLevels->get(1)->getName(); If that works, I'll update my answer. – Simon R Apr 16 '19 at 05:35
  • Error again `Undefined property: Geocoder\Provider\GoogleMaps\Model\GoogleAddress::$getAdminLevels` :( – xslibx Apr 16 '19 at 05:45
  • 1
    Can you confirm you're calling this: $geo->getAdminLevels()->get(1)->getName(); also are you using the same lat, lng each time? – Simon R Apr 16 '19 at 06:11
  • Hey that did it! `$geo->getAdminLevels()->get(1)->getName()` works! Please update your answer – xslibx Apr 16 '19 at 06:24
0

You can do $geo->getAdminLevels()->get()->adminLevels[0]->name or $geo->getAdminLevels()->get()->adminLevels[1]->name

gpasci
  • 1,420
  • 11
  • 16
  • Both throws an error `Too few arguments to function Geocoder\Model\AdminLevelCollection::get(), 0 passed in /path/to/my/Controller.php exactly 1 expected` – xslibx Apr 12 '19 at 05:41