1
我正在嘗試構建一個簡單的頁面,顯示用戶相對於地圖上固定位置的位置。我已經能夠複製和修改一些代碼來跟蹤用戶的位置,但我不確定如何在同一個地圖上的固定位置添加標記(例如-38.100 +145.500)。任何幫助將不勝感激。我想跟蹤我的位置,並在同一個地圖上標記固定位置
到目前爲止,我有以下....
<!DOCTYPE html>
<html>
<head>
<style>
#map { width:100%; height:150px; }
</style>
<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
</head>
<body>
<div id="map"></div>
<script>
var marker;
if (navigator.geolocation) {
var timeoutVal = 10 * 1000 * 1000;
navigator.geolocation.watchPosition(
displayPosition,
displayError,
{ enableHighAccuracy: true, timeout: timeoutVal, maximumAge: 0 }
);
}
else {
alert("Geolocation is not supported by this browser");
}
function displayPosition(position) {
var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
var options = {
zoom: 18,
center: pos,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map"), options);
// Remove the current marker, if there is one
if (typeof(marker) != "undefined") marker.setMap(null);
marker = new google.maps.Marker({
position: pos,
map: map,
icon: 'https://upload.wikimedia.org/wikipedia/commons/a/ae/Bluedot.png',
title: "User location"
});
}
function displayError(error) {
var errors = {
1: 'Permission denied',
2: 'Position unavailable',
3: 'Request timeout'
};
alert("Error: " + errors[error.code]);
}
</script>
</body>
</html>
親切的問候
日
是的,我想有兩個標記上相同的地圖。一個像我在地圖上已經有的標記我的位置的標記,另一個標記給定對象的位置(比如說38.100 +145.500)。 –
你看到一行說var pos = new google.maps.LatLng?改變那條線! - 將這些座標放在括號內,看看會發生什麼! –
是的,這給了我一個標記,標記固定座標的位置,這也是我所能達到的。但我想在同一張地圖上得到兩個。所以用戶可以看到他們的當前位置和給定對象的位置。 –