Gil*_*lko 5 javascript php foreach laravel laravel-4
我有一个带有纬度和经度字段的表位置。
对于每个位置,我想要一张新地图(或者更好的新标记),它使用表格位置中的纬度和经度在地图上显示一个城市。
控制器的索引动作:
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);
}
Run Code Online (Sandbox Code Playgroud)
带有标记的谷歌地图:
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);
Run Code Online (Sandbox Code Playgroud)
当我这样做时,它只显示最后插入的纬度/经度的城市:
@foreach($locations as $location)
var location = new google.maps.LatLng({{ $location->location_latitude }}, {{ $location->location_longitude }});
@endforeach
Run Code Online (Sandbox Code Playgroud)
这可能是因为在循环访问集合时,您实际上并没有创建单独的标记。
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
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6916 次 |
| 最近记录: |