谷歌地图V3:只有一些标记显示谷歌地图V3:只有一些标记显示(Google Map V3 : Onl

2019-05-12 11:10发布

我有以下的代码,应该点击阵列中的每个标记的地方显示几个标记将显示一个信息窗口一切工作只是罚款了21个地方,我能够显示只有8标记。

// declare Variables
   var geocoder;
   var map;
   var tex;     
   var markersArray = [];
   // pids array 21 addreses
    var pids = [{ad:'251 Pantigo Road Hampton Bays NY 11946',pid:'9'},
    {ad:'Amagensett Quiogue NY 11978',pid:'10'},
    {ad:'789 Main Street Hampton Bays NY 11946',pid:'12'},
    {ad:'30 Abrahams Path Hampton Bays NY 11946',pid:'14'},
    {ad:'3 Winnebogue Ln Westhampton NY 11977',pid:'15'},
    {ad:'44 White Oak Lane Montauk NY 11954',pid:'16'},
    {ad:'107 stoney hill road Bridgehampton NY 11932',pid:'17'},
    {ad:'250 Pantigo Rd Hampton Bays NY 11946',pid:'19'},
    {ad:'250 Pantigo Rd Hampton Bays NY 11946',pid:'20'},
    {ad:'44 Woodruff Lane Wainscott NY 11975',pid:'21'},
    {ad:'Address East Hampton NY 11937',pid:'46'},
    {ad:'Address Amagansett NY 11930',pid:'49'},
    {ad:'Address Remsenburg NY 11960 ',pid:'50'},
    {ad:'Address Westhampton NY 11977',pid:'51'},
    {ad:'prop address Westhampton Dunes NY 11978',pid:'52'},
    {ad:'prop address East Hampton NY 11937',pid:'53'},
    {ad:'Address East Hampton NY 11937',pid:'58'},
    {ad:'Address Southampton NY 11968',pid:'59'},
    {ad:'Address Bridgehampton NY 11932',pid:'60'},
    {ad:'Address Sagaponack NY 11962',pid:'61'}];

    // create an MVCobject for creating Info window on marker
   var pin = new google.maps.MVCObject();
   // The content placeholder for the Info window. 
   var content = document.createElement("DIV");   
   var title = document.createElement("DIV");
   content.appendChild(title);
   // that is where you have the ajax result placed
   var streetview = document.createElement("DIV");
   streetview.style.width = "326px";
   streetview.style.height = "212px";
   content.appendChild(streetview);
   var infowindow = new google.maps.InfoWindow({
       content: content
   });
       // Initialize
   function initialize() {
       geocoder = new google.maps.Geocoder();
       var latlng = new google.maps.LatLng(40.8687097, -73.0014946);
       var myOptions = {
           zoom: 8,
           center: latlng,
           mapTypeId: google.maps.MapTypeId.ROADMAP
       }
       map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
       getAllPids();
   }
   // loop to create all markers
   function getAllPids() {
       var i;
      for (i = 0; i < pids.length; i++) {
           var test = pids[i];
           codeAddress(test);
       }        
   }
   // get latlng for each address , create marker  and add eventlistner to click open infowindow 
   function codeAddress(place) {
       geocoder.geocode({ 'address': place.ad }, function(results, status) {
           if (status == google.maps.GeocoderStatus.OK) {
               var marker = new google.maps.Marker({
                   map: map,
                   position: results[0].geometry.location,
                   title: place.pid
               });
               markersArray.push(marker);                  
               google.maps.event.addListener(marker, "click", function() {
                   openInfoWindow(marker);
               });
            } else {
               // alert("Geocode was not successful for the following reason: " + status);
           }
       });
   }
   // click event on marker calls this to show infowindow.
   function openInfoWindow(marker) {        
    getContent(marker.getTitle());          
       pin.set("position", marker.getPosition());
       infowindow.open(map, marker);
   }
   // Now ajax call to get the content for the current info window
   function getContent(pid) {          
      $.ajax({
                        type: "POST",
                        url: "mapSearchResult.aspx/get_map",                            
                        data: "{'pids':'"+pid +"'}",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function(msg) {

                         title.innerHTML =msg.d; 

                        }
                    });          
   }

Answer 1:

如果这个注释掉

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

你会发现, 原因代码是OVER_QUERY_LIMIT (620)。

有一个速度上限:如果您许多地理编码地址不引入它们之间的延迟,你会打破这种限制,因为你正在做在太短的时间太多了。

有些地址不会地理编码(或不会产生你想要的结果),因为地理编码器使用的邮政地址。 如地址东汉普顿NY 11937道具地址西安普敦沙丘NY 11978地址不邮政地址。

建议的策略是不浪费谷歌的,你与他人共享资源。 地理编码自己的地址,在一次性的操作,在数据库中存储的位置(或直接在代码中使用它们),然后使用的坐标位置标记。 不要刻意去寻找你已经知道每次页面加载时的位置。 仅使用地理编码找到你事先不知道的地方:通过用户输入的地址。

如果你真的必须颠覆这一战略和地理编码一切所有的时间,那么你需要慢下来的请求。 正如你提交更多的要求,你可能会慢下来,更满足了地理编码器。 我在做一个版本3例(由一位著名的版本2例) http://acleach.me.uk/gmaps/v3/plotaddresses.htm -你可以看到它开始了与请求之间的延时100ms,但需求经过二十迭代放慢到大约150毫秒。



文章来源: Google Map V3 : Only some markers are displayed