Skip to content

Commit

Permalink
Update oms.coffee to work with AdvancedMarkerElement
Browse files Browse the repository at this point in the history
Changes:
 * Google didn't provide an equivalent for getVisible in AdvancedMarkerElement; I'm not quite sure how this was intended to be used - whether it was asking "is this marker's location within the bounds of the map" or "is this marker status visible vs. invisible".  So I created a new method IsVisibleMarker that returns true if the marker has a non-null map (map == null is how you remove a marker from the map) AND is within the bounds of that maps latitude/longitude boundaries.  This works for my purposes, I hope it is the correct definition
 * changed getPosition/setPosition to .position property
 * changed getZIndex/setZIndex to .zIndex property
 * commented out the line "(return i if o is obj) for o, i in arr" in p.arrIndexOf.  If I'm understanding Coffee correctly (I'm not!!) this never gets called if arr.indexOf is defined, so this seems to work...

Probably worth noting a few other things I had to do to migrate to AdvancedMarkerElement:
 * No longer use defered script include, so I no longer get a callback when it is loaded, so the old code of waiting for both oms and maps to load no longer works.    E.g., now I load googlemaps with        (g => { var h, a, k, p = "The Google Maps JavaScript API", c = "google", l = "importLibrary", q = "__ib__", m = document, b = window; b = b[c] || (b[c] = {}); var d = b.maps || (b.maps = {}), r = new Set, e = new URLSearchParams, u = () => h || (h = new Promise(async (f, n) => { await (a = m.createElement("script")); e.set("libraries", [...r] + ""); for (k in g) e.set(k.replace(/[A-Z]/g, t => "_" + t[0].toLowerCase()), g[k]); e.set("callback", c + ".maps." + q); a.src = `https://maps.${c}apis.com/maps/api/js?` + e; d[q] = f; a.onerror = () => h = n(Error(p + " could not load.")); a.nonce = m.querySelector("script[nonce]")?.nonce || ""; m.head.append(a) })); d[l] ? console.warn(p + " only loads once. Ignoring:", g) : d[l] = (f, ...n) => r.add(f) && u().then(() => d[l](f, ...n)) })({
           key: "...",
           v: "weekly",
           // Use the 'v' parameter to indicate the version to use (weekly, beta, alpha, etc.).
           // Add other bootstrap parameters as needed, using camel case.
       });

So now I need to create the  overlappingmarkerspiderfier in oogle.maps.event.addListenerOnce(this.gmap, 'idle', function () { ...}
  • Loading branch information
ericberman committed Mar 19, 2024
1 parent 1f18a3e commit 5038dce
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions lib/oms.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ class @['OverlappingMarkerSpiderfier']
p.initMarkerArrays = ->
@markers = []
@markerListenerRefs = []

p.isVisibleMarker = (m) -> m.map != null && m.map.getBounds().contains(m.position)


p['addMarker'] = (marker, spiderClickHandler) ->
marker.setMap(@map)
Expand Down Expand Up @@ -123,7 +126,7 @@ class @['OverlappingMarkerSpiderfier']
p.markerChangeListener = (marker, positionChanged) ->
return if @spiderfying or @unspiderfying

if marker['_omsData']? and (positionChanged or not marker.getVisible())
if marker['_omsData']? and (positionChanged or not @isVisibleMarker(marker))
@['unspiderfy'](if positionChanged then marker else null)

@formatMarkers()
Expand Down Expand Up @@ -212,7 +215,7 @@ class @['OverlappingMarkerSpiderfier']
pxSq = nDist * nDist
markerPt = @llToPt(marker.position)
for m in @markers
continue unless m.map? and m.getVisible() # at 2011-08-12, property m.visible is undefined in API v3.5
continue unless m.map? and @isVisibleMarker(m) # at 2011-08-12, property m.visible is undefined in API v3.5
mPt = @llToPt(m.position)
if @ptDistanceSq(mPt, markerPt) < pxSq
nearbyMarkerData.push(marker: m, markerPt: mPt)
Expand All @@ -231,7 +234,7 @@ class @['OverlappingMarkerSpiderfier']
markerPt = @llToPt(marker.position)
markers = []
for m in @markers
continue if m is marker or not m.map? or not m.getVisible()
continue if m is marker or not m.map? or not @isVisibleMarker(m)
mPt = @llToPt(m['_omsData']?.usualPosition ? m.position)
if @ptDistanceSq(mPt, markerPt) < pxSq
markers.push(m)
Expand All @@ -246,12 +249,12 @@ class @['OverlappingMarkerSpiderfier']
mData = for m in @markers
{pt: @llToPt(m['_omsData']?.usualPosition ? m.position), willSpiderfy: no}
for m1, i1 in @markers
continue unless m1.getMap()? and m1.getVisible() # marker not visible: ignore
continue unless m1.getMap()? and @isVisibleMarker(m1) # marker not visible: ignore
m1Data = mData[i1]
continue if m1Data.willSpiderfy # true in the case that we've assessed an earlier marker that was near this one
for m2, i2 in @markers
continue if i2 is i1 # markers cannot be near themselves: ignore
continue unless m2.getMap()? and m2.getVisible() # marker not visible: ignore
continue unless m2.getMap()? and @isVisibleMarker(m2) # marker not visible: ignore
m2Data = mData[i2]
continue if i2 < i1 and not m2Data.willSpiderfy # if i2 < i1, m2 has already been checked for proximity to any other marker;
# so if willSpiderfy is false, it cannot be near any other marker, including this one (m1)
Expand Down Expand Up @@ -324,8 +327,8 @@ class @['OverlappingMarkerSpiderfier']
strokeWeight: @['legWeight']
zIndex: @['usualLegZIndex']
marker['_omsData'] =
usualPosition: marker.getPosition()
usualZIndex: marker.getZIndex()
usualPosition: marker.position
usualZIndex: marker.zIndex
leg: leg
unless @['legColors']['highlighted'][@map.mapTypeId] is
@['legColors']['usual'][@map.mapTypeId]
Expand All @@ -334,8 +337,8 @@ class @['OverlappingMarkerSpiderfier']
highlight: ge.addListener(marker, 'mouseover', highlightListenerFuncs.highlight)
unhighlight: ge.addListener(marker, 'mouseout', highlightListenerFuncs.unhighlight)
@trigger('format', marker, @constructor['markerStatus']['SPIDERFIED'])
marker.setPosition(footLl)
marker.setZIndex(Math.round(@['spiderfiedZIndex'] + footPt.y)) # lower markers cover higher
marker.position = footLl
marker.zIndex = Math.round(@['spiderfiedZIndex'] + footPt.y) # lower markers cover higher
marker
delete @spiderfying
@spiderfied = yes
Expand All @@ -349,8 +352,8 @@ class @['OverlappingMarkerSpiderfier']
for marker in @markers
if marker['_omsData']?
marker['_omsData'].leg.setMap(null)
marker.setPosition(marker['_omsData'].usualPosition) unless marker is markerNotToMove
marker.setZIndex(marker['_omsData'].usualZIndex)
marker.position = marker['_omsData'].usualPosition unless marker is markerNotToMove
marker.zIndex = marker['_omsData'].usualZIndex
listeners = marker['_omsData'].hightlightListeners
if listeners?
ge.removeListener(listeners.highlight)
Expand Down Expand Up @@ -392,7 +395,7 @@ class @['OverlappingMarkerSpiderfier']

p.arrIndexOf = (arr, obj) ->
return arr.indexOf(obj) if arr.indexOf?
(return i if o is obj) for o, i in arr
# (return i if o is obj) for o, i in arr
-1


Expand Down

3 comments on commit 5038dce

@ericberman
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue jawj#180

@copiri-six
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI @ericberman... I've included your updates and a few others in a public fork here: https://github.com/copiri-six/AME_Spiderfier. It works with AdvancedMarkerElement... at least, for my purposes. Feel free to use the compiled script from there, if it's useful to you.

@ericberman
Copy link
Owner Author

@ericberman ericberman commented on 5038dce May 21, 2024 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.