Skip to content

Instantly share code, notes, and snippets.

@wilg
Created July 22, 2012 19:12
Show Gist options
  • Save wilg/3160745 to your computer and use it in GitHub Desktop.
Save wilg/3160745 to your computer and use it in GitHub Desktop.
jQuery ->
return unless $("#module-location-map").length > 0
window.overlays = []
# Setup Map
options = {
center: new google.maps.LatLng(34.1626653, -118.2518423),
zoom: 3,
mapTypeId: "Watercolor",
panControl: true,
panControlOptions: {
position: google.maps.ControlPosition.TOP_RIGHT
},
zoomControl: true,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.LARGE,
position: google.maps.ControlPosition.TOP_RIGHT
},
# panControl: false,
streetViewControl: false,
mapTypeControlOptions: {
mapTypeIds: []
}
}
map = new google.maps.Map(document.getElementById("module-location-map"), options)
# Map Type
map.mapTypes.set("Toner", new google.maps.StamenMapType("toner"))
map.mapTypes.set("Terrain", new google.maps.StamenMapType("terrain"))
map.mapTypes.set("Watercolor", new google.maps.StamenMapType("watercolor"))
$('a[target$="_blank"]').hide()
updateMarkers(map)
class Rect
constructor: (@left, @right, @top, @bottom) ->
intersects:(r2) ->
not (r2.left > @right or r2.right < @left or r2.top > @bottom or r2.bottom < @top)
window.coalesceOverlays = ->
# Find and map overlaps
overlays = window.overlays
overlaysHash = {}
for overlay in overlays
overlaysHash[overlay.user_id()] = overlay
intersections = {}
for overlay in overlays
view = $(overlay.div_)
# Find every other overlay that this intersects with
for comparisonOverlay in _.without(overlays, overlay)
if rectForOverlay(overlay).intersects(rectForOverlay(comparisonOverlay))
overlay_id = overlay.user_id()
comparison_overlay_id = comparisonOverlay.user_id()
if intersections[overlay_id]?
intersections[overlay_id].push comparison_overlay_id
else
intersections[overlay_id] = [comparison_overlay_id]
if intersections[comparison_overlay_id]?
intersections[comparison_overlay_id].push overlay_id
else
intersections[comparison_overlay_id] = [overlay_id]
# haveIntersections.push overlay
# haveIntersections.push comparisonOverlay
# Uniq intersections
for key, value of intersections
intersections[key] = _.uniq(value)
# Resolve overlaps
for key, value of intersections
masterOverlay = overlaysHash[key]
for slave_id in value
slave = overlaysHash[slave_id]
masterOverlay.coalesceWithOverlay(slave)
slave.hide()
delete intersections[slave_id]
delete intersections[key]
rectForOverlay = (overlay) ->
view = $(overlay.div_)
l = view.position().left
t = view.position().top
r = l + view.width()
b = t + view.height()
rect = new Rect(l, r, t, b)
updateMarkers = (map) ->
console.log "update4234reweMarkers"
url = $("#module-location").data("marker-url")
$.getJSON url, (data) ->
bounds = new google.maps.LatLngBounds()
# Set bounds
for user in data
ll = new google.maps.LatLng user.latitude.latitude, user.latitude.longitude
bounds.extend ll
map.fitBounds bounds
map.setZoom 15 if map.getZoom() > 15
map.panBy(100, 0)
# Clear overlays
for overlay in window.overlays
overlay.setMap(null)
window.overlays = []
# Markers
for user in data
person = user.latitude
ll = new google.maps.LatLng person.latitude, person.longitude
window.overlays.push new TxtOverlay(ll, person.marker, "google-map-html-overlay", map )
# Update regularly
window.setTimeout ->
updateMarkers(map)
, 10000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment