1

I am beginner in Laravel. I use Laravel 5.8 in my project.

I have this object:

NominatimAddress {#1146 ▼
  -attribution: "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright"
  -category: "building"
  -displayName: "99, 9 Maja, Janowo, Olsztyn, powiat pomorski, pomorskie, "
  -osmType: "way"
  -osmId: 142789154
  -type: "yes"
  -coordinates: Coordinates {#1147 ▼
    -latitude: 51.51171143
    -longitude: 11.4023121
  }
  -bounds: Bounds {#1148 ▶}
  -adminLevels: AdminLevelCollection {#1149 ▶}
  -country: Country {#1152 ▶}
  -timezone: null
  -providedBy: "nominatim"
}

How can I get latitude and longitude from this object?

5
  • just like from any object, access with ->. $object->coordinates->latitude Commented May 31, 2019 at 13:42
  • Undefined property: Geocoder\Provider\Nominatim\Model\NominatimAddress::$coordinates - I have this error Commented May 31, 2019 at 13:44
  • This is a private property. look this stackoverflow.com/questions/56386215/… Commented May 31, 2019 at 13:44
  • I agree with @Davit that those properties are protected or private, hence not accessible from the outside. Since you didn't include what libraries are you using in the question, I'm gonna venture a guess and say that you try $object->coordinates->getLatitude() and $object->coordinates->getLongitude() as most of the time private properties that need access are hidden behind getters and setters. But as a general rule of thumb please try to include as much information in your question as you can, because it helps you get faster and more accurate answers. Commented May 31, 2019 at 13:57
  • 1
    Actually after a minute of googling I think this is the class that has the coordinates implementation github.com/geocoder-php/Geocoder/blob/master/src/Common/Model/…. Commented May 31, 2019 at 14:00

3 Answers 3

2

As I can see, you are using Geocoder library. So, NominatimAddress extends Address, which have following method:

public function getCoordinates()
{
    return $this->coordinates;
}

And as Bogdan commented, coordinates are incapsulated into Coordinates class, which has accessors for latitude and longitude.

Then you can try:

$lat = $object->getCoordinates()->getLatitude();
$lon = $object->getCoordinates()->getLongitude();
Sign up to request clarification or add additional context in comments.

Comments

0

Accessing attribute in Laravel Eloquent Model is pretty easy. It's same as accessing stdClass object's attributes.

Let's say you have variable $object that holds your object. You will access longitude and latitude attributes with:

$lat = $object->coordinates->latitude;

2 Comments

This is not working :( I have error: Undefined property: Geocoder\Provider\Nominatim\Model\NominatimAddress::$coordinates
Please edit your answer with piece of code where you're trying to access attribute, your "use" piece of code and other relevant lines so I can edit my answer accordingly.
-1
$lat = $object->coordinates->latitude;
$long = $object->coordinates->longitude;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.