geokit in social apps
DESCRIPTION
A presentation on the Geokit rubygem and rails plugin, given at April's LRUG meetupTRANSCRIPT
![Page 1: Geokit In Social Apps](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f5d54b4c9058a148b4573/html5/thumbnails/1.jpg)
Geokitinsocialapps
![Page 2: Geokit In Social Apps](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f5d54b4c9058a148b4573/html5/thumbnails/2.jpg)
WhatisGeokit?
๏ ARubyGemandRailsplugin
๏ Awrappertovariousgeocodingproviders
๏ Supportsgeocodingaddresses,lat/lngcoordinates,andIPAddresses
![Page 3: Geokit In Social Apps](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f5d54b4c9058a148b4573/html5/thumbnails/3.jpg)
WhatisGeokit?
๏ Providesdistance,heading,andmidpointcalcula>onsbetweentwopoints
๏ Providesrectangularboundscalcula>on;revealifapointiswithinbounds
![Page 4: Geokit In Social Apps](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f5d54b4c9058a148b4573/html5/thumbnails/4.jpg)
ThegutsofthegemGeokit
Mappable
Inflector
Geocoders
Contains class and instance methods providing distance
calculation services
Contains customised instance methods to extend the Inflector
module
Contains class methods wrapping geocoding services
![Page 5: Geokit In Social Apps](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f5d54b4c9058a148b4573/html5/thumbnails/5.jpg)
ThegutsofthegemGeocoders
GeocodeError < StandardError
Geocoder
CaGeocoder
UsGeocoder
YahooGeocoder
GeonamesGeocoder
GoogleGeocoder
GeoPluginGeocoder
IpGeocoder
MultiGeocoder
![Page 6: Geokit In Social Apps](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f5d54b4c9058a148b4573/html5/thumbnails/6.jpg)
Thegutsofthegem
Mappable
ClassMethods
LatLng
GeoLoc
Bounds
![Page 7: Geokit In Social Apps](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f5d54b4c9058a148b4573/html5/thumbnails/7.jpg)
Geocodinganaddress
sudo gem install geokit
irb
> require ‘rubygems’
> require ‘geokit’
![Page 8: Geokit In Social Apps](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f5d54b4c9058a148b4573/html5/thumbnails/8.jpg)
Geocodinganaddress> a=Geokit::Geocoders::YahooGeocoder.geocode('45 Balls Pond Road, London, UK')
=> <Geokit::GeoLoc:0x19105cc @all=[#<Geokit::GeoLoc:0x19105cc ...>], @lat=51.546004, @precision="address", @state="United Kingdom", @success=true, @city="Hoxton, N1 4", @country_code="GB", @provider="yahoo", @street_address="45 Balls Pond Road", @lng=-0.077033, @full_address=nil, @zip=nil>
> a.ll
=> "51.546004,-0.077033"
![Page 9: Geokit In Social Apps](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f5d54b4c9058a148b4573/html5/thumbnails/9.jpg)
ReverseGeocoding> b= Geokit::Geocoders::GoogleGeocoder.geocode(a.ll)
(output skipped)
> b.all.map{|obj| puts obj.full_address}
=> 28-30 Bentley Rd, Hackney, Greater London, UKIslington, Greater London N1 4, UKHackney, Greater London, UKHackney, Greater London, UKGreater London, UKEngland, United KingdomUnited Kingdom
![Page 10: Geokit In Social Apps](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f5d54b4c9058a148b4573/html5/thumbnails/10.jpg)
IPAddressGeocoding
> cia_dot_gov = Geokit::Geocoders::GeoPluginGeocoder.geocode('198.81.129.100')
=> #<Geokit::GeoLoc:0x18160e0 @all=[#<Geokit::GeoLoc:0x18160e0 ...>], @lat=38.957901, @precision="unknown", @state="VA", @success=true, @city="Reston", @country_code="US", @provider="geoPlugin", @street_address=nil, @lng=-77.343903, @full_address=nil, @zip=nil>
![Page 11: Geokit In Social Apps](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f5d54b4c9058a148b4573/html5/thumbnails/11.jpg)
Railspluginmagic
• Therailsplugincanautoma>callygeocodenewAc>veRecordrecordsuponcrea>on
• Itcangeocodeyouripaddressinthecontrollerandstoretheresultinthesession.
![Page 12: Geokit In Social Apps](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f5d54b4c9058a148b4573/html5/thumbnails/12.jpg)
Hippo
![Page 13: Geokit In Social Apps](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f5d54b4c9058a148b4573/html5/thumbnails/13.jpg)
Hippo
• Arailsapplica>onbuiltusingMichaelBleigh’sexcellentTwiHerAuthrailstemplate.
• Let’sgeocodethelogged‐inuser’sloca>onuponthemloggingintotheapplica>on
![Page 14: Geokit In Social Apps](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f5d54b4c9058a148b4573/html5/thumbnails/14.jpg)
Auto‐geocodemodel
class User < TwitterAuth::GenericUser acts_as_mappable :auto_geocode => {:field => :location} before_validation_on_update :auto_geocode_address, :if => :location_changed?end
![Page 15: Geokit In Social Apps](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f5d54b4c9058a148b4573/html5/thumbnails/15.jpg)
Ge?ngaloca@onfromanipaddress
•Maybeyourusersdon’tlogintoyourapplica>on,orprovideanaddresstogeocode,whatdoyoudothen?
![Page 16: Geokit In Social Apps](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f5d54b4c9058a148b4573/html5/thumbnails/16.jpg)
Ge?ngaloca@onfromanipaddress
• Youcanusea1linemethodinyourcontrollertogeocodeyouruser’sipaddressandgetacity‐levelaccurateloca>onforyouruser.
![Page 17: Geokit In Social Apps](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f5d54b4c9058a148b4573/html5/thumbnails/17.jpg)
Ge?ngaloca@onfromanipaddress
class StaticController < ApplicationController geocode_ip_addressend
![Page 18: Geokit In Social Apps](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f5d54b4c9058a148b4573/html5/thumbnails/18.jpg)
SoyouknowwhereIam...
• ...butmaybethat’snotwhatyourapplica>oncaresabout.
•Whatifyouhaveanapplica>onthathaslotsofrecordswithaddressesaHachedtothem,andyou’dliketomakethoserecordssearchablebytheirloca>on?
![Page 19: Geokit In Social Apps](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f5d54b4c9058a148b4573/html5/thumbnails/19.jpg)
Ac@veRecordfinders
•Geokitgivesyouacollec>onofAc>veRecord‐stylefindersthatcan:
• auto‐geocodetheinputloca>on
• findrecordswithinagivenrange
• orderresultsbytheirdistancefromtheaddress
![Page 20: Geokit In Social Apps](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f5d54b4c9058a148b4573/html5/thumbnails/20.jpg)
Ac@veRecordfinders
class CafeController < ApplicationController def search @query = params[:query] @range = params[:range]
Cafe.find_within(@range, :origin => @query, :order => ‘distance’)
end
end
![Page 21: Geokit In Social Apps](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f5d54b4c9058a148b4573/html5/thumbnails/21.jpg)
WhatdoesGeokitdo?
๏ Providesdistance,heading,andmidpointcalcula>onsbetweentwopoints
๏ Providesrectangularboundscalcula>on;revealifapointiswithinbounds
![Page 22: Geokit In Social Apps](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f5d54b4c9058a148b4573/html5/thumbnails/22.jpg)
DistanceCalcula@on
> london = Geokit::Geocoders::GoogleGeocoder.geocode('London')
=> #<Geokit::GeoLoc:0x17d1c88 @all=[#<Geokit::GeoLoc:0x17d1c88 ...>], @lat=51.5001524, @precision="city", @state="Greater London", @success=true, @city="London", @country_code="GB", @provider="google", @street_address=nil, @lng=-0.1262362, @full_address="Westminster, London, UK", @zip=nil>
![Page 23: Geokit In Social Apps](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f5d54b4c9058a148b4573/html5/thumbnails/23.jpg)
DistanceCalcula@on
> new_york = Geokit::Geocoders::GoogleGeocoder.geocode('New York')
=> #<Geokit::GeoLoc:0x182d510 @all=[#<Geokit::GeoLoc:0x182d510 ...>], @lat=40.756054, @precision="city", @state="NY", @success=true, @city="New York", @country_code="US", @provider="google", @street_address=nil, @lng=-73.986951, @full_address="New York, NY, USA", @zip=nil>
![Page 24: Geokit In Social Apps](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f5d54b4c9058a148b4573/html5/thumbnails/24.jpg)
DistanceCalcula@on
>london.distance_from( new_york, :units => :miles)
=> 3462.62226994318
>london.distance_from( new_york, :formula => :flat)
=> 2748.37417609529
![Page 25: Geokit In Social Apps](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f5d54b4c9058a148b4573/html5/thumbnails/25.jpg)
HeadingCalcula@on
>london.heading_from( new_york)
=> 51.254276626762
>london.heading_to( new_york)
=> 288.368616560178
![Page 26: Geokit In Social Apps](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f5d54b4c9058a148b4573/html5/thumbnails/26.jpg)
MidpointCalcula@on
> london.midpoint_to(new_york)
=> #<Geokit::LatLng:0x1789960 @lat=52.3833438845311, @lng=-41.2615007683062>
![Page 27: Geokit In Social Apps](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f5d54b4c9058a148b4573/html5/thumbnails/27.jpg)
EndpointCalcula@on
> london.endpoint(340, 50)
=> #<Geokit::LatLng:0x18385f0 @lat=52.1787256392731, @lng=-0.5294069905067>
![Page 28: Geokit In Social Apps](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f5d54b4c9058a148b4573/html5/thumbnails/28.jpg)
WithinBounds
> south_west = Geokit::Geocoders::GoogleGeocoder.geocode('Bristol, UK')
(output skipped)
> north_east = Geokit::Geocoders::GoogleGeocoder.geocode('Hull, UK')
(output skipped)
![Page 29: Geokit In Social Apps](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f5d54b4c9058a148b4573/html5/thumbnails/29.jpg)
WithinBounds
> bounds = Geokit::Bounds.new(south_west, north_east)
(output skipped)
> midlands = Geokit::Geocoders::GoogleGeocoder.geocode('Nottingham, UK')
(output skipped)
![Page 30: Geokit In Social Apps](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f5d54b4c9058a148b4573/html5/thumbnails/30.jpg)
WithinBounds
> bounds.contains?(midlands)
=> true
> bounds.center
=> #<Geokit::LatLng:0x1708090 @lat=52.6051976419128, @lng=-1.49169672003523>
![Page 31: Geokit In Social Apps](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f5d54b4c9058a148b4573/html5/thumbnails/31.jpg)
Limita@ons
•Geokit’sRailsplugindoesnotworkwith:
• SQLite
• PostgreSQL(versionsbelow8.1)
![Page 32: Geokit In Social Apps](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f5d54b4c9058a148b4573/html5/thumbnails/32.jpg)
Ruby1.9?
![Page 33: Geokit In Social Apps](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f5d54b4c9058a148b4573/html5/thumbnails/33.jpg)
YES√
![Page 34: Geokit In Social Apps](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f5d54b4c9058a148b4573/html5/thumbnails/34.jpg)
Alterna@ves
•Gra>cule(hHp://gra>cule.rubyforge.org)
• YM4R(hHp://ym4r.rubyforge.org)
• Geocoder(hHp://geocoder.rubyforge.org/)
![Page 35: Geokit In Social Apps](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f5d54b4c9058a148b4573/html5/thumbnails/35.jpg)
Ausefulresource
• BeginningGoogleMapsApplica>onswithRailsandAjax:FromNovicetoProfessional
• Part‐wriHenbyAndreLewis(co‐authorofGeokit)
• Containsawealthofinforma>onaboutgeocoding
![Page 36: Geokit In Social Apps](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f5d54b4c9058a148b4573/html5/thumbnails/36.jpg)
Thanks
PaulJensenNewBamboo
paul@new‐bamboo.co.uk@paulbjensen