forked from singlow/meteor-live-maps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlive-maps-client.coffee
47 lines (40 loc) · 1.38 KB
/
live-maps-client.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
LiveMaps =
addMarkersToMap: (map, cursors)->
if not Array.isArray(cursors)
cursors = [cursors]
queries = (liveMarkers(map, cursor) for cursor in cursors)
# return method to destroy them when you are done
return stop: -> do stopQuery for stopQuery in queries
liveMarkers = (map, cursor)->
markers = []
if cursor.observe
transform = (doc)->
position: new google.maps.LatLng(doc.latitude or doc.lat or doc.location[1], doc.longitude or doc.lon or doc.lng or doc.location[0])
title: doc.title or doc.name or doc.label
animation: doc.animation or google.maps.Animation.DROP
icon: doc.icon or '//maps.google.com/mapfiles/ms/icons/green-dot.png'
else
transform = cursor.transform
onClick = cursor.onClick
cursor = cursor.cursor
addMarker = (doc)->
options = transform(doc)
options.map = map unless options.map
marker = new google.maps.Marker options
marker.id = doc._id
google.maps.event.addListener marker, 'click', onClick
markers[doc._id] = marker
removeMarker = (doc)->
markers[doc._id].setMap(null)
delete markers[doc._id]
liveQuery = cursor.observe
added: addMarker
changed: (newDoc, oldDoc)->
removeMarker(oldDoc)
addMarker(newDoc)
removed: removeMarker
return ->
do liveQuery.stop
marker.setMap(null) for marker in markers
unless Package?
@LiveMaps = LiveMaps