2013-07-22 49 views
0

我有一個谷歌地圖的API的問題。我必須在我的網站上添加一張地圖,顯示用戶的位置和前往另一個點的路線。谷歌地圖API,從2點的方向

1)我不能給出請求的「起源」,地理定位的變量pos。

2)我不能給自動縮放與查看兩個標記coorect距離。

這是我的代碼:

var directionsService = new google.maps.DirectionsService(); 
var directionsDisplay = new google.maps.DirectionsRenderer(); 

var request = { 
    travelMode: google.maps.DirectionsTravelMode.DRIVING 
    }; 

//Initializing map 
var initialize = function() { 

    // Taking the address of school 
    var address = document.getElementById('address').firstChild.nodeValue; 
    var city = document.getElementById("city").firstChild.nodeValue; 
    var comun = document.getElementById("comun").firstChild.nodeValue; 
    var search = address + " " + city + " " + comun; 

    // Initializing the geocoder and searcing the address in search 
    var geocoder = new google.maps.Geocoder(); 
    geocoder.geocode({'address': search}, function(results,status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
     var marker2 = new google.maps.Marker({ 
     position: results[0].geometry.location, 
     map: map, 
     title: 'Posizione scuola' 
     }); 
    } else { 
     alert("Problema nella ricerca dell'indirizzo: " + status); 
    } 
    }); 

    // Input the visualization options 
    var options = { 
    zoom: 12, 
    mapTypeId: google.maps.MapTypeId.ROADMAP, 
    disableDefaultUI: true 
    }; 

    // Create the map 
    var map = new google.maps.Map(document.getElementById('maps'), options); 

    // Try HTML5 geolocation 
    if(navigator.geolocation) { 
    navigator.geolocation.getCurrentPosition(function(position) { 
     var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); 
     map.setCenter(pos); 
     request.origin = (map.center); 
     // Put the marker 
     var marker = new google.maps.Marker({ 
     position: pos, 
     map: map, 
     title: 'Tua posizione' 
     }); 
    }); 
    } else { 
    alert("Il tuo dispositivo non permette di visualizzare la tua posizione"); 
    } 

    directionsDisplay.setMap(map); 
    calcRoute(); 
} // End initialize 

var calcRoute =function() { 

    // Taking the address of school 
    var address = document.getElementById('address').firstChild.nodeValue; 
    var city = document.getElementById("city").firstChild.nodeValue; 
    var comun = document.getElementById("comun").firstChild.nodeValue; 
    var search = address + " " + city + " " + comun; 

    request.destination = search; 

    directionsService.route(request, function(response, status) { 
    if (status == google.maps.DirectionsStatus.OK) { 
     directionsDisplay.setDirections(response); 
    } 
    }); 
} 

window.onload = initialize; 

謝謝您的幫助

回答

0

1)必須的calcRoute通話遷入getCurrentPosition成功回調(大地定位是一個異步過程,目前request.origin並沒有被設定,當你調用calcRoute)當你有固定的1

if(navigator.geolocation) { 
    navigator.geolocation.getCurrentPosition(function(position) { 
     var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); 
     map.setCenter(pos); 
     request.origin = (pos); 
     // Put the marker 
     var marker = new google.maps.Marker({ 
     position: pos, 
     map: map, 
     title: 'Tua posizione' 
     }); 
     directionsDisplay.setMap(map); 
     calcRoute(); 
    }); 
    } else { 
    alert("Il tuo dispositivo non permette di visualizzare la tua posizione"); 
    } 
} 

2)將工作)