Skip to content

Instantly share code, notes, and snippets.

@laurawhalin
Created March 20, 2015 18:04
Show Gist options
  • Save laurawhalin/8c5caa2f4e5906277cdd to your computer and use it in GitHub Desktop.
Save laurawhalin/8c5caa2f4e5906277cdd to your computer and use it in GitHub Desktop.
How to pass JSON from your rails controller to the google map
class WelcomeController < ApplicationController
respond_to :html, :json
def index
@coordinates = {}
@coordinates[:latitude] = 39.749813
@coordinates[:longitude] = -104.999717
# @coordinates = "#{latitude}, #{longitude}"
respond_with @coordinates
end
end
for the marker:
// put jQuery or ajax
$.get('welcome.json', function(welcomeData) {
// welcomeData.forEach(function(welcome) {
// make a new LatLng object
var latlng = new google.maps.LatLng(welcomeData.latitude, welcomeData.longitude);
// make a new Marker object
var marker = new google.maps.Marker({
position: latlng,
map: map,
// });
});
// place the marker on the map
marker.setMap(map);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment