2015-04-06 62 views
0

當我調用Google地圖方向服務時,出現Uncaught InvalidValueError: in property origin: not a string; and not a LatLng or LatLngLiteral: not an Object錯誤 - 即便如此,方向似乎已添加到地圖中了嗎?調用Google地圖方向時未捕獲的異常服務

這裏是調用當前位置(currentPos)和位置之間的路線服務不太遠的功能(LatLng(52.705151, -2.741861)

function calcRoute() { 
    var start = currentPos; 
    var end = new google.maps.LatLng(52.705151, -2.741861); 
    var request = { 
     origin: start, 
     destination: end, 
     travelMode: google.maps.TravelMode.WALKING 
    }; 
    directionsService.route(request, function (response, status) { 
     if (status == google.maps.DirectionsStatus.OK) { 
      directionsDisplay.setDirections(response); 
     } 
    }); 
} 

這裏是初始化地圖並調用calcRoute()函數的代碼:

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

var currentPos; 
var destinationPos; 

var map; 

function initialize() { 
    directionsDisplay = new google.maps.DirectionsRenderer(); 
    var mapOptions = { 
     zoom: 14 
    }; 

    map = new google.maps.Map(document.getElementById('map_canvas'), 
     mapOptions); 

    directionsDisplay.setMap(map); 

    var infoWindow = new google.maps.InfoWindow(); 

    if (navigator.geolocation) { 
     navigator.geolocation.getCurrentPosition(function (position) { 
      currentPos = new google.maps.LatLng(position.coords.latitude, 
              position.coords.longitude); 

      var marker = new google.maps.Marker({ 
       position: currentPos, 
       map: map, 
       title: "You are here" 
      }); 

      marker.setIcon('http://maps.google.com/mapfiles/ms/icons/green-dot.png') 

      map.setCenter(currentPos); 

      $.ajax({ 
       type: 'GET', 
       url: $('#AddMarkersToMap').val(), 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: function (data) { 
        addMarkers(data, infoWindow); 
       }, 
       error: function() { 
        alert("Error"); 
       } 
      }); 


     }, function() { 
      handleNoGeolocation(true); 
     }); 
    } else { 
     handleNoGeolocation(false); 
    } 

    calcRoute(); 


} 

一些谷歌搜索建議的例外可能引起因爲地圖沒有加載,所以我試圖把calcRoute這樣調用它並沒有幫助:

$(function() { 
     calcRoute() 
    }); 

回答

1

當您致電calcRoute時,您的currentPos變量未定義。
getCurrentPosition是異步的,在calcRoute之前不會執行,因此也不會設置currentPos
如果您需要在calcRoute中使用currentPos,則應在回調getCurrentPosition時調用它以確保其已定義。

if (navigator.geolocation) { 
    navigator.geolocation.getCurrentPosition(function (position) { 
     currentPos = new google.maps.LatLng(position.coords.latitude, 
             position.coords.longitude); 

     var marker = new google.maps.Marker({ 
      position: currentPos, 
      map: map, 
      title: "You are here" 
     }); 

     marker.setIcon('http://maps.google.com/mapfiles/ms/icons/green-dot.png') 

     map.setCenter(currentPos); 

     $.ajax({ 
      type: 'GET', 
      url: $('#AddMarkersToMap').val(), 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: function (data) { 
       addMarkers(data, infoWindow); 
      }, 
      error: function() { 
       alert("Error"); 
      } 
     }); 
     calcRoute(); 
     //\\//\\//\\ 
    }, function() { 
     handleNoGeolocation(true); 
    }); 
+0

完美的,謝謝你解釋得這麼好 – NRKirby