public
Last active

Get google maps integrated with your app -minimal code

  • Download Gist
google map rails integration
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#------ application layout
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script type="text/javascript" src="http://code.google.com/apis/gears/gears_init.js"></script>
<%= javascript_include_tag "jquery", "jquery-ui","rails" %>
<%= javascript_include_tag "application" %>
.....
<script type="text/javascript">
<%= yield(:javascript)%>
</script>
 
 
#----------application.js
 
var initialLocation;
//example of a location/point
var newyork = new google.maps.LatLng(40.69847032728747, -73.9514422416687);
var browserSupportFlag = new Boolean();
var map;
var infowindow = new google.maps.InfoWindow();
 
function initialize_map(points, centerpoint) {
var myOptions = {
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
// map_canvas must be a div on your page
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
for(var i=0; i < points.length; i++) {
var marker = new google.maps.Marker({
position: points[i],
map: map
});
}
infowindow.setPosition(centerpoint);
infowindow.open(map);
}
 
# ---- a view in your app that pulls a list of locations possibly stored in a db
<% content_for(:javascript) do %>
locations = [];
<% @locations.each do |loc|%>
locations.push(new google.maps.LatLng(<%=loc.lat%>, <%=loc.lon%>));
<%end%>
$(function() {
// make second argument to whatever the map should be centered on
initialize_map(locations, locations[0]);
});
<%end%>

Thanks for the gist. Unfortunately, I've been unable to get this to work. I'm seeing that my app is loading the google maps .js file but no map is displayed. The last thing my pages loads is js from AuthenicationService.Authenticate that happens 4 seconds after everything else.
Any help would be greatly appreciated.
Thanks..

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.