Ho un problema con google Maps. Sto facendo un sito e vorrei che l'utente potesse calcolare il percorso dal posto in cui si trova fino a destinazione. Problema: non riesco a impostare come posto di partenza la posizione dell'utente presa attraverso GPS. Come fare? Grazie in anticipo.
<script type="text/javascript">
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
var trento = new google.maps.LatLng(41.890809,12.492399);
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(load, error);
} else { error('not supported'); }
function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer();
var mapOptions = {
zoom: 14,
mapTypeId: google.maps.MapTypeId.WALKING,
center: latlng
}
map = new google.maps.Map(document.getElementById("map"), mapOptions);
directionsDisplay.setMap(map);
}
function calcRoute() {
var selectedMode = document.getElementById("mode").value;
var request = {
origin: latlng,
destination: trento,
travelMode: google.maps.TravelMode[selectedMode]
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
</script>