In the use of Baidu API to query the latitude and longitude of multiple addresses, because the Baidu API provides latitude and longitude Query method is a callback function, and subsequent operations must be achieved by latitude to complete to proceed, the problem exists in how to judge whether all the places are callback completed, Asked a front-end big-brother colleague (I am a front-end weak chicken), give me the idea, in the Baidu API callback function to adjust the custom callback function, so that in the custom function inside to determine whether all locations are callback completed.
Here is the specific code,
1<script>2 //all locations to be queried for latitude and longitude3 varalladdress=NewArray ("Beijing West railway Station", "Chengdu East railway Station", "Shanghai South Railway station", "Xi ' an north station");4 //callback pre-count with5 varCallbackbefore = 0;6 //callback after count with7 varCallbackafter = 0;8 //latitude and longitude of storage9 vargeocoord={};Ten //Loop all addresses One for(vari = 0; i < alladdress.length; i++) { A //first determine if you have already queried the latitude and longitude (row weight) - if(! (Alladdress[i]inchGeocoord)) { - //to adjust Baidu API, check the latitude and longitude, first remember the number of thecallbackbefore++; -GetPoint (Alladdress[i],function(address, point) { - //callback back, latitude, then count -callbackafter++; +Geocoord[address] =Point ; - //determines whether the number before the callback is the same as the number after the callback, which means that all addresses are queried . + if(Callbackbefore = =callbackafter) { A //to do what you want to do . at //Travel (Geocoord); - return; - } - }); - } - in } - //functions for querying latitude and longitude to functionGetPoint (address, callback) { + varLocal =NewBmap.localsearch (Address, - { the //Smart Search, which is a callback method *"Onsearchcomplete":function(obj) { $ //latitude and longitude of storagePanax Notoginseng varPoint = []; - if(obj && obj.getpoi (0)) { the varpp = obj.getpoi (0). Point; + Point.push (PP.LNG); A Point.push (Pp.lat); the //callback Method + callback (address, point); - } $ } $ }); - Local.search (address); - } the</script>
The fact is that the address alladdress is more complex and requires two cycles, not as simple as the example above, so this method is used.
Baidu API Query the latitude and longitude of multiple address issues