Skip to content

Commit da6af5c

Browse files
committed
add auto position
1 parent 71735b7 commit da6af5c

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

index.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ <h1 id="web-title">全台灣郵筒位置地圖</h1>
160160
markers.forEach(function(marker) {
161161
marker.setMap(null);
162162
});
163+
163164
markers = [];
164165

165166
var bounds = new google.maps.LatLngBounds();
@@ -191,6 +192,23 @@ <h1 id="web-title">全台灣郵筒位置地圖</h1>
191192
});
192193
map.fitBounds(bounds);
193194
});
195+
if (navigator.geolocation) {
196+
navigator.geolocation.getCurrentPosition(function(position) {
197+
var pos = {
198+
lat: position.coords.latitude,
199+
lng: position.coords.longitude
200+
};
201+
var marker = new google.maps.Marker({
202+
position: pos,
203+
map: map
204+
});
205+
map.setZoom(17);
206+
map.setCenter(pos);
207+
});
208+
} else {
209+
// Browser doesn't support Geolocation
210+
alert("未允許或遭遇錯誤!");
211+
}
194212
}
195213

196214
</script>

0 commit comments

Comments
 (0)