2

我正在尝试根据数据库的结果生成谷歌地图。我可以对地址进行地理编码并在地图上显示,但我不能很快做到。我有一个 setTimeout 函数来帮助加载标记;如果我不包括它,那么并不是所有的标记都会加载。

我有什么办法可以快速推出标记?标记最终也将在其上具有 InfoWindows。注意:我使用的是 ColdFusion 和 SQL。这就是发生的事情。到目前为止,这是我的代码:

    <body onLoad="initialize()">

   <div id="map_canvas" class="grid_12">
   </div>

</div> 
<!end .container_12> 

</body>

<script type="text/javascript"> 

function initialize(){
    // Prepare the array from ColdFusion and database
    var locations = [ 
    <cfset locationArray=ArrayNew(1)>
        <cfloop query="GetLocations">
            <cfscript>
                ArrayAppend(locationArray, #Client_Address# & ' ' & #Client_City# & ' ' & #Client_State# & ' ' & #Client_Company#);
            </cfscript>
            '<cfoutput>#Client_Address# #Client_City# #Client_State#</cfoutput>',
        </cfloop>

    ];

    //Set options of the google map
    var mapOpt = { 
       mapTypeId: google.maps.MapTypeId.ROADMAP,
       center: new google.maps.LatLng(42.48019996901214, -90.670166015625),
       zoom: 8
    };

    //Create new map
    var map = new google.maps.Map(document.getElementById("map_canvas"), mapOpt);

    var geocoder = new google.maps.Geocoder();
    var index = 0;

    //Begin geocoding function converting addresses to LatLng
    var geocoderFunction = function () { 
       geocoder.geocode({ 'address': locations[index] }, function (results, status) {

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

             new google.maps.Marker({
                map: map, 
                position: results[0].geometry.location,
                title: ''
             }); 

          }

        // Call the geocoder with a 150ms delay
          index++;
          if (locations.length > index) {
             setTimeout(geocoderFunction, 150);

          }

       });

    }

    // Launch the geocoding process
    geocoderFunction();

}
</script> 
</html>

我对此很陌生,所以任何帮助将不胜感激!

4

1 回答 1

3

地理编码可能是缓慢的部分。您可以提前进行地理编码,将纬度和经度存储在数据库中,然后在制图时将标记推送到带有经度和经度的地图上吗?

于 2010-08-17T19:01:25.217 回答