I am trying to get my google maps markers to display onto my laravel project, but none of the markers seem to be showing. I have done a dd() on places and it shows that it is getting information from the database. But for some reason none of the markers seem to be showing.
JavaScript
x
46
46
1
@section('content')
2
3
4
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
5
<script src="http://maps.google.com/maps/api/js"></script>
6
<script src="https://cdnjs.cloudflare.com/ajax/libs/gmaps.js/0.4.24/gmaps.js"></script>
7
<script src="https://maps.googleapis.com/maps/api/js?key=env(GOOGLE_MAPS_API_KEY)"></script>
8
<script src="={{asset('js/app.js')}}"></script>
9
10
11
<div id="map" style="width: 100%; height: 500px;"></div>
12
<script type="text/javascript">
13
14
15
16
17
var places = <?php echo json_encode($places); ?>;
18
console.log(places);
19
var map = new google.maps.Map(document.getElementById('map'), {
20
zoom: 10,
21
center: new google.maps.LatLng(56, -1.56),
22
mapTypeId: google.maps.MapTypeId.ROADMAP
23
});
24
var infowindow = new google.maps.InfoWindow();
25
26
var marker, i;
27
28
for (i = 0; i < places.length; i++) {
29
marker = new google.maps.Marker({
30
position: new google.maps.LatLng(places[i][1], places[i][2]),
31
map: map
32
});
33
34
google.maps.event.addListener(marker, 'click', (function(marker, i) {
35
return function() {
36
infowindow.setContent(places[i][0]);
37
infowindow.open(map, marker);
38
}
39
})(marker, i));
40
}
41
42
43
44
45
</script>
46
With the controller:
JavaScript
1
8
1
public function index()
2
{
3
//
4
$crags = DB::table('places')->get();
5
6
return view('users/map',compact('places'));
7
}
8
This is what the first item of my arays looks like as well:
JavaScript
1
10
10
1
0 => {#1312
2
+"id": 2
3
+"name": "stanage"
4
+"location": "sheffield"
5
+"latitude": 53
6
+"longitude": 2
7
+"created_at": "2022-03-03 21:36:49"
8
+"updated_at": "2022-03-03 21:36:49"
9
}
10
Advertisement
Answer
JavaScript
1
10
10
1
0 => {#1312
2
+"id": 2
3
+"name": "stanage"
4
+"location": "sheffield"
5
+"latitude": 53
6
+"longitude": 2
7
+"created_at": "2022-03-03 21:36:49"
8
+"updated_at": "2022-03-03 21:36:49"
9
}
10
You should acces your place data like place[i].latitude, place[i].longitude not places[i][1]
JavaScript
1
5
1
marker = new google.maps.Marker({
2
position: new google.maps.LatLng(places[i].latitude, places[i].longitude),
3
map: map
4
});
5