Sei sulla pagina 1di 32

Google Maps

& Rails
Why Google Maps?
Why Google Maps?

• Rich User Interface


Why Google Maps?

• Rich User Interface


• Well documented Javascript API
Why Google Maps?

• Rich User Interface


• Well documented Javascript API
• Free
But...
But...

• I want it to be a bit simpler and faster to work with


Plugins To The Rescue
Plugins To The Rescue

• YM4R - The easy way to use google maps


with Rails
YM4R
YM4R

• Simple Methods for creating a map all from


within the controller
YM4R

• Define a new map instance


YM4R

• Define a new map instance


• @map = GMap.new(“map_div”)
YM4R

• Tell it what controls you want


YM4R

• Tell it what controls you want


• @map.control_init(:large_map => true,:map_type => true)

This adds the large


zoom slider and
pan cross + map
type selector, ie
hybrid, satillite
etc.
YM4R

• Tell Gmaps where you want to center it


YM4R

• Tell Gmaps where you want to center it


• @map.center_zoom_init([-28.99425, 132.03845], 4)

• # => Australia
YM4R

• Inject Markers
YM4R

• Inject Markers
@map.overlay_init(GMarker.new([-28.99425, 132.03845],
:title => “Australia”,
:info_window => “Yay!”))
YM4R
def index
@map = GMap.new(“map_div”)
@map.control_init(:large_map => true,:map_type => true)
@map.center_zoom_init([75.5,-42.56],4)
@map.overlay_init(GMarker.new([75.6,-42.467],:title => “Hello”,
:info_window => “Info! Info!”))
end
You can also add multiple markers So I find all my dealers in the DB.
and have YM4R set how the max Create a new GMap instance
number to be shown before it Set controls and the center, then I create
“clumps” them together as 1 marker, a new array and assign it to the variable
and all sorts of stuff. markers. From there I “push” into the
This is the controller code from the array each of the db records
orbea site I created. Then we use YM4R’s clusterer class, where
we tell it the array of markers and the
max-Number of visible markers.

YM4R
def find_a_dealer
@dealers = Dealer.find(:all, :order => “name ASC”)
@map = GMap.new(“map_div”)
@map.control_init(:large_map => true, :map_type => true)
@map.center_zoom_init([-28.99425, 132.03845], 4)
markers = Array.new
@dealers.each do |dealer|
markers << GMarker.new([dealer.lat, dealer.lng],
:info_window => “<strong>#{dealer.name}</strong><br />
#{dealer.street_number}, #{dealer.street_name.capitalize},<br />
#{dealer.suburb.capitalize}, #{dealer.state.upcase}, #{dealer.postcode}<br />
PH: #{dealer.phone_number}, #{dealer.suburb.upcase}, #{dealer.state.upcase}“,
:title => dealer.name)
end
clusterer = Clusterer.new(markers, :max_visible_markers => 15)
@map.overlay_init clusterer
end
YM4R

• script/plugin install svn://rubyforge.org/var/svn/ym4r/


Plugins/GM/trunk/ym4r_gm

• http://thepochisuperstarmegashow.com/projects
Wait
I want more than just that!
Wait
I want more than just that!

I want geolocation, distance finders and other cool stuff!


GeoKit
Provides key functionality for location-oriented Rails applications
GeoKit

• find_within(distance, options={})
• find_beyond(distance, options={})
• find_closest(options={})
• find_farthest(options={})
GeoKit

• IpGeocoder.geocode('12.215.42.19')
• # => @success=true, @street_address=nil, @country_code="US", @zip=nil, @lng=-88.4588,
@state="IL", @city="Sugar Grove", @provider="hostip", @lat=41.7696

• GoogleGeocoder.geocode('281 Clarence St, Sydney, AU’)


• # => @success=true, @street_address="281 Clarence St", @country_code="AU",
@full_address="281 Clarence St, Sydney, New South Wales 2000, Australia", @zip="2000",
@lng=151.205542, @state="New South Wales", @city="Sydney", @provider="google",
@lat=-33.872399
GeoKit
Add lat,lng co-ordinates to a record using before_save:
class Dealer < ActiveRecord::Base
include GeoKit::Geocoders
acts_as_mappable :default_units => :kms

before_save :find_in_google_maps

protected
def find_in_google_maps
location = GoogleGeocoder.geocode(“#{self.street_number} #{self.street_name},
#{self.suburb}, #{self.state},
#{self.postcode}, Australia”)
if location.success
self.lat = location.lat
self.lng = location.lng
else
self.lat = nil
self.lng = nil
end
end

end
GeoKit
You can now find dealers in your controller and add them to the map:
def find_a_dealer
@dealers = Dealer.find(:all, :order => “name ASC”)
@map = GMap.new(“map_div”)
@map.control_init(:large_map => true, :map_type => true)
@map.center_zoom_init([-28.99425, 132.03845], 4)
markers = Array.new
@dealers.each do |dealer|
markers << GMarker.new([dealer.lat, dealer.lng],
:info_window => “<strong>#{dealer.name}</strong><br />
#{dealer.street_number}, #{dealer.street_name.capitalize},<br />
#{dealer.suburb.capitalize}, #{dealer.state.upcase}, #{dealer.postcode}<br />
PH: #{dealer.phone_number}, #{dealer.suburb.upcase}, #{dealer.state.upcase}“,
:title => dealer.name)
end

clusterer = Clusterer.new(markers, :max_visible_markers => 15)


@map.overlay_init clusterer
end
GeoKit
Update with rjs:
CONTROLLER:
def update_dealer_map
unless params[:search].blank?
home = GoogleGeocoder.geocode(params[:search] + “, Australia”)
unless home.success==false
@dealer = Dealer.find_closest(:origin => home)
@map = Variable.new(“map”)
@location = [@dealer.lat, @dealer.lng]
@search = params[:search]
end
end
end

update_dealer_map.rjs:
unless @location.blank?
page << @map.set_center(GLatLng.new(@location), 12)
end
GeoKit
script/plugin install svn://rubyforge.org/var/svn/geokit/trunk

http://geokit.rubyforge.org/
GeoKit
script/plugin install svn://rubyforge.org/var/svn/geokit/trunk

http://geokit.rubyforge.org/

YM4R
script/plugin install svn://rubyforge.org/var/svn/ym4r/Plugins/GM/trunk/ym4r_gm

http://thepochisuperstarmegashow.com/projects

Potrebbero piacerti anche