Google Maps API最快的路线

时间:2015-11-03 00:45:02

标签: javascript api google-maps maps

这是我的地图代码。 但是当你运行它时,一切都很好。然后,如果你点击“我们去吧!”你得到这个错误:
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:7

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);
            }
          });

        }

2 个答案:

答案 0 :(得分:0)

看起来lat和lng坐标的格式不正确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);
            }
          });

同时检查返回的当前位置值可能是您的地理定位器已关闭。如果是,它会解释无法阅读的财产&#39; N&#39;你得到的null错误。

它可能不是您正在寻找的答案,但也许这将有助于一些调试。发布你的html的简化版本。

祝你好运!

答案 1 :(得分:0)

当您的navigator.geolocation失败时会出现此错误,在这种情况下,您将不会使用latlng文字设置currentPosition,因此API方向会产生错误,因为原点为{{1 }}