0

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

2
  • how could you know that always the last infowindow content is showing?? For all the markers you've used the same content hello. So, all the markers have the same content right? Commented Feb 12, 2015 at 18:12
  • I looked at the json array and the location of the last item in the array matches the point on the map. I just used hello as a temporary place holder until I get everything working. Any marker that is clicked on opens that same info window that is in the same location. Commented Feb 12, 2015 at 18:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.