Skip to content

Commit f0f3977

Browse files
committed
Show current location marker on map
1 parent 8603a87 commit f0f3977

1 file changed

Lines changed: 79 additions & 12 deletions

File tree

Sources/StreetViewWander/StreetViewWebView.swift

Lines changed: 79 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -133,26 +133,43 @@ struct StreetViewWebView: NSViewRepresentable {
133133
#mapPanel.is-collapsed #mapResizeHandle {
134134
display: none;
135135
}
136-
#mapPanel.is-map-paused #mapBody,
137-
#mapPanel.is-map-paused #mapCenterPin {
136+
#mapPanel.is-map-paused #mapBody {
138137
display: none;
139138
}
140139
#map {
141140
width: 100%;
142141
height: 100%;
143142
}
144-
#mapCenterPin {
143+
.currentLocationMarker {
144+
position: absolute;
145+
width: 30px;
146+
height: 42px;
147+
transform: translate(-50%, -100%);
148+
pointer-events: none;
149+
}
150+
.currentLocationMarker::before {
151+
content: "";
145152
position: absolute;
146153
left: 50%;
147-
top: 50%;
148-
width: 14px;
149-
height: 14px;
150-
border: 3px solid #fff;
151-
border-radius: 50%;
152-
background: #176c5f;
154+
top: 4px;
155+
width: 24px;
156+
height: 24px;
157+
border: 2px solid #fff;
158+
border-radius: 50% 50% 50% 0;
159+
background: #d93025;
153160
box-shadow: 0 2px 10px rgba(0,0,0,.35);
154-
transform: translate(-50%, -50%);
155-
pointer-events: none;
161+
transform: translateX(-50%) rotate(-45deg);
162+
}
163+
.currentLocationMarker::after {
164+
content: "";
165+
position: absolute;
166+
left: 50%;
167+
top: 12px;
168+
width: 8px;
169+
height: 8px;
170+
border-radius: 50%;
171+
background: #fff;
172+
transform: translateX(-50%);
156173
}
157174
#mapToggle {
158175
position: absolute;
@@ -242,7 +259,6 @@ struct StreetViewWebView: NSViewRepresentable {
242259
<button id="mapToggle" type="button" aria-controls="mapBody" aria-expanded="true">Hide map</button>
243260
<div id="mapBody">
244261
<div id="map"></div>
245-
<div id="mapCenterPin" aria-hidden="true"></div>
246262
</div>
247263
</aside>
248264
<div id="empty">Street View will appear here.</div>
@@ -268,6 +284,7 @@ struct StreetViewWebView: NSViewRepresentable {
268284
let loadedKey = null;
269285
let panorama = null;
270286
let map = null;
287+
let locationMarker = null;
271288
let activeGoogle = null;
272289
let currentCenter = null;
273290
let isMapExpanded = false;
@@ -315,6 +332,7 @@ struct StreetViewWebView: NSViewRepresentable {
315332
}
316333
if (currentCenter) {
317334
map.setCenter(currentCenter);
335+
locationMarker?.draw();
318336
}
319337
});
320338
}
@@ -355,6 +373,54 @@ struct StreetViewWebView: NSViewRepresentable {
355373
}, 450);
356374
}
357375
376+
function createLocationMarker() {
377+
const markerElement = document.createElement('div');
378+
markerElement.className = 'currentLocationMarker';
379+
markerElement.setAttribute('aria-label', 'Current location');
380+
381+
const overlay = new activeGoogle.maps.OverlayView();
382+
overlay.onAdd = () => {
383+
overlay.getPanes()?.overlayMouseTarget?.appendChild(markerElement);
384+
};
385+
overlay.draw = () => {
386+
if (!currentCenter) {
387+
markerElement.style.display = 'none';
388+
return;
389+
}
390+
const projection = overlay.getProjection();
391+
const point = projection?.fromLatLngToDivPixel(
392+
new activeGoogle.maps.LatLng(currentCenter.lat, currentCenter.lng)
393+
);
394+
if (!point) {
395+
markerElement.style.display = 'none';
396+
return;
397+
}
398+
markerElement.style.display = 'block';
399+
markerElement.style.left = `${point.x}px`;
400+
markerElement.style.top = `${point.y}px`;
401+
};
402+
overlay.onRemove = () => {
403+
markerElement.remove();
404+
};
405+
overlay.setMap(map);
406+
407+
return {
408+
draw: () => overlay.draw(),
409+
remove: () => overlay.setMap(null)
410+
};
411+
}
412+
413+
function syncLocationMarker() {
414+
if (!activeGoogle || !map || !currentCenter) {
415+
return;
416+
}
417+
if (!locationMarker) {
418+
locationMarker = createLocationMarker();
419+
} else {
420+
locationMarker.draw();
421+
}
422+
}
423+
358424
function syncMap(center) {
359425
if (center) {
360426
currentCenter = center;
@@ -378,6 +444,7 @@ struct StreetViewWebView: NSViewRepresentable {
378444
map.setCenter(center);
379445
map.setZoom(MAP_ZOOM);
380446
}
447+
syncLocationMarker();
381448
}
382449
383450
function syncMapFromStreetView() {

0 commit comments

Comments
 (0)