Laravel和google地图:为每个纬度/经度显示标记或地图
Laravel and google maps : foreach latitude/longitude show marker or map
我有一个包含经度和纬度字段的表位置
对于每个位置,我想要一个新的地图(或者最好是一个新的标记),它使用来自表位置的经纬度在地图上显示一个城市。
控制器索引动作:
public function index()
{
$locations = Location::all();
$locations->location_latitude = Input::get('location_latitude');
$locations->location_longitude = Input::get('location_longitude');
return View::make('forecasts.index')
->with('locations', $locations);
}
google map with marker:
function initialize() {
var map_canvas = document.getElementById('map_canvas');
var location = new google.maps.LatLng({{ $location->location_latitude }}, {{ $location->location_longitude }});
var map_options = {
center: location,
zoom: 10,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(map_canvas, map_options)
var marker = new google.maps.Marker({
position: location,
map: map,
});
marker.setMap(map);
}
google.maps.event.addDomListener(window, 'load', initialize);
当我这样做时,它只显示最后插入的纬度/经度的城市:
@foreach($locations as $location)
var location = new google.maps.LatLng({{ $location->location_latitude }}, {{ $location->location_longitude }});
@endforeach
这可能是因为在循环遍历集合时,您实际上并没有创建单独的标记。
function initialize() {
var map_canvas = document.getElementById('map_canvas');
// Initialise the map
var map_options = {
center: location,
zoom: 10,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(map_canvas, map_options)
// Put all locations into array
var locations = [
@foreach ($locations as $location)
[ {{ $location->location_latitude }}, {{ $location->location_longitude }} ]
@endforeach
];
for (i = 0; i < locations.length; i++) {
var location = new google.maps.LatLng(locations[i][0], locations[i][1]);
var marker = new google.maps.Marker({
position: location,
map: map,
});
}
// marker.setMap(map); // Probably not necessary since you set the map above
}
相关文章:
- “可绘制地图”设置地图选项“纬度-经度”
- 如何用javascript获取谷歌地图的经度和纬度
- 使用传单在地图框上显示纬度和经度
- 如何使用谷歌地图中的纬度和经度搜索附近的位置
- 如何使用XML中的经度和纬度值在地图上显示位置
- 使用地图框 API 将邮政编码转换为纬度和经度
- 给定带有位置“loc”的链接,搜索使用谷歌地图API从标记中提取纬度和经度
- 谷歌地图标记在通过变量设置标记和经度时不保持不变
- 在钛地图中计算纬度增量和经度增量[Android]
- 如何将纬度经度从HTML表单传递到谷歌地图
- 谷歌地图:从地图元素中获取纬度和经度
- 将诸如“;纽约水牛城”;使用Google Maps API将地图转换为纬度和经度
- Amcharts-地图:自定义地图上的错误坐标(纬度/经度)(罗马尼亚县级)
- 纬度/经度在D3.js地图上的位置
- 更改纬度和经度谷歌地图 API
- 海上交通地图 - 纬度/经度 Javascript
- 谷歌地图和GeoJSon:纬度和经度转换
- 谷歌地图获得具有城市名称的纬度和经度
- 谷歌地图API将标记的纬度和经度添加到信息窗口
- 当谷歌地图标记点移动时,使用纬度和经度更新数据库