Pregunta

map.js.coffee

jQuery ->
  getLocation = ->
    if navigator.geolocation
      navigator.geolocation.getCurrentPosition userPosition showError options
    else
      alert("Geolocation is not supported by this browser.")

  handler = Gmaps.build 'Google'
  handler.buildMap 
    provider: {}
    internal: {id: 'map'}
  , ->
    handler.panTo latlng

geolocation.js.coffee

userPosition = (position) ->
  latlng = new google.maps.LatLng(position.coords.latitude + "," + position.coords.longitude)
  alert(latlng)

showError = (error) ->
  switch error.code
    when error.PERMISSION_DENIED
      alert("User denied the request for Geolocation.")
    when error.POSITION_UNAVAILABLE
      alert("Location information is unavailable.")
    when error.TIMEOUT
      alert("The request to get user location timed out.")
    when error.UNKNOWN_ERROR
      alert("An unknown error occurred.")

options =
  enableHighAccuracy: false
  timeout: 5000

I feel like maybe I don't have a grasp on global variables? I tried moving the getLocation call all over the place...

I tried putting it all in the same file...

I tried moving handler above (outside) of the jQuery ready function... that gave a different error about _ not being defined.

I threw the alert(latlng) in there just to see if it was even hitting that function... it's not.

I'm trying out the new gmaps4rails gem and struggling a bit. I checked out the info over at W3schools too, which this code is pretty close to.

¿Fue útil?

Solución

Dont understand your code but here is the idea:

handler = Gmaps.build 'Google'
handler.buildMap { internal: {id: 'map'} }, ->

  positionSuccess = (position) ->
    handler.map.centerOn
      lat: position.coords.latitude
      lng: position.coords.longitude

  positionError = ->

  navigator.geolocation.getCurrentPosition(positionSuccess, positionError)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top