Hi. I've been trying to play with this for a while now but I haven't seemed to get it to work. I have a admin controller, which is scaffolded, to input the lat and long along with info. I have a restaurant controller that will display the map with the points. I can get the map to show but the points do not show.
Code: ( text )
class CreateRestaurants < ActiveRecord::Migration
def self.up
create_table :restaurants do |t|
t.column :category, :string
t.column :name, :string
t.column :address, :string
t.column :phone, :string
t.column :hours, :text
t.column :more_info, :text
t.column :latitude, :float
t.column :longitude, :float
end
end
def self.down
drop_table :restaurants
end
end
class RestaurantController < ApplicationController
def map
@restaurants = Restaurant.find :all
end
end
--index.rhtml--
<div id="map" style="width: 500px; height: 300px"></div>
--restaurant.rhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Restaurants: <%= controller.action_name %></title>
<%= stylesheet_link_tag 'scaffold' %>
<%= javascript_include_tag 'map' %>
<script src="http://maps.google.com/maps?file=api&v=2&key=KEY"
type="text/javascript"></script>
<script type="text/javascript">var restaurants=<%= @restaurants.to_json %>;</script>
</head>
<body>
<p style="color: green"><%= flash[:notice] %></p>
<%= yield %>
</body>
</html>
--map.js--
var map;
var centerLatitude = 42.733868932421856;
var centerLongitude = -84.47994947433472;
var startZoom = 16;
var markerHash = {};
var currentFocus = false;
function addMarker(latitude, longitude, id) {
var marker = new GMarker(new GLatLng(latitude, longitude));
GEvent.addListener(marker, 'click',
function() {
//marker.openInfoWindowHtml(description);
focusPoint(id);
});
map.addOverlay(marker);
return marker;
}
function init() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map"));
map.addControl(new GSmallMapControl());
map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
for (var i=0; i<markers.length; i++) {
//addMarker(restaurants[i].latitude, restaurants[i].longitude, restaurants[i].name);
var current = markers[i];
marker = addMarker(current.latitude, current.longitude, current.id);
markerHash[current.id]={marker:marker, address:current.address, visible:true};
}
}
}
window.onload = init;
window.onunload = GUnload;