File size: 7,941 Bytes
5c876be | 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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | import type { LatLng } from '../../types/location';
export interface LeafletMapMarker {
lat: number;
lng: number;
title?: string;
type?: 'origin' | 'destination' | 'pickup' | 'default';
}
export interface LeafletMapPolyline {
points: LatLng[];
color?: string;
weight?: number;
}
export interface LeafletMapOptions {
centerLat: number;
centerLng: number;
zoom?: number;
markers?: LeafletMapMarker[];
polylines?: LeafletMapPolyline[];
showCurrentLocation?: boolean;
interactive?: boolean;
showRoute?: boolean;
}
/**
* Returns a self-contained HTML string that renders a Leaflet.js map
* inside a WebView. Includes CDN links for Leaflet CSS / JS and
* communicates with the parent via `window.ReactNativeWebView.postMessage`.
*/
export function getLeafletMapHtml(options: LeafletMapOptions): string {
const {
centerLat,
centerLng,
zoom = 14,
markers = [],
polylines = [],
showCurrentLocation = false,
interactive = true,
} = options;
const markersJson = JSON.stringify(markers);
const polylinesJson = JSON.stringify(polylines);
return `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<link
rel="stylesheet"
href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css"
integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY="
crossorigin=""
/>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; width: 100%; overflow: hidden; }
#map { width: 100%; height: 100%; }
/* Pulsing user-location marker */
.pulse-marker {
width: 18px;
height: 18px;
background: #4CAF50;
border: 3px solid #FFFFFF;
border-radius: 50%;
box-shadow: 0 0 0 rgba(76, 175, 80, 0.4);
animation: pulse 2s ease-in-out infinite;
}
@keyframes pulse {
0% { box-shadow: 0 0 0 0 rgba(76, 175, 80, 0.6); }
70% { box-shadow: 0 0 0 16px rgba(76, 175, 80, 0); }
100% { box-shadow: 0 0 0 0 rgba(76, 175, 80, 0); }
}
.origin-marker {
width: 14px;
height: 14px;
background: #1B6B4D;
border: 3px solid #FFFFFF;
border-radius: 50%;
box-shadow: 0 1px 4px rgba(0,0,0,0.3);
}
.destination-marker {
width: 14px;
height: 14px;
background: #E8A838;
border: 3px solid #FFFFFF;
border-radius: 50%;
box-shadow: 0 1px 4px rgba(0,0,0,0.3);
}
.default-marker {
width: 12px;
height: 12px;
background: #1565C0;
border: 2px solid #FFFFFF;
border-radius: 50%;
box-shadow: 0 1px 4px rgba(0,0,0,0.3);
}
</style>
</head>
<body>
<div id="map"></div>
<script
src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"
integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo="
crossorigin=""
></script>
<script>
(function () {
var centerLat = ${centerLat};
var centerLng = ${centerLng};
var zoom = ${zoom};
var interactive = ${interactive};
var showCurrentLocation = ${showCurrentLocation};
var markers = ${markersJson};
var polylines = ${polylinesJson};
var map = L.map('map', {
center: [centerLat, centerLng],
zoom: zoom,
zoomControl: interactive,
attributionControl: interactive,
dragging: interactive,
scrollWheelZoom: interactive,
doubleClickZoom: interactive,
touchZoom: interactive,
boxZoom: interactive,
keyboard: interactive,
tap: interactive,
});
if (interactive) {
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
maxZoom: 19,
}).addTo(map);
} else {
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attributionControl: false,
}).addTo(map);
map.attributionControl && map.removeControl(map.attributionControl);
}
// Current location marker
var currentLocMarker = null;
if (showCurrentLocation) {
var pulseIcon = L.divIcon({
className: '',
html: '<div class="pulse-marker"></div>',
iconSize: [18, 18],
iconAnchor: [9, 9],
});
currentLocMarker = L.marker([centerLat, centerLng], { icon: pulseIcon }).addTo(map);
}
// Custom markers
var markerInstances = [];
markers.forEach(function (m) {
var cssClass = 'default-marker';
if (m.type === 'origin') cssClass = 'origin-marker';
if (m.type === 'destination') cssClass = 'destination-marker';
var icon = L.divIcon({
className: '',
html: '<div class="' + cssClass + '"></div>',
iconSize: [14, 14],
iconAnchor: [7, 7],
});
var marker = L.marker([m.lat, m.lng], { icon: icon }).addTo(map);
if (m.title) {
marker.bindTooltip(m.title, { direction: 'top', offset: [0, -10] });
}
marker.on('click', function () {
sendMsg({ event: 'marker_tap', data: { lat: m.lat, lng: m.lng, title: m.title, type: m.type } });
});
markerInstances.push(marker);
});
// Polylines
polylines.forEach(function (p) {
var latLngs = p.points.map(function (pt) { return [pt.lat, pt.lng]; });
L.polyline(latLngs, {
color: p.color || '#1B6B4D',
weight: p.weight || 5,
opacity: 0.8,
lineCap: 'round',
lineJoin: 'round',
}).addTo(map);
});
// Map move events
if (interactive) {
map.on('moveend', function () {
var c = map.getCenter();
sendMsg({ event: 'map_move', data: { lat: c.lat, lng: c.lng, zoom: map.getZoom() } });
});
}
function sendMsg(obj) {
if (window.ReactNativeWebView) {
window.ReactNativeWebView.postMessage(JSON.stringify(obj));
}
}
// Fit bounds to markers if we have multiple
if (markerInstances.length >= 2) {
var group = L.featureGroup(markerInstances);
map.fitBounds(group.getBounds().pad(0.15));
}
// Notify ready
sendMsg({ event: 'map_ready', data: { lat: centerLat, lng: centerLng, zoom: zoom } });
// Expose methods for the RN side to call via injectedJavaScript
window.setCenter = function (lat, lng, z) {
map.setView([lat, lng], z || map.getZoom());
};
window.addMarker = function (lat, lng, type, title) {
var cssClass = 'default-marker';
if (type === 'origin') cssClass = 'origin-marker';
if (type === 'destination') cssClass = 'destination-marker';
var icon = L.divIcon({
className: '',
html: '<div class="' + cssClass + '"></div>',
iconSize: [14, 14],
iconAnchor: [7, 7],
});
var marker = L.marker([lat, lng], { icon: icon }).addTo(map);
if (title) marker.bindTooltip(title, { direction: 'top', offset: [0, -10] });
return marker;
};
window.updateCurrentLocation = function (lat, lng) {
if (currentLocMarker) {
currentLocMarker.setLatLng([lat, lng]);
} else {
var pulseIcon = L.divIcon({
className: '',
html: '<div class="pulse-marker"></div>',
iconSize: [18, 18],
iconAnchor: [9, 9],
});
currentLocMarker = L.marker([lat, lng], { icon: pulseIcon }).addTo(map);
}
};
window.fitToMarkers = function () {
if (markerInstances.length >= 2) {
map.fitBounds(L.featureGroup(markerInstances).getBounds().pad(0.15));
}
};
})();
</script>
</body>
</html>`;
}
|