-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-is-inside-coverage-area.html
161 lines (134 loc) · 4.75 KB
/
check-is-inside-coverage-area.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Check if point is inside coverage area</title>
<script src='https://api.mapbox.com/mapbox-gl-js/v2.0.1/mapbox-gl.js'></script>
<link href='https://api.mapbox.com/mapbox-gl-js/v2.0.1/mapbox-gl.css' rel='stylesheet' />
<script src='https://unpkg.com/@turf/turf@6/turf.min.js'></script>
<style>
html,
body {
margin: 0;
}
#map-container {
width: min(100vw, 800px);
height: min(80vw, 500px);
}
</style>
</head>
<body>
<h1>Clique em um local no mapa para calcular o valor da entrega:</h1>
<div id="map-container"></div>
<p id="result"></p>
<script>
mapboxgl.accessToken = 'pk.eyJ1IjoiaWFnb2JydW5vIiwiYSI6ImNram41cDI1bjBhYWEyeXFscGp5ZG15bnAifQ.kGQsH0C6Li7MwXrOWr1Qmw';
const carnaubalLatLng = new mapboxgl.LngLat(-40.941498, -4.162548);
const resultEl = document.getElementById('result')
// Init map
const map = new mapboxgl.Map({
container: document.getElementById('map-container'),
center: carnaubalLatLng, // starting position [lng, lat]
zoom: 12, // starting zoom
style: 'mapbox://styles/mapbox/streets-v11', // stylesheet location
});
const marker = new mapboxgl.Marker();
let pointB = null;
const areas = {
// meters: price,
300: 1,
700: 2,
1200: 3,
2300: 4,
4000: 5,
}
map.on('load', function (event) {
for (const meters in areas) {
const deliveryFee = areas[meters];
drawCircle(Number(meters));
}
})
map.on('click', function (event) {
pointB = new mapboxgl.LngLat(event.lngLat.lng, event.lngLat.lat);
showMarkerOnMap(event.lngLat);
calcPriceFromCenterToPointB();
})
function showMarkerOnMap(lngLat) {
marker.setLngLat(lngLat).addTo(map);
}
function drawCircle(radiusInMeters) {
const center = carnaubalLatLng;
const radiusInKilometers = radiusInMeters / 1000
const circle = turf.circle(center.toArray(), radiusInKilometers, {
units: 'kilometers',
});
map.addLayer({
id: "circle-fill_" + Date.now(),
type: "fill",
source: {
type: "geojson",
data: circle
},
paint: {
"fill-color": "red",
"fill-opacity": 0.1,
"fill-outline-color": '#000',
}
});
// Mostrar o raio do circulo
const el = document.createElement('div');
const text = document.createElement('div');
text.textContent = radiusInMeters < 1000 ? `${radiusInMeters} M` : `${radiusInMeters / 1000} KM`;
text.style = `opacity:0.6; color:red; font-weight:600; transform:rotate(-90deg);`
el.appendChild(text);
new mapboxgl.Marker(el).setLngLat(destination(center, -90, radiusInMeters)).addTo(map);
}
function calcPriceFromCenterToPointB() {
const distanceInMeters = carnaubalLatLng.distanceTo(pointB);
let isWithinCoverageArea = false
let price = 0;
for (const [areaRadius, areaPrice] of Object.entries(areas)) {
if (distanceInMeters <= areaRadius) {
price = areaPrice;
isWithinCoverageArea = true
break;
}
}
if (isWithinCoverageArea) {
resultEl.innerHTML = `
Distância: ${prettyPrintNumber(Math.floor(distanceInMeters))} metros<br>
Preço da entrega = ${price.toLocaleString('pt-BR', { style: 'currency', currency: 'BRL' })}
`;
}
else {
resultEl.innerHTML = `<span style="color:red">❌ Está FORA da área de cobertura</span>`
}
}
function prettyPrintNumber(number) {
return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ".");
}
// From https://github.com/makinacorpus/Leaflet.GeometryUtil/blob/master/src/leaflet.geometryutil.js#L713
function destination(latlng, heading, distance) {
heading = (heading + 360) % 360;
var rad = Math.PI / 180,
radInv = 180 / Math.PI,
R = 6378137, // approximation of Earth's radius
lon1 = latlng.lng * rad,
lat1 = latlng.lat * rad,
rheading = heading * rad,
sinLat1 = Math.sin(lat1),
cosLat1 = Math.cos(lat1),
cosDistR = Math.cos(distance / R),
sinDistR = Math.sin(distance / R),
lat2 = Math.asin(sinLat1 * cosDistR + cosLat1 *
sinDistR * Math.cos(rheading)),
lon2 = lon1 + Math.atan2(Math.sin(rheading) * sinDistR *
cosLat1, cosDistR - sinLat1 * Math.sin(lat2));
lon2 = lon2 * radInv;
lon2 = lon2 > 180 ? lon2 - 360 : lon2 < -180 ? lon2 + 360 : lon2;
return new mapboxgl.LngLat(lon2, lat2 * radInv);
}
</script>
</body>
</html>