Created
July 19, 2013 21:36
-
-
Save malandrina/6042541 to your computer and use it in GitHub Desktop.
http://diveintohtml5.info/geolocation.html#the-code, https://developers.google.com/maps/documentation/geocoding/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Geocoder | |
queryGeocodingApi: (options) -> | |
if options.coordinates | |
formattedLatlng = "#{options.coordinates.latitude},#{options.coordinates.longitude}" | |
@.reverseGeocode(latlng: formattedLatlng) | |
reverseGeocode: (options) -> | |
$.ajax | |
type: 'GET' | |
url: 'https://maps.googleapis.com/maps/api/geocode/json' | |
data: { latlng: options.latlng, sensor: false } | |
error: (geocodingApiResponse) -> | |
return | |
success: (geocodingApiResponse) -> | |
current_address = geocodingApiResponse.results[0].formatted_address | |
$('#search_search').val(current_address) | |
class Location | |
getAddress: (geoposition) -> | |
geocoder = new Geocoder | |
latitude = geoposition.coords.latitude | |
longitude = geoposition.coords.longitude | |
geocoder.queryGeocodingApi(coordinates: { latitude, longitude } ) | |
handleGeolocationFailure: (geoposition) -> | |
return | |
$.fn.extend | |
user_location: -> | |
currentLocation = new Location | |
navigator.geolocation.getCurrentPosition( | |
currentLocation.getAddress, | |
currentLocation.handleGeolocationFailure | |
) | |
$ -> | |
$('#search_search').user_location() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment