Skip to content

Commit

Permalink
Fixes #3
Browse files Browse the repository at this point in the history
  • Loading branch information
juanjcsr committed Feb 10, 2014
2 parents 4537de5 + 1724a50 commit 1e3e540
Showing 1 changed file with 72 additions and 62 deletions.
134 changes: 72 additions & 62 deletions app/assets/javascripts/places.js.coffee
Original file line number Diff line number Diff line change
@@ -1,15 +1,45 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
# Script para controlar el comportamiento de los mapas
# y la ubicación de los puntos.

unless navigator.geolocation then console.log("No geoloc")

success = (position) ->
## Init de variables "globales" para los mapas
# TODO: ¿Es posible NO utilizar el objeto window
# para salir del closure en Coffeescript y poder
# implementar Google Maps de forma más limpia?
######
infowindow = new google.maps.InfoWindow({
maxWidth: 300
})
window.markers = []
window.infowindows = []
window.current_points = []
######

#jQuery document.ready
#
$ ->
console.log "poss"
window.map = gm_init()
load_all_points(map)
google.maps.event.addListener(map, 'dragend', moviendo_mapa)
navigator.geolocation.getCurrentPosition(loc_success, loc_error)
submit_ajax_form()
get_current_location()

# Google Maps init script.
# Mapa localizado en el Zócalo de la Ciudad de México
gm_init = ->
gm_center = new google.maps.LatLng(19.49, -99.2033)
gm_map_type = google.maps.MapTypeId.ROADMAP
map_options = { center: gm_center, zoom: 14, mapTypeId: gm_map_type }
new google.maps.Map(@map_canvas, map_options)

#### Browser Geolocation methods
loc_success = (position) ->
console.log "position disponible"
latitude = position.coords.latitude
longitude = position.coords.longitude
#@map = gm_init(latitude, longitude)
#load_points(@map

center = new google.maps.LatLng(latitude,longitude)
console.log(latitude, longitude)
Expand All @@ -20,68 +50,45 @@ success = (position) ->
icon: {
url: 'arrows.png'
}
delete_markers(window.map)
# Clean map
clean_map(window.map)
load_from_position(window.map, latitude, longitude)


error = (err) ->
loc_error = (err) ->
console.log "no position disponible"


#### /Browser Geolocation Methods

gm_init = ->
gm_center = new google.maps.LatLng(19.49, -99.2033)
gm_map_type = google.maps.MapTypeId.ROADMAP
map_options = { center: gm_center, zoom: 14, mapTypeId: gm_map_type }
new google.maps.Map(@map_canvas, map_options)

#gm_center = (map, lat, lon) ->


infowindow = new google.maps.InfoWindow({
maxWidth: 300
})
window.markers = []
window.infowindows = []
window.current_points = []

$ ->
console.log "poss"
window.map = gm_init()
load_points(map)
google.maps.event.addListener(map, 'dragend', moviendo_mapa)
navigator.geolocation.getCurrentPosition(success,error)
ajaxtrue()
getLocationNow()
#

load_from_position = (map, lat, lon) ->
callback = (data) -> display_on_map(data, map)
$.get '/lugares', {latitude: lat, longitude: lon}, callback, 'json'


load_points = (map) ->
# '/hospitales' es un endpoint que trae todos los puntos
# en caso de que no se obtenga una posición mediante el browser
load_all_points = (map) ->
callback = (data) -> display_on_map(data, map)
$.get '/hospitales.json', {}, callback, 'json'

# Obtiene puntos para desplegar en el mapa a partir
# de un endpoint. en éste caso '/lugares', sin embargo, éste
# puede cambiar dependiendo las necesidades del end-user.
load_from_position = (map, lat, lon) ->
callback_to_map = (data) -> display_on_map(data, map)
$.get '/lugares', {latitude: lat, longitude: lon}, callback_to_map, 'json'

set_marker_map = (map) ->
#indow.center_marker.setMap(map)
for marker in window.markers
console.log("marcador")
marker.setMap(map)

# A partir de un JSON con array de objetos, genera un nuevo
# marker y adjuntar la referencia de window.map
display_on_map = (data, map) ->
#console.log(map)
#console.log(data.centros[0])
set_marker_map(null)
for centro in data.centros
create_marker(centro, map)
set_marker_map(map)
console.log "total marcadores " + window.markers.length
create_cards()

create_marker = (point, map) ->
set_marker_map = (map) ->
for marker in window.markers
console.log("marcador")
marker.setMap(map)

create_marker = (point, map) ->
content_string = '<div class="centro_info">' +
'<div class="centro_encabezado">' +
'<h3>' + point.nombre + '</h3>' +
Expand Down Expand Up @@ -112,14 +119,6 @@ create_marker = (point, map) ->
)


delete_markers = (map)->
console.log("borrando")
console.log (window.markers)
set_marker_map(null)
window.markers = []
window.infowindows = []
window.current_points = []

moviendo_mapa = ->
centro_pos = window.map.getCenter()
distancia_centro = google.maps.geometry.spherical.computeDistanceBetween(centro_pos, window.center_marker.getPosition());
Expand All @@ -128,7 +127,7 @@ moviendo_mapa = ->
#console.log window.markers
create_cards()
if distancia_centro > 3000
delete_markers(window.map)
clean_map(window.map)
window.center_marker.setMap(null)
window.center_marker = null
load_from_position(window.map, centro_pos.lat(), centro_pos.lng())
Expand All @@ -139,6 +138,16 @@ moviendo_mapa = ->
url: 'arrows.png'
}

clean_map = (map)->
console.log("borrando")
console.log (window.markers)
set_marker_map(null)
window.markers = []
window.infowindows = []
window.current_points = []



create_cards = ->
$("#tarjetas div").remove()
$("#tarjetas").append(card(point)) for point in window.current_points
Expand All @@ -156,13 +165,14 @@ card = (point) ->
telefonos_centro +
hacer_cita + "</div>"

ajaxtrue = ->

submit_ajax_form = ->
$("#forma_busqueda").bind('ajax:success', (evt, data, status, xhr) ->
delete_markers(window.map)
clean_map(window.map)
display_on_map(data, map)
)

getLocationNow = ->
get_current_location = ->
mostrar_todos = ->
console.log "wooo"
navigator.geolocation.getCurrentPosition(success,error)
Expand Down

0 comments on commit 1e3e540

Please sign in to comment.