2012-08-24 46 views
1

我用Google Map寫了一段代碼來實現位置和GPS功能。 但導航路線總是無法顯示,我不知道哪裏出錯了? 尋求幫助。誰能幫助我? 謝謝!Google Map API麻煩,請教支持

JavaScript代碼

<script type="text/javascript"> 

$("#map-page").live("pageinit", function() { 

    var directionsDisplay; 
    var directionsService = new google.maps.DirectionsService(); 
    var map; 
    var salon = new google.maps.LatLng(22.981666,120.194301); 
    var defaultLatLng = new google.maps.LatLng(22.983587,120.22599); // Default to Hollywood, CA when no geolocation support 

    if (navigator.geolocation) { 
     function success(pos) { 
      // Location found, show map with these coordinates 
      drawMap(new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude)); 
      calcRoute(new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude));  
     } 

     function fail(error) { 
      console.log(error); 
      drawMap(defaultLatLng); // Failed to find location, show default map 
     } 

     // Find the users current position. Cache the location for 5 minutes, timeout after 6 seconds 
     navigator.geolocation.getCurrentPosition(success, fail, {maximumAge: 500000, enableHighAccuracy:true, timeout: 6000}); 
    } else { 
     drawMap(defaultLatLng); // No geolocation support, show default map  
    } 

    function drawMap(latlng) { 

     directionsDisplay = new google.maps.DirectionsRenderer(); 

     var myOptions = { 
      zoom: 10, 
      center: latlng, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 

     }; 

     map = new google.maps.Map(document.getElementById("map-canvas"), myOptions); 
     directionsDisplay.setMap(map); 
     // Add an overlay to the map of current lat/lng 

     var marker = new google.maps.Marker({ 
      position: latlng, 
      map: map, 
      title: "Greetings!" 
     }); 

     var marker = new google.maps.Marker({ 
      position:new google.maps.LatLng(22.981666,120.194301), 
      map:map, 
      title:"the salon" 
     }); 
    } 

    function calcRoute(latlng) { 
    var start = latlng; 
    var end = new google.maps.LatLng(22.981666,120.194301); 
    var request = { 
     origin:start, 
     destination:end, 
     travelMode: google.maps.TravelMode.DRIVING 
    }; 
    directionsService.route(request, function(response, status) { 
     if (status == google.maps.DirectionsStatus.OK) { 
     directionsDisplay.setDirections(response); 
    } 
    }); 
} 


}); 


    </script> 
+0

我希望你有'傳感器= TRUE'? – mast0r

+0

關於您的代碼的注意事項 - 您的評論閱讀 - 「查找用戶當前位置,緩存位置5分鐘,6秒後超時」 - 但您的代碼顯示爲:navigator.geolocation.getCurrentPosition(success,fail,{maximumAge :500000,enableHighAccuracy:true,timeout:6000});有兩個問題。 500000是8.33分鐘,而不是5分鐘(500000/1000/60)。但是,更重要的是,現在這是最大年齡的含義。您正在告訴設備您願意接受的位置年齡,但告訴它需要多長時間進行緩存。 –

回答

0

我已經制定了 這段代碼是錯誤的

if (navigator.geolocation) {  
    function success(pos) {  
     // Location found, show map with these coordinates  
     drawMap(new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude));  
     calcRoute(new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude));   
    }  

    function fail(error) {  
     console.log(error);  
     drawMap(defaultLatLng); // Failed to find location, show default map  
    }  

    // Find the users current position. Cache the location for 5 minutes, timeout after 6 seconds  
    navigator.geolocation.getCurrentPosition(success, fail, {maximumAge: 500000, enableHighAccuracy:true, timeout: 6000});  
} else {  
    drawMap(defaultLatLng); // No geolocation support, show default map   
} 

我直接

// Location found, show map with these coordinates  
drawMap(defaultLatLng); 
calcRoute(defaultLatLng);