类型错误:a为null main.js

时间:2015-08-04 20:48:41

标签: javascript google-maps google-maps-api-3

所以我正在使用谷歌地图api,它给了我这个错误

InvalidValueError: not an Object" main.js:26:873
Bf() main.js:26
Xca() main.js line 61 > eval line 39 > eval line 30 > eval:8
Wca() main.js line 61 > eval line 39 > eval line 30 > eval:5
a_[u].Bj/<() main.js line 61 > eval line 39 > eval line 30 > eval:11
kg() main.js:30
a_[u].Bj() main.js line 61 > eval line 39 > eval line 30 > eval:11
Bi[u].route/<() main.js:45
lg() main.js:30
<anonymous> main.js line 61 > eval line 39 > eval line 30 > eval:55
Nh.util() main.js line 61 > eval line 39 > eval:30
Oh/<() main.js:41
dg[u].Qd/</d[a]<() main.js:29
$f/<() main.js:29
dg[u].Qd/<() main.js:29
hg() main.js:30
dg[u].Qd() main.js:29
<anonymous> directions.js:1
TypeError: a is null

这是我的代码:

    var directionsDisplay;
    var directionsService = new google.maps.DirectionsService();
    var map;
    var waypts=[];
    var setpos = false;
    var pos;
    var start;
    var posMarker;
    var mapPage=0;
    var request;
    function initialize() {
      console.log('started');
      directionsDisplay = new google.maps.DirectionsRenderer();
      var mapOptions = {
        zoom: 6
      };
      var chicago = new google.maps.LatLng(41.850033, -87.6500523);
      map = new google.maps.Map(document.getElementById('map-canvas'),
      mapOptions);
      if(navigator.geolocation) {
          navigator.geolocation.getCurrentPosition(function(position) {
          pos = new google.maps.LatLng(position.coords.latitude,
          position.coords.longitude);

          var infowindow = new google.maps.InfoWindow({
            map: map,
            position: pos,
            content: 'Location found using HTML5.'
          });

          map.setCenter(pos);
          }, function() {
          handleNoGeolocation(true);
        });
      } 
      else {
        // Browser doesn't support Geolocation
        handleNoGeolocation(false);
      }
      map = new google.maps.Map(document.getElementById('map-canvas'),mapOptions);
      directionsDisplay.setMap(map);
    }

     function calcRoute() {
        var selectedMode = document.getElementById('mode').value;
        posMarker=null;

        if (setpos===false){
      start = document.getElementById('start').value;
        }
      var end = document.getElementById('end').value;
      if(selectedMode!== 'TRANSIT'){
        console.log('non-transit')
        var request = {
          origin: start,
          destination: end,
          waypoints: waypts,
          optimizeWaypoints: true,
          travelMode: google.maps.TravelMode[selectedMode]
         };
      }
      else if(mapPage===waypts.length+1){
        var request = {
          origin: waypts[mapPage-1],
          destination: end,
          travelMode: google.maps.TravelMode[selectedMode]
        };
      }
       else if(mapPage===1){
        var request = {
          origin: start,
          destination: waypts[mapPage+1],
          travelMode: google.maps.TravelMode[selectedMode]
        };
      }
      else {
        var request = {
           origin: waypts[mapPage-1],
           destination: waypts[mapPage+1],
           travelMode: google.maps.TravelMode[selectedMode]
        };
       }
     }

      directionsService.route(request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
          directionsDisplay.setDirections(response);
          var route = response.routes[0];
          var summaryPanel = document.getElementById('directions_panel');
          summaryPanel.innerHTML = '';
          // For each route, display summary information.
            for (var i = 0; i < route.legs.length; i++) {
              var routeSegment = i + 1;
              summaryPanel.innerHTML += '<b>Route Segment: ' + routeSegment + '</b><br>';
              summaryPanel.innerHTML += route.legs[i].start_address + ' to ';
              summaryPanel.innerHTML += route.legs[i].end_address + '<br>';
              summaryPanel.innerHTML += route.legs[i].distance.text + '<br><br>';
             }
          }
         });

      function changeText(elem,text){
        var element=elem;
        element = text;
    }
    function addWay(){
          waypts.push({location:document.getElementById('waypoints').value,stopover:true});
        alert("added: " + document.getElementById('waypoints').value);
        for(var i = 0; i<waypts.length;i++){
            console.log(waypts[i]);
        }
        console.log("worked THANK BEJEEBUS");
    }
    function setCurrentPos(){
        setpos=!setpos;
          if(setpos===true){
              start = pos;
          }
          else changeText(document.getElementById('start').text,'Not your current position');
        console.log(start);
        console.log("pos set");
        posMarker = new google.maps.Marker({
          position: pos,
          map: map,
          title: 'Your Pos'
        });
    }
    function nextPage(){
        if(mapPage<=waypts.length+1)
          mapPage++;
          calcRoute();
    }
    function backPage(){
        if(mapPage>1){
        mapPage--;
        calcRoute();
        }
    }
    google.maps.event.addDomListener(window, 'load', initialize);

它之前没有这样做,但是当我在计算路线中的代码中添加了传输方式时,它全部破了。

1 个答案:

答案 0 :(得分:0)

directionsService.route的调用移至末尾,但在calcRoute内。

目前它位于calcRoute之外的全局范围内,其中request未知(我猜你错误地混淆了一些括号)