Search code examples
javascriptrubygoogle-mapsruby-on-rails-4gmaps4rails2

How to add an additional marker to a google map containing markers


The code below is what I am trying to use in order to add markers to the maerkers var and then add an additional marker to the handler. The second handler.addMarker method just overwrites my first setting of the markers. How can I concatenate using the gmaps4rails gem?

markers = handler.addMarkers(<%=raw @hash.to_json %>);


markers = handler.addMarker({
  lat: position.coords.latitude,
  lng: position.coords.longitude
});

Solution

  • Using JavaScript you can add your multiple marker by creating new object for each one.

    function initialize() {
      var myLatlng_first = new google.maps.LatLng(-25.363882,131.044922);
      var myLatlng_second = new google.maps.LatLng(-20.253256,131.044922);
      var mapOptions = {
        zoom: 4,
        center: myLatlng
      }
      var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
    
      var marker_first = new google.maps.Marker({
          position: myLatlng,
          map: map,
          title: 'Hello World!'
      });
    
    
       var marker_second = new google.maps.Marker({
          position: myLatlng,
          map: map,
          title: 'Hello World!'
      });
    }
    
    google.maps.event.addDomListener(window, 'load', initialize);