我需要从这个数组获得经度和经度:
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
}
]
}
我的控制器中的代码:
$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'));
不确定这是否会为我的地图生成标记,但我仍然不知道如何从JSON数组中获取特定信息。 提前谢谢
答案 0 :(得分:2)
要从集合中获取第一个地址,请执行以下操作:
$address = $marker->first();
要获取坐标,您可以在地址上调用getCoordinates()
。
$coordinates = $address->getCoordinates();
获得坐标后,您只需调用getLatitude()
和getLongitude()
方法即可获得纬度和经度。
$longitude = $coordinates->getLongitude();
$latitude = $coordinates->getLatitude();