2013-09-23 153 views
-2

嘗試訪問函數中數組的索引值。該數組有4條記錄,但只有索引1 - 3值正在訪問數組中的最後一個值沒有被訪問,只有3個標記正在創建。 jquery拋出TypeError:a.eb未定義TypeError:b未定義。數組變量是global_citizens。jquery訪問數組索引

誤差在MakeRouteCallBack功能在

marker[rNum] = createMarker(legs[i].start_location,"start",legs[i].start_address,ctype); 

控制檯打印結果發生

sdfsd sfsdf 
fsdf sdfsdf 
john wells 
sdfdf dfrre 

10.58909,-61.13388800000001-10.5089259,-61.3822726,2 

10.66753800802045,-61.51776409679476-11.3165044,-60.53128919999999,3 

10.67278501519437,-61.51673537546543-10.2690277,-61.3422372,2 

10.67107578209723,-61.51689561536466-10.65,-61.4,4 

Citizen icon is 2 
Citizen icon is 3 
Citizen icon is 4 

TypeError: a.eb is undefined....... 

代碼:

創建標記

function createMarker(latlng, label, html,citizenType) { 

    var contentString = '<b>'+label+'</b><br>'+html; 

    console.log('Citizen icon is '+citizenType);  

    //replaces if...then..else...else if. Icon source are placed in an array that corresponds to value of the citizenType. 
    //array starts from 0 . The first value of the citizens type is 2 so array should have a value at the 2 location 
    //so jquery will map the icon to the citizenType based on its matching location in the array 

     var markerSource = [null, 
         'resources/icons/criminal_new.ico', 
         'resources/icons/victim_new.ico', 
         'resources/icons/suspect_new.ico']; 


    var icon = markerSource[citizenType]; 

     var marker = new google.maps.Marker({ 
      position: latlng, 
      map: map, 
      title: label, 
      icon: new google.maps.MarkerImage(icon), 
      zIndex: Math.round(latlng.lat()*-100000)<<5 
      }); 
      marker.myname = label;   

     google.maps.event.addListener(marker, 'click', function() { 
      infowindow.setContent(contentString); 
      infowindow.open(map,marker); 
      }); 
     return marker; 
    } 

路由集

function setRoutes(){  

     var directionsDisplay = new Array(); 

     for (var i=0; i< global_citizens.length; i++){ 

     var rendererOptions = { 
      map: map, 
      suppressMarkers : true, 
      preserveViewport: true 
     } 
     directionsService = new google.maps.DirectionsService(); 

     var travelMode = google.maps.DirectionsTravelMode.DRIVING; 

     console.log(global_citizens[i].startLat+','+global_citizens[i].startLng+'-'+global_citizens[i].endLat+','+global_citizens[i].endLng+','+global_citizens[i].citizenType); 

     var request = { 
      origin: global_citizens[i].startLat+','+global_citizens[i].startLng, 
      destination: global_citizens[i].endLat+','+global_citizens[i].endLng, 
      travelMode: travelMode 
     }; 

      directionsService.route(request,makeRouteCallback(i,directionsDisplay[i],global_citizens[i].citizenType)); 

     } 

MakeRouteCallBack

function makeRouteCallback(rNum, disp,ctype){ 
    if (polyline[rNum] && (polyline[rNum].getMap() != null)) { 
      startAnimation(rNum); 
      return; 
      } 

     return function(response, status){  

      if (status == google.maps.DirectionsStatus.OK){ 

      var bounds = new google.maps.LatLngBounds(); 
      var route = response.routes[0]; 
      startLocation[rNum] = new Object(); 
      endLocation[rNum] = new Object(); 


      polyline[rNum] = new google.maps.Polyline({ 
      path: [], 
      strokeColor: '#FFFF00', 
      strokeWeight: 3 
      }); 

      poly2[rNum] = new google.maps.Polyline({ 
      path: [], 
      strokeColor: '#FFFF00', 
      strokeWeight: 3 
      });  


      // For each route, display summary information. 
      var path = response.routes[0].overview_path; 
      var legs = response.routes[0].legs; 

      disp = new google.maps.DirectionsRenderer(rendererOptions);  
      disp.setMap(map); 
      disp.setDirections(response); 


      //Markers    
      for (i=0;i<legs.length;i++) { 
       if (i == 0) { 
       startLocation[rNum].latlng = legs[i].start_location; 
       startLocation[rNum].address = legs[i].start_address; 
       // marker = google.maps.Marker({map:map,position: startLocation.latlng}); 


       //console.log('(i) The Citizen Type Is : '+ global_citizens[i].citizenType); 
       marker[rNum] = createMarker(legs[i].start_location,"start",legs[i].start_address,ctype);      

       } 

       endLocation[rNum].latlng = legs[i].end_location; 
       endLocation[rNum].address = legs[i].end_address; 
       var steps = legs[i].steps; 

       for (j=0;j<steps.length;j++) { 
       var nextSegment = steps[j].path;     
       var nextSegment = steps[j].path; 

       for (k=0;k<nextSegment.length;k++) { 
        polyline[rNum].getPath().push(nextSegment[k]); 
        //bounds.extend(nextSegment[k]);    


       } 

       } 
      }   
     } 

     polyline[rNum].setMap(map); 
     //map.fitBounds(bounds);   
     startAnimation(rNum); 

     } 

    } 

回答

1

你的代碼是不容易理解,但你的回調有時會返回undefined,有時一個功能,也許這會導致TypeError,因爲你可能會返回undefined whe ñAPI預計功能...這只是一個猜測雖然...

索引到數組是錯誤的,你的變量我採取值從1 - 3,當我成爲4你索引的數組外,因此你得到了一個TypeError,索引到數組應該從0開始。

+0

它工作數組索引1 - 3,但不是數組中的最後一個記錄值。當處理達到jquery拋出錯誤時的最後一條記錄。 – devdar

+1

您的陣列中有多少個元素?發生錯誤時,我的價值是多少? –

+0

我在數組中有4個元素,當出現錯誤時,我的值是4 – devdar