I have a table locations with a latitude and longitude field.
For each location I want a new map (or better a new marker) that uses the latitude and longitude from the table locations to display a city on the map.
index action of controller :
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);
When I do this it only displays the city from the last inserted latitude/longitude :
@foreach($locations as $location)
var location = new google.maps.LatLng({{ $location->location_latitude }}, {{ $location->location_longitude }});
@endforeach
It is probably because while looping through the collection you are not actually creating separate marker.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With