2012-03-06 213 views
1

我有一个在asp.net中隐藏代码的脚本。它工作正常,但只显示了200个地图上的前11个标记。它引发错误,说'0'为空或不是对象为第12个地址。即使我删除了第12个地址,它仍然表现相同。数据库中的所有物理地址也是有效的。我有200个这样的地址。我正在使用google map api v3。预先感谢您的帮助。下面是我用来构建脚本的方法:谷歌地图标记不显示

private void MapScript(DataSet ds) 
    { 
     StringBuilder sb = new StringBuilder(); 
     string itemList = string.Empty; 
     foreach (DataRow r in ds.Tables[0].Rows) 
     { 
      // bypass empty rows   
      if (r["Address"].ToString().Trim().Length == 0) 
       continue; 
      sb = sb.Append("'" + r["Address"].ToString().Trim() + "',");    
     } 
     if (sb.Length > 0) 
     { 
      itemList = sb.ToString().Substring(0, sb.ToString().Length - 1); 
      itemList = itemList.Replace("\n", " "); 
     } 
     //script 
     script.Text = @"<script type='text/javascript'>        
         window.onload = function() { 
          var temp = [" + itemList + @"]; 
          var latlng = new google.maps.LatLng(32.802955, -96.769923); 
          var options = { 
          zoom: 10, 
          center: latlng, 
          mapTypeId: google.maps.MapTypeId.ROADMAP 
          }; 
          var map = new google.maps.Map(document.getElementById('map_canvas'), options);  
          var geocoder = new google.maps.Geocoder(); 

          for (var i = 0; i < temp.length; i++) { 
          (function(address) { 
           geocoder.geocode({ 
            'address': address 
           }, function(results) { 
            var marker = new google.maps.Marker({ 
             map: map, 
             position: results[0].geometry.location, 
             title: address 
            });          
           }); 
          })(temp[i]); 
          } 
         }       
         </script> ";    
    } 
+1

做任何一个地址有'''在其中?例如,“奥黑尔机场”或类似的? – AakashM 2012-03-06 17:22:52

+0

我没有任何地址中的任何单引号。 – antar 2012-03-06 17:40:40

+1

我们需要看到'temp'来回答这个问题。 – 2012-03-06 20:10:25

回答

1

你所得到的错误'0' is null or not an object,因为你没有检查请求的状态您尝试设置标记的位置属性之前。就像@david说的那样,返回的状态可能是OVER_QUERY_LIMIT

这是我在测试过程中处理过的。基本上,它会检查返回的状态,如果状态超出限制,它将等待200毫秒并重试。我只在测试环境中使用过,因为客户端有一个不受限制的首要帐户。 200毫秒似乎是目前的甜蜜点,但谷歌已经改变了过去有多少请求可以进行的限制。

Here is a fiddle of it working

function Geocode(address) { 
    geocoder.geocode({ 
     'address': address 
    }, function(results, status) { 
     if (status === google.maps.GeocoderStatus.OK) { 
      //update finished count so we know which markers are done. 
      finishedCount++; 
      UpdateProgress(); 
      var result = results[0].geometry.location; 
      var marker = new google.maps.Marker({ 
       position: result, 
       map: map 
      });  
      bounds.extend(marker.position); 
     } else if (status === google.maps.GeocoderStatus.OVER_QUERY_LIMIT) { 
      setTimeout(function(){ 
       //retry 
       Geocode(address); 
      }, 200); 
     }else{ 
      alert("Geocode was not successful for the following reason: " 
       + status); 
     } 
    }); 
} 

function UpdateProgress(){   
    var progress = Math.round((finishedCount/citiesInTexas.length) * 100); 
    $('#progress').text("percent done: " + progress); 

    if(progress === 100) 
     map.fitBounds(bounds); 
} 
+0

感谢Bryan,至少我现在可以在TEST中看到他们全部:) – antar 2012-03-06 23:36:32