Skip to content

Instantly share code, notes, and snippets.

@dvisockas
Created November 20, 2022 10:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dvisockas/ae7fa94bcce61169cb8d22f07dd12b5f to your computer and use it in GitHub Desktop.
Save dvisockas/ae7fa94bcce61169cb8d22f07dd12b5f to your computer and use it in GitHub Desktop.
Car geojson example
class CarsController < ApplicationController
def index
@cars = Car.all
render json: cars_to_geojson(@cars)
end
private
def cars_to_geojson(cars)
{
type: :FeatureCollection,
features: cars.map do |car|
{
type: :Feature,
geometry: {
type: :Point,
coordinates: [
car.lon,
car.lat,
],
},
properties: {
title: car.title,
id: car.id,
},
}
end,
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment