Skip to content

Instantly share code, notes, and snippets.

@ptn
Created July 23, 2012 00:34
Show Gist options
  • Save ptn/3161485 to your computer and use it in GitHub Desktop.
Save ptn/3161485 to your computer and use it in GitHub Desktop.
$(document).ready ->
if $("#map").length > 0
class MapManager
constructor: ->
@defaultLat = -12.043333
@defaultLng = -77.028333
@latEl = $("#experience_latitude")
@lngEl = $("#experience_longitude")
coords = @initialCoords()
@map = new GMaps
div: "#map"
lat: coords.lat
lng: coords.lng
zoom: 15
@map.addMarker
lat: coords.lat
lng: coords.lng
@bindForm()
bindForm: ->
$("#geocoding a.button").click (e) =>
e.preventDefault()
GMaps.geocode
address: $('#city').val().trim()
callback: (results, status) =>
if status == 'OK'
latlng = results[0].geometry.location
@moveMap(latlng.lat(), latlng.lng())
@latEl.val(latlng.lng())
@lngEl.val(latlng.lat())
initialCoords: ->
coords = {}
lat = @latEl.val()
lat = @defaultLat unless lat
coords.lat = lat
lng = @lngEl.val()
lng = @defaultLng unless lng
coords.lng = lng
coords
moveMap: (lat, lng) ->
@map.setCenter(lat, lng)
@map.removeMarkers
@map.addMarker
lat: lat
lng: lng
map = new MapManager
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment