geokit in social apps

36
Geokit in social apps

Upload: paul-jensen

Post on 10-May-2015

2.257 views

Category:

Technology


0 download

DESCRIPTION

A presentation on the Geokit rubygem and rails plugin, given at April's LRUG meetup

TRANSCRIPT

Page 1: Geokit In Social Apps

Geokitinsocialapps

Page 2: Geokit In Social Apps

WhatisGeokit?

๏ ARubyGemandRailsplugin

๏ Awrappertovariousgeocodingproviders

๏ Supportsgeocodingaddresses,lat/lngcoordinates,andIPAddresses

Page 3: Geokit In Social Apps

WhatisGeokit?

๏ Providesdistance,heading,andmidpointcalcula>onsbetweentwopoints

๏ Providesrectangularboundscalcula>on;revealifapointiswithinbounds

Page 4: Geokit In Social Apps

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

ThegutsofthegemGeocoders

GeocodeError < StandardError

Geocoder

CaGeocoder

UsGeocoder

YahooGeocoder

GeonamesGeocoder

GoogleGeocoder

GeoPluginGeocoder

IpGeocoder

MultiGeocoder

Page 6: Geokit In Social Apps

Thegutsofthegem

Mappable

ClassMethods

LatLng

GeoLoc

Bounds

Page 7: Geokit In Social Apps

Geocodinganaddress

sudo gem install geokit

irb

> require ‘rubygems’

> require ‘geokit’

Page 8: Geokit In Social Apps

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

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

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

Railspluginmagic

• Therailsplugincanautoma>callygeocodenewAc>veRecordrecordsuponcrea>on

• Itcangeocodeyouripaddressinthecontrollerandstoretheresultinthesession.

Page 12: Geokit In Social Apps

Hippo

Page 13: Geokit In Social Apps

Hippo

• Arailsapplica>onbuiltusingMichaelBleigh’sexcellentTwiHerAuthrailstemplate.

• Let’sgeocodethelogged‐inuser’sloca>onuponthemloggingintotheapplica>on

Page 14: Geokit In Social Apps

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

Ge?ngaloca@onfromanipaddress

•Maybeyourusersdon’tlogintoyourapplica>on,orprovideanaddresstogeocode,whatdoyoudothen?

Page 16: Geokit In Social Apps

Ge?ngaloca@onfromanipaddress

• Youcanusea1linemethodinyourcontrollertogeocodeyouruser’sipaddressandgetacity‐levelaccurateloca>onforyouruser.

Page 17: Geokit In Social Apps

Ge?ngaloca@onfromanipaddress

class StaticController < ApplicationController geocode_ip_addressend

Page 18: Geokit In Social Apps

SoyouknowwhereIam...

• ...butmaybethat’snotwhatyourapplica>oncaresabout.

•Whatifyouhaveanapplica>onthathaslotsofrecordswithaddressesaHachedtothem,andyou’dliketomakethoserecordssearchablebytheirloca>on?

Page 19: Geokit In Social Apps

Ac@veRecordfinders

•Geokitgivesyouacollec>onofAc>veRecord‐stylefindersthatcan:

• auto‐geocodetheinputloca>on

• findrecordswithinagivenrange

• orderresultsbytheirdistancefromtheaddress

Page 20: Geokit In Social Apps

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

WhatdoesGeokitdo?

๏ Providesdistance,heading,andmidpointcalcula>onsbetweentwopoints

๏ Providesrectangularboundscalcula>on;revealifapointiswithinbounds

Page 22: Geokit In Social Apps

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

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

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

HeadingCalcula@on

>london.heading_from( new_york)

=> 51.254276626762

>london.heading_to( new_york)

=> 288.368616560178

Page 26: Geokit In Social Apps

MidpointCalcula@on

> london.midpoint_to(new_york)

=> #<Geokit::LatLng:0x1789960 @lat=52.3833438845311, @lng=-41.2615007683062>

Page 27: Geokit In Social Apps

EndpointCalcula@on

> london.endpoint(340, 50)

=> #<Geokit::LatLng:0x18385f0 @lat=52.1787256392731, @lng=-0.5294069905067>

Page 28: Geokit In Social Apps

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

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

WithinBounds

> bounds.contains?(midlands)

=> true

> bounds.center

=> #<Geokit::LatLng:0x1708090 @lat=52.6051976419128, @lng=-1.49169672003523>

Page 31: Geokit In Social Apps

Limita@ons

•Geokit’sRailsplugindoesnotworkwith:

• SQLite

• PostgreSQL(versionsbelow8.1)

Page 32: Geokit In Social Apps

Ruby1.9?

Page 33: Geokit In Social Apps

YES√

Page 34: Geokit In Social Apps

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

Ausefulresource

• BeginningGoogleMapsApplica>onswithRailsandAjax:FromNovicetoProfessional

• Part‐wriHenbyAndreLewis(co‐authorofGeokit)

• Containsawealthofinforma>onaboutgeocoding

Page 36: Geokit In Social Apps

Thanks

PaulJensenNewBamboo

paul@new‐bamboo.co.uk@paulbjensen