Skip to content

Instantly share code, notes, and snippets.

@malandrina
Created July 19, 2013 21:36
Show Gist options
  • Save malandrina/6042541 to your computer and use it in GitHub Desktop.
Save malandrina/6042541 to your computer and use it in GitHub Desktop.
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