Show markers on google maps dynamically -Rails 3.2

后端 未结 2 1600
离开以前
离开以前 2021-01-07 08:43

i have a working code that shows multiple markers on google map using geocoder such as @nearbys = Place.near(\"#{params[:address]}\", 5,:order => \"di

相关标签:
2条回答
  • 2021-01-07 09:13

    well after struggling with jquery arrays and json ,i was able to find this best solution...this is working in all scenarios and all i need is to handle empty and null conditions,which i already did and its working the way i needed.HOPE THIS HELPS SOMEONE

    my controller

    def show_nearby_locations
    
      @nearbys = Place.near("#{params[:address]}", 5,:order => "distance",:units => :km)
      @nearbys.first(10)
    
    end
    

    in my view file

    <%= javascript_tag do%>
     window.nearbys= <%=raw @nearbys.to_json %>;
    
    <%end%>
    

    this is my script inside view

    function initialize() {
        var map;
        var bounds = new google.maps.LatLngBounds();
        var mapOptions = {
            mapTypeId: 'roadmap'
        };
    
        // Display a map on the page
        map = new google.maps.Map(document.getElementById("map-canvas-guest"), mapOptions);
        map.setTilt(45);            
    
        var markers=[];
        var infoWindowContent=[];
        $.each(nearbys, function(index) { 
    
            console.log( data[0][index].latitude,data[0][index].longitude );
            markers.push([nearbys[index]['title'], nearbys[index]['latitude'],nearbys[index]['longitude']]);
            infoWindowContent.push(['<div class="info_content" >' +
            '<h3 style="color: rgb(<%= rand(0..200)%>,<%= rand(0..200)%> ,<%= rand(0..200)%>);">'+nearbys[index]['title']+'</h3>' +'</br>'+
            '<p>'+nearbys[index]['about']+'</p>' +       
             '</div>']);
    
            });   
    
           // Display multiple markers on a map
           var infoWindow = new google.maps.InfoWindow(), marker, i;        
          // Loop through our array of markers & place each one on the map  
          for( i = 0; i < markers.length; i++ ) {
              var position = new google.maps.LatLng(markers[i][1], markers[i][2]);
              bounds.extend(position);
              marker = new google.maps.Marker({
                position: position,
                map: map,
                title: markers[i][0]
            });
    
            // Allow each marker to have an info window    
            google.maps.event.addListener(marker, 'click', (function(marker, i) {
                return function() {
                    infoWindow.setContent(infoWindowContent[i][0]);
                    infoWindow.open(map, marker);
    
    
                }
            })(marker, i));
    
            // Automatically center the map fitting all markers on the screen
            map.fitBounds(bounds);
    
        }
    
        // Override our map zoom level once our fitBounds function runs (Make sure it only runs once)
        var boundsListener = google.maps.event.addListener((map), 'bounds_changed', function(event) {
            this.setZoom(14);
            google.maps.event.removeListener(boundsListener);
        });
    
    }//initialize ends
    
    0 讨论(0)
  • 2021-01-07 09:27

    Hello Friends by doing with Google Maps With Rails i have searched the information from google maps site and Gmaps4rails i have done Multiple Markers and Info-window her is the code

    My Controller

        @users = User.all
        @hash = Gmaps4rails.build_markers(@users) do |user, marker|
          marker.lat user.latitude
          marker.lng user.longitude
          marker.infowindow user.title` 
        end
    

    In My View

    window.onload = function () {
        var markers = <%=raw @hash.to_json %>;
        var mapOptions = {
            center: new google.maps.LatLng(markers[0].lat, markers[0].lng),
            zoom: 15,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var infoWindow = new google.maps.InfoWindow();
        var map = new google.maps.Map(document.getElementById("map"), mapOptions);
        for (i = 0; i < markers.length; i++) {
            var data = markers[i]
            var myLatlng = new google.maps.LatLng(data.lat, data.lng);
            marker = new google.maps.Marker({
                position: myLatlng,
                map: map,
                title: data.title
            });
            (function (marker, data) {
                google.maps.event.addListener(marker, "click", function (e) {
                    infoWindow.setContent(data.description);
                    infoWindow.open(map, marker);
                });
            })(marker, data);
        }
    }
    
    0 讨论(0)
提交回复
热议问题