我的网站上需要谷歌地图API的帮助。我将地图包含在我的网站中,然后自动绘制我的位置或找到我,但问题是它给了我错误的位置,但有些位置是正确的。
这是我的网站:http://webserver.site88.net/beta-test
这是mycode:
<script type="text/javascript">
function writeAddressName(latLng) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
"location": latLng
},
function(results, status) {
if (status == google.maps.GeocoderStatus.OK)
document.getElementById("address").value = results[0].formatted_address;
else
document.getElementById("error").innerHTML += "Unable to retrieve your address" + "<br />";
});
}
function geolocationSuccess(position) {
var userLatLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
document.getElementById("inLat").value = position.coords.latitude;
document.getElementById("inLong").value = position.coords.longitude;
// Write the formatted address
writeAddressName(userLatLng);
var myOptions = {
zoom : 16,
center : userLatLng,
mapTypeId : google.maps.MapTypeId.ROADMAP,
location_type: google.maps.GeocoderLocationType.ROOFTOP
};
// Draw the map
var mapObject = new google.maps.Map(document.getElementById("map"), myOptions);
// Place the marker
var marker = new google.maps.Marker({
map: mapObject,
position: userLatLng
});
var infowindow = new google.maps.InfoWindow({
content: '<div>'+'<b>'+'You are here '+'</b>'+'</div>'
});
infowindow.open(mapObject,marker);
google.maps.event.addDomListener(window, 'load', geolocateUser);
}
function geolocationError(positionError) {
document.getElementById("error").innerHTML += "Error: " + positionError.message + "<br />";
}
function geolocateUser() {
// If the browser supports the Geolocation API
if (navigator.geolocation)
{
var positionOptions = {
enableHighAccuracy: true,
timeout: 10 * 1000 // 10 seconds
};
navigator.geolocation.getCurrentPosition(geolocationSuccess, geolocationError, positionOptions);
}
else
document.getElementById("error").innerHTML += "Your browser doesn't support the Geolocation API";
}
</script>