2014年4月27日 星期日

rails geocode

http://eureka.ykyuen.info/2011/04/08/rails-implementation-of-geocoder-on-model/
1. Add latitudelongitude and address to the User model
  • rails generate migration AddLatitudeAndLongitudeAndAddressToUser latitude:float longitude:float address:string

2. Run the migration
  • rake db:migrate

3. Add the following lines in app/model/user.rb

1
2
geocoded_by :address
after_validation :geocode
4. Try how Geocoder works in Rails Console
1
2
3
4
5
6
7
8
9
10
11
# Get the first user
user = User.first
# Set the user address field
user.address = "Hong Kong"
# Generate the user latitude and longitude
user.geocode
# Check the user latitude and longitude
user.latitude.to_s + " " + user.longitude.to_s



http://www.synbioz.com/blog/search_by_location_with_geocoder



location_info = request.location

@locations = Location.near([location_info.latitude, location_info.longitude], radius_distance_you_want_to_include)




沒有留言:

張貼留言