I am Building an app on Rails 5, Ruby 2.4.0 and trying to use Google Maps API V3.
That said, I have two locations in my locations model that I want to display on a map displayed on my locations/index.html.erb.
I followed this stack question in hopes I could modify it to meet my needs, however to no avail. The Map its self loads, but none of the markers appear.
My api link is in my application/layout.html.erb in the head, and the following is found in my locations/index.html.erb file:
<script type="text/javascript">
var locations = [
<% @locations.each do |location| %>
{ lat:<%= location.latitude %>, lng:<%= location.longitude %> },
<% end %>
];
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>
Essentially I was trying to loop through each location and provide the lat and lng of each location into a new marker and display on the index page map.
Any assistance here would be greatly appreciated as I am really stuck here now!
Thanks.
EDIT #1