     @view.setCenter(ol.proj.transform([longitude, latitude], "EPSG:4326", "EPSG:3857"))
     @view.setZoom(zoom)
 
+  locationSuccess: (position) =>
+    @zoomTo(position.coords.latitude, position.coords.longitude)
+    @setupPicker(position.coords.latitude, position.coords.longitude)
+
+  locationFail: (err) =>
+    @setupPicker(0,0)
+
   setLocationToGeolocation: ->
     if navigator.geolocation
-      navigator.geolocation.getCurrentPosition (position) =>
-        @zoomTo(position.coords.latitude, position.coords.longitude)
-        @setupPicker(position.coords.latitude, position.coords.longitude) if $(@mapElement).data('map-picker')
+      navigator.geolocation.getCurrentPosition @locationSuccess, @locationFail
     else
-      @setupPicker(0,0) if $(@mapElement).data('map-picker')
+      @locationFail()
 
   constructor: (@mapElement) ->
     @view = new ol.View(

class Map

  setupPicker: (latitude, longitude) ->

    @pin = new ol.Overlay(
      position: ol.proj.transform([longitude,latitude], 'EPSG:4326', 'EPSG:3857')
      element: $('.pin')[0]
    )

    @map.addOverlay(@pin)

    @setForm(latitude, longitude)

    @map.on "click", (evt) =>
      console.log evt
      coordinate = evt.coordinate
      @pin.setPosition(evt.coordinate)
      latlng = ol.proj.transform(coordinate, 'EPSG:3857', 'EPSG:4326');
      @setForm(latlng[0], latlng[1])

  setForm: (latitude, longitude) ->
    $('#event_longitude').val latitude

      latitude = parseFloat(latitude, 10)
      longitude = parseFloat(longitude, 10)
      @zoomTo(latitude, longitude, 14)
      @setupPicker(latitude, longitude)
    else
      @setLocationToGeolocation()

