2013-10-29 59 views
2

我陷入了一種情況,我需要在爲我的Google Maps v3獲取Jsonresult時修復代碼,但是當我在循環中從jquery中加載我的地​​圖時,這個錯誤顯示Uncaught TypeError: Cannot call method 'toLowerCase' of undefined我不知道如何解決這個問題,我沒有看到這個問題的一些燈具。我希望有人能幫助我解決這個不能調用未定義的方法'toLowerCase'GetJson

這是我爲我的javascript代碼:

<script type="text/javascript"> 
    var geocoder; 
    var map; 

    function initialize() { 
     var minZoomLevel = 4; 
     var zooms = 7; 
     geocoder = new google.maps.Geocoder(); 

     map = new google.maps.Map(document.getElementById('map'), { 
      zoom: minZoomLevel, 
      center: new google.maps.LatLng(38.50, -90.50), 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }); 

     // Bounds for North America 
     var strictBounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(15.70, -160.50), 
    new google.maps.LatLng(68.85, -55.90) 
    ); 

     // Listen for the dragend event 
     google.maps.event.addListener(map, 'dragend', function() { 
      if (strictBounds.contains(map.getCenter())) return; 

      // We're out of bounds - Move the map back within the bounds 

      var c = map.getCenter(), 
     x = c.lng(), 
     y = c.lat(), 
     maxX = strictBounds.getNorthEast().lng(), 
     maxY = strictBounds.getNorthEast().lat(), 
     minX = strictBounds.getSouthWest().lng(), 
     minY = strictBounds.getSouthWest().lat(); 

      if (x < minX) x = minX; 
      if (x > maxX) x = maxX; 
      if (y < minY) y = minY; 
      if (y > maxY) y = maxY; 

      map.setCenter(new google.maps.LatLng(y, x)); 
     }); 


     // Limit the zoom level 
     google.maps.event.addListener(map, 'zoom_changed', function() { 
      if (map.getZoom() < minZoomLevel) map.setZoom(minZoomLevel); 
     }); 


    } 
    var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/'; 
    function codeAddress() { 
     $.getJSON("/Dashboard/LoadWorkerList", function (address) { 
      var infowindow = new google.maps.InfoWindow(); 
      $.each(address,function (index,currVal) { 
       currVal = $(this).val(); 
       geocoder.geocode({ 'address': currVal }, function (results, status) { 
        if (status == google.maps.GeocoderStatus.OK) { 

         map.setCenter(results[0].geometry.location); 
         var marker = new google.maps.Marker({ 
          map: map, 
          icon: iconBase + 'man.png', 
          position: results[0].geometry.location, 
          title: currVal 
         }) 


         google.maps.event.addListener(marker, 'click', (function (marker, i) { 
          return function() { 
           infowindow.setContent(currVal); 
           infowindow.open(map, marker); 
          } 
         })(marker, currVal)); 
         address.push(marker); 
        } 
        else { 
         alert("Geocode was not successful for the following reason: " + status); 
        } 
       }); 
      }); 
     }); 
     return true; 
    } 

    window.onload = function() { 
     initialize(); 
     codeAddress(); 
    } 
</script> 

這是LoadWorkerList

public JsonResult LoadWorkerList() 
     { 
      var workerList = new List<Worker_Address>(); 

      // check if search string has value 
      // retrieve list of workers filtered by search criteria 
      var list = (from a in db.Worker_Address 
         where a.LogicalDelete == false 
         select a).ToList(); 



      List<WorkerAddressInfo> wlist = new List<WorkerAddressInfo>(); 
      foreach (var row in list) 
      { 
       WorkerAddressInfo ci = new WorkerAddressInfo 
       { 
        ID = row.ID, 
        Worker_ID = row.WorkerID, 
        AddressLine1 = row.Address_Line1 + " " + row.Address_Line2+ " " +row.City + " "+ GetLookupDisplayValById(row.State_LookID), 
        LogicalDelete = row.LogicalDelete 

       }; 
       wlist.Add(ci); 
      } 


      return Json(wlist.ToList().OrderBy(p => p.AddressLine1), JsonRequestBehavior.AllowGet); 
     } 

代碼中的錯誤是在這個FF:代碼

Uncaught TypeError: Cannot call method 'toLowerCase' of undefined 
v.fn.extend.val       jquery-1.8.3.min.js:2 
(anonymous function)    $.each(address,function (index,currVal) { 
v.extend.each       jquery-1.8.3.min.js:2 
(anonymous function)    currVal = $(this).val(); 
l         jquery-1.8.3.min.js:2 
c.fireWith      jquery-1.8.3.min.js:2 
T         jquery-1.8.3.min.js:2 
r 
+0

哪些瀏覽器您使用的測試?在鉻中檢查元素,並在該錯誤顯示的行上應該有一個箭頭在左側,單擊該箭頭以展開堆棧跟蹤 – pythonian29033

+0

將堆棧跟蹤粘貼到此處作爲問題的一部分,即如果您無法讀取堆棧跟蹤,否則,這應該告訴你究竟在你的代碼中你錯了什麼 – pythonian29033

+0

@ pythonian29033添加stacktrace。 (#) –

回答

0

好吧,從堆棧跟蹤來看,它是這樣說的; - 在currVal = $(this).val();中的$(this).val()返回的值在某個索引處未定義,或者 - address$.each(address,function (index,currVal)中的其中一個索引的值未定義。

確保在$.each(address,function (index,currVal) {索引值不超越的address長度和在address所有元素被分配一個值

我猜你試圖做一個地址地理編碼數組address,它試圖使地址成爲lowerCase字符串,但沒有找到字符串。

如果address不是一個數組,使它成爲一個陣列,它不應該是一個字符串

+0

謝謝,但你能幫我嗎? –

相關問題