Skip to content

Instantly share code, notes, and snippets.

@dux
Created September 30, 2016 15:16
Show Gist options
  • Save dux/815c02620966c28a6acfcb9f60d87d85 to your computer and use it in GitHub Desktop.
Save dux/815c02620966c28a6acfcb9f60d87d85 to your computer and use it in GitHub Desktop.
# 1. fill markers/locations array
# 2. pass objects and draw them
# @markers = Model.all.inject([]) { |list, el| list.push({ id:el.id, lat:el.latitude, lng:el.longitude, name:el.name, city:el.city, path:el.admin_path }); list }
# ---
# var markers = #{@markers.to_json};
# GoogleMap.init('map2', { lat: 50.9188435, lng: 9.3206294, zoom: 6 });
# GoogleMap.add_markers(markers, function(o) {
# return '<div><b><a href="'+o.path+'">'+o.name+'</a></b><br /><br />'+o.city+'</div>'
# });
@GoogleMap =
init: (node_id, opts={}) ->
# prevent double init
return if @map
# initMap is functon triggrered by google maps JS
window.initMap = ->
unless node = document.getElementById(node_id)
alert "Google map node ##{node_id} not found"
return
GoogleMap.info_window = new google.maps.InfoWindow();
GoogleMap.map = new google.maps.Map node,
center: new google.maps.LatLng(opts.lat, opts.lng)
zoom: opts['zoom'] || 6
# get location objects, onclick render modal based on that location object
add_markers: (markers, click_func) ->
unless GoogleMap.map
setTimeout ->
GoogleMap.add_markers(markers, click_func)
, 100
return false
# give map some time to init
setTimeout ->
for el in markers
marker = new google.maps.Marker
position: new google.maps.LatLng(el.lat, el.lng)
map: GoogleMap.map
marker.data = el
marker.addListener 'click', ->
GoogleMap.info_window.setContent click_func @data
GoogleMap.info_window.open GoogleMap.map, @
, 200
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment