2015-11-03 409 views
-3

這是我的地圖代碼。 但是當你運行它時,一切都很好。然後,如果你點擊「Let's Go!」你得到這個錯誤:
Assertion failed: InvalidValueError: in property origin: not a string; and not a LatLng or LatLngLiteral: not an Object; and not an ObjectAb @ maps.googleapis.com/maps-api-v3/api/js/22/11a/main.js:16 index.html:7Google Maps API最快路線

Uncaught TypeError: Cannot read property 'N' of null 

有誰知道如何解決這個問題???如果是的話請幫忙

HTML:

<div id="floating-panel"> 
    <input id="toAddress" type="textbox" value=""> 
    <input id="submit" type="button" value="Lets Go!"> 
</div> 
<div id="map"></div> 

的javascript:

 var currentPosition; 

     function initMap() { 
      var map = new google.maps.Map(document.getElementById('map'), { 
      zoom: 8, 
      center: {lat: 33.8, lng: -84.4} 

      }); 
      var geocoder = new google.maps.Geocoder(); 

      var trafficLayer = new google.maps.TrafficLayer(); 
      trafficLayer.setMap(map); 

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

      var infoWindow = new google.maps.InfoWindow({map: map}); 

      if (navigator.geolocation) { 
      navigator.geolocation.getCurrentPosition(function(position) { 
       var pos = { 
       lat: position.coords.latitude, 
       lng: position.coords.longitude 
       }; 

       currentPosition = new google.maps.LatLng(parseFloat(pos.geolocation.lat), parseFloat(pos.geolocation.lng)); 


       infoWindow.setPosition(pos); 
       infoWindow.setContent('Location found.'); 
       map.setCenter(pos); 
      }, function() { 
       handleLocationError(true, infoWindow, map.getCenter()); 
      }); 
      } else { 
      // Browser doesn't support Geolocation 
      handleLocationError(false, infoWindow, map.getCenter()); 
      } 
     } 

     function handleLocationError(browserHasGeolocation, infoWindow, pos) { 
      infoWindow.setPosition(pos); 
      infoWindow.setContent(browserHasGeolocation ? 
           'Error: The Geolocation service failed.' : 
           'Error: Your browser doesn\'t support geolocation.'); 
     } 
      document.getElementById('submit').addEventListener('click', function() { 
      // geocodeAddress(geocoder, map); 
     var directionsService = new google.maps.DirectionsService; 

      directionsService.route({ 
       origin: currentPosition, 
       destination: document.getElementById('toAddress').value, 
       travelMode: google.maps.TravelMode.DRIVING 
       }, function(response, status) { 
       if (status === google.maps.DirectionsStatus.OK) { 
        directionsDisplay.setDirections(response); 
       } else { 
        window.alert('Directions request failed due to ' + status); 
       } 
       }); 

      }); 



     function geocodeAddress(geocoder, resultsMap) { 
      var address = document.getElementById('address').value; 
      geocoder.geocode({'address': address}, function(results, status) { 
      if (status === google.maps.GeocoderStatus.OK) { 
       resultsMap.setCenter(results[0].geometry.location); 
       var marker = new google.maps.Marker({ 
       map: resultsMap, 
       position: results[0].geometry.location 
       }); 
      } else { 
       alert('Geocode was not successful for the following reason: ' + status); 
      } 
      }); 

     } 
+1

請提供一個[最小,完整,已測試和可讀的示例](http://stackoverflow.com/help/mcve)來說明問題。在發佈的代碼中沒有HTML(「Let's Go」)。 – geocodezip

+0

我添加了html – Thayne

回答

0

它看起來像緯度和經度座標是不正確的格式in property origin: not a string; and not a LatLng or LatLngLiteral: not an Object; and not an ObjectAbUncaught TypeError: Cannot read property 'N' of null意味着你的價值之一是進入你的函數爲null或沒有任何東西。如果可以,發佈一些HTML。如果你不想..我會console.log這個目標值是在這個代碼運行之前。

directionsService.route({ 
      origin: currentPosition, //<------ 
      destination: document.getElementById('toAddress').value, //<------ 
      travelMode: google.maps.TravelMode.DRIVING 
      }, function(response, status) { 
      if (status === google.maps.DirectionsStatus.OK) { 
       directionsDisplay.setDirections(response); 
      } else { 
       window.alert('Directions request failed due to ' + status); 
      } 
      }); 

此外,請檢查返回的當前位置值可能是您的geolocator關閉。如果是這樣,它將解釋不能讀取屬性'N'你得到的空錯誤。

這可能不是你正在尋找的答案,但也許這將有助於一些調試。發佈你的html的簡化版本。

祝你好運!

+0

添加了html。 – Thayne

0

這個錯誤會來,當你navigator.geolocation失敗,在這種情況下,你不會有currentPosition一套具有經緯度文字如此以來,起源是unknown方向API會給錯誤。