Вопрос

I would like to take an array and load the data into the json object, but i am stuck on exactly how to do it.

TripsController

def index
@trips = Trip.all
@markers = Array.new
@trips.each do |trip|
  for marker in trip.markers
    @markers.push(marker)
  end
end

@gmaps_options =
{
  "markers"     => { "data" => '[{ "lng": "-99.9018131", "lat": "31.9685988"},
                                 { "lng": "-102.552784", "lat": "23.634501"},
                                 { "lng": "-122.3667", "lat": "40.5833"},
                                 { "lng": "-121.8356", "lat": "39.7400"}
                                  ]', },
  "polylines"   => { "data" => ' [ [
                     {"lng": -99.9018131, "lat": 31.9685988},
                     {"lng": -102.552784, "lat": 23.634501},
                     ], [
                      { "lng": "-122.3667", "lat": "40.5833"},
                      { "lng": "-121.8356", "lat": "39.7400"}
                      ] ]' }
}

respond_to do |format|
  format.html # index.html.erb
  format.json { render json: @trips }
  end 
end

The longitude and latitude are hardcoded right now but i would like to load the @markers into the longitude latitude,

"lng": @markers[0].longitude, "lat": @markers[0].latitude

This whole data object is a single string. I would have to add

"lng": + "@markers[0].longitude" + , "lat": + "@markers[0].latitude"

for every marker object, so in some hackey way make a whole string in a for loop. I am thinking there has to be a better way.

In the view the map is called with gmaps(@gmaps_options)

Any help would be greatly appreciated.

Это было полезно?

Решение

Not sure about what all you have in your application, but from the controller code it looks like you have model Trip with association has_many :markers

and Marker model have latitude and longitude fields.

and also you want to display all those associated markers.

Now to display the pins on google-map it requires proper activerecord-relation records

so one-way to do is

CONTROLLER:

@trips = Trip.pluck(:id)
@markers = Marker.where("trip_id in (?)", @trips)
@gmaps_options = @markers.to_gmaps4rails

VIEW:

= gmaps("markers" => { data: @gmaps_options })

Another way is:

form the json string of the all location's latitude and longitude with the help of loop and pass that string to data in view, as you have done above, because .to_gmaps4rails does not accept array.

Hope this would help you.

Thanks.

Другие советы

Here is the final code working for polylines with gmaps4rails

def index
@trips = Trip.all
@tripLocation = Trip.pluck(:id)
@markers = Marker.where("trip_id in (?)", @tripLocation)
@gmaps_options = @markers.to_gmaps4rails

@polylines_json = {}
polylines = []

i = 0;
@trips.each do |trip|
  polylines[i] = []
  trip.markers.each do |marker|
    polylines[i] += [{:lng=>marker.longitude.to_f, :lat=>marker.latitude.to_f}]
  end
  i+=1
end

@polylines_json = polylines.to_json

respond_to do |format|
  format.html # index.html.erb
  format.json { render json: @trips }
end
end

def show
@trip = Trip.find(params[:id])
@markers = @trip.markers
@gmaps_options = @markers.to_gmaps4rails

@polylines_json = {}
polylines = [] 
polylines[0] = []
  @trip.markers.each do |marker|
    polylines[0] += [{:lng=>marker.longitude.to_f, :lat=>marker.latitude.to_f}]
  end


@polylines_json = polylines.to_json

respond_to do |format|
  format.html # show.html.erb
  format.json { render json: @trip }
end
end
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top