google map markers does not appear my script
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Google Maps Multiple Markers</title>
<script src="http://maps.google.com/maps/api/js?sensor=false"
type="text/javascript"></script>
</head>
<body>
<div id="map" style="width: 500px; height: 400px;"></div>
<script type="text/javascript">
var locations = <?php print_r(json_encode($locations)) ?>;
console.log(locations);
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: new google.maps.LatLng(-33.92, 151.25),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var marker, i;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
</script>
</body>
</html>
my controller
public function index()
{
$locations = Message::get()
->toArray();
return view('welcome',compact('locations'));
}
i don't know why my locations markers doesn't appear and when i run console.log(locations) return object not array
You don't need to use print_r here AFAIK. This should work as well
<script type="text/javascript">
var locations = <?php echo json_encode($locations); ?>;
// Your javascript here
</script>
Note: Depending on what is in $locations you either get a javascript array or object. You can always loop over an object or get an array item from there.
Please sign in or create an account to participate in this conversation.