0

I need to get latitude and longitude from this array:

AddressCollection {#237 ▼
-addresses: array:1 [▼
0 => Address {#232 ▼
  -coordinates: Coordinates {#233 ▼
    -latitude: 55.923431
    -longitude: 23.3490609
  }
  -bounds: Bounds {#234 ▶}
  -streetNumber: "18"
  -streetName: "Vilniaus gatvė"
  -subLocality: null
  -locality: "Šiauliai"
  -postalCode: "76258"
  -adminLevels: AdminLevelCollection {#235 ▶}
  -country: Country {#236 ▶}
  -timezone: null
}
]
}

Code in my controller:

 $posts = Post::latest()->get();  
     Mapper::location('Lietuva')->map(['zoom' => 7, 'scrollWheelZoom'=>false, 'marker'=>false]); //A map is displayed in the index page
    foreach($posts as $post){

        $city = $post->city;
        $address = $post->address;

        $mark = app('geocoder')->geocode($city . $address)->get();

        dd($mark);
        //
        //THIS IS WHERE I NEED THOSE COODINATES
        //
        //For example $lat = $mark->coordinates.......

        //Mapper::marker($lat, $long); Make a marker for every address in the DB
    }
 return view('posts.index', compact('posts'));

Not sure if this will generate markers for my map, but i still have no idea how to get specific information from JSON array. Thank you in advance

3
  • 1
    Per the docs you should be able to do $marker->first()->getCoordinates()? Commented Mar 8, 2017 at 19:38
  • @jfadich It worked, how should i now access latitude and longitude? I have this now: Coordinates {#233 ▼ -latitude: 55.923431 -longitude: 23.3490609 } Commented Mar 8, 2017 at 20:11
  • 1
    Did you try anything before asking? Again per the Docs it's just $marker->first()->getCoordinates()->getLatitude() or $marker->first()->getCoordinates()->getLongitude() Commented Mar 8, 2017 at 20:21

1 Answer 1

2

To get the first address from the collection you do:

$address = $marker->first();

To get the coordinates you can call getCoordinates() on the address.

$coordinates = $address->getCoordinates();

Once you have the coordinates you just need to call the getLatitude() and getLongitude() methods to get the latitude and longitude respectively.

$longitude = $coordinates->getLongitude();
$latitude  = $coordinates->getLatitude();
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.