Created
June 9, 2011 18:55
-
-
Save schowdhury/1017434 to your computer and use it in GitHub Desktop.
Get google maps integrated with your app -minimal code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#------ 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%> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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..