I stored the json in a variable in the application controller using HTTParty. The map displays all of the markers. The problem occurs when a marker is clicked in order to view the info window. No matter which marker is clicked, the last marker in the array's info window appears. Do you have a solution using the code that I have? I am also not sure if this is best practice mixing ruby with javascript like I did. Any input on this too would also be greatly appreciated.
The javascript which is in application.html.erb:
<script type="text/javascript">
function initialize() {
var mapOptions = {
center: { lat: 35.50, lng: -98.5},
zoom: 4
};
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
<% @place['ymca'].each do |p| %>
var infowindow = new google.maps.InfoWindow({
content: 'hello'
});
var marker = new google.maps.Marker({
position: { lat: <%= p['lat'] %>, lng: <%= p['lon'] %>},
map: map,
title: 'Random',
});
google.maps.event.addListener(marker, "click", function(event) {
infowindow.open(map, marker);
});
<% end %>
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
application_controller.rb:
def parse
uri = HTTParty.get"http://www.livestrong.org/we-can-help/ymca.json"
@place = JSON.parse(uri.body)
end
Thanks in advance!
P.S. I already used this as a reference and was not able to figure out a solution to this problem. Google Maps JS API v3 - Simple Multiple Marker Example