2012-11-24 48 views
1

在我的Asp.net Web應用程序中,我使用setTimeout擺脫 地理編碼器OVER_QUERY_LIMIT,較短的超時是10ms,這對我來說太長,我有800以上的地址來自SQL SERVER,由於setTimeout會增加,因此需要大約5到7個造幣才能在地圖上佔據所有標記的位置,這令人沮喪。我研究過,看到這個鏈接setTimeout: how to get the shortest delay更好的方法,然後setTimeout谷歌地圖V3

但無法弄清楚他想要實際做什麼。請人指導我....

function InitializeMap() {  
    // Here am calling the webService by PageMethods in which CityNames, Countries Name will take their places 
    PageMethods.GetCitiesbyUser_Extender(onSucess); 

    var myOptions = 
    { 
     zoom: 0, 
     center: new google.maps.LatLng(-34.397, 150.644), 
     mapTypeId: google.maps.MapTypeId.ROADMAP, 

    }; 
    var map = new google.maps.Map(document.getElementById("map"), myOptions); 
    // Creating latlngbound to bound the markers on map 
    var bounds = new google.maps.LatLngBounds(); 

    //// Creating an array that will contain the addresses 
    var places = []; 
    // Creating a variable that will hold the InfoWindow object 
    var infowindow; 
    // create this to add the marker Cluster on map 
    mc = new MarkerClusterer(map); 
    var popup_content = []; 
    var geocoder = new google.maps.Geocoder(); 
    // image for ballon i want to change default ballon to this 
    var iconimage = "http://chart.apis.google.com/chart?cht=mm&chs=24x32&chco=FFFFFF,008CFF,000000&ext=.png";   
    var markers = []; 

    // Create this function for passing the values which was taken by webservice cntName is the return in webservice 
    function onSucess(cntName){ 
    // loop through the cntName to pass the individual City one by one from geocode 
     for (i = 0; i < cntName.length; ++i) { 
       //for fixing the issue use closure to localize the cntName[i] variable before passing into geocode and callback function within it. 
      (function CreateMarkAndInfo(address) { 
       geocoder.geocode({ 'address': address }, 
        function (results, status) { 
         if (status == google.maps.GeocoderStatus.OK) { 
          places[i] = results[0].geometry.location; 

          var marker = new google.maps.Marker({ 
           position: places[i], 
           title: results[0].formatted_address, 
           map: map, 
           icon: iconimage 
          }); 

          markers.push(marker); 
          mc.addMarker(marker); 
          google.maps.event.addListener(marker, 'click', function() { 
           if (!infowindow) { 
            infowindow = new google.maps.InfoWindow(); 
           } 

           // Setting the content of the InfoWindow afterward 
           infowindow.setContent(popup_content[i]); 

           // Tying the InfoWindow to the marker afterward 
           infowindow.open(map, marker); 
          }); 

          // Extending the bounds object with each LatLng 
          bounds.extend(places[i]); 

          // Adjusting the map to new bounding box 
          map.fitBounds(bounds); 
          // Zoom out after fitBound 
          var listener = google.maps.event.addListenerOnce(map, "idle", function() { 
           if (map.getZoom() < 10) map.setZoom(2); 

          }); 
         } 
         else { 
          // if geocode will end the limit then make delay by timer in order to avoid the OVER_QUERY_LIMIT 
          if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) { 
           setTimeout(function() { CreateMarkAndInfo(address); }, (15)); // here i think i should use better approch but for now it`s ok.         
          } 

          else { 
           alert("Geocode was not successful for the following reason: " + status); 
          } 
         }        
        }); 

      })(cntName[i]);// End closure trick  
     } 
    } 
} 
google.maps.event.addDomListener(window, 'load', InitializeMap); 

編輯: @ just.another.programmer我不能因爲在DB沒有latitute和經度,客戶端將通過他增加城市和國家的自我這就是爲什麼我不得不convet通過地理編碼和地理編碼的城市和國家的名稱accuretly在這裏做它`工作 我是多麼調用由Web服務

[System.Web.Services.WebMethod] 
[System.Web.Script.Services.ScriptMethod()] 
    public static string[] GetCitiesbyUser_Extender() 
    {  
     System.Data.DataSet dtst = new System.Data.DataSet(); 
     string ses = HttpContext.Current.Session["UserName"].ToString(); 
     USTER.Dal.clsSearch clssearch = new USTER.Dal.clsSearch(); 
     // Assinging the Stroed Procedure Method to DataSet 
     dtst = clssearch.GetAllCitiesByUser(ses); 
     string[] cntName = new string[dtst.Tables[0].Rows.Count]; 
     int i = 0; 
     try 
     { 
      foreach (System.Data.DataRow rdr in dtst.Tables[0].Rows) 
      { 
       // Columns Name in SQL Server Table "CityName" and "CountryName" 
       cntName.SetValue(rdr["CityName"].ToString() +","+ rdr["CountryName"].ToString() , i); 
       i++; 
      } 
     } 
     catch { } 
     finally 
     { 

     } 
     return cntName; 
    } 
+0

http://www.webstutorial.com/google-server-side-geocoding-php-infobox/website-tweaks/google – Andreas

回答

0

城市和國名地理編碼地址中的一個時間,當你第一次得到他們,然後將緯度/長度存儲在你的分貝中,這樣你就不必進行地理編碼再次。這將大大地減少您的地理編碼請求,並刪除需要setTimeout

+0

我不能因爲生產服務器中的數據庫而不允許編輯數據庫表,但只能創建proc和函數 –

+0

甜言蜜語讓你的dba改變你的桌子。你真的不想對每個使用你的網站的人重新地理編碼相同的數據。 –

+0

你是對的我真的不希望每個人都會根據用戶角色動態更改相同的數據,你沒有看到我傳入WebService的「UserName」會話嗎? –