Baidu api queries longitude and latitude of multiple addresses

When using Baidu api to query the longitude and latitude of multiple addresses, because the longitude and latitude query method provided by Baidu api is a callback function, and subsequent operations must wait for the completion of longitude and latitude acquisition. The problem lies in how to judge whether all the locations have been callback completed. I asked a former front-end bigwig colleague (I am a front-end weak chicken) to provide me with Idea: in the callback function of Baidu api, call the user-defined callback function again, so as to judge whether all the callbacks are completed in the user-defined function.

Here is the code

 1 <script>
 2         //All locations of longitude and latitude to be inquired
 3         var allAddress=new Array("Beijing West Railway Station","Chengdu East Railway Station","Shanghai South Railway Station","XiAn North Railway Station");
 4         //For counting before callback
 5         var callbackBefore = 0;
 6         //For counting after callback
 7         var callbackAfter = 0;
 8         //Storage longitude and latitude
 9         var geoCoord={};
10         //Loop all addresses
11         for (var i = 0; i < allAddress.length; i++) {
12             //First, judge whether the longitude and latitude (weight arrangement) have been inquired
13             if (!(allAddress[i] in geoCoord)) {
14                 //To tune Baidu api,Check the latitude and longitude. First, count them
15                 callbackBefore++;
16                 GetPoint(allAddress[i], function (address, point) {
17                     //It's back. Latitude and longitude. Count again
18                     callbackAfter++;
19                     geoCoord[address] = point;
20                     //Determine whether the number before the callback is the same as the number after the callback, which means that all addresses have been queried
21                     if (callbackBefore == callbackAfter) {
22                         //Do what you want
23                         //Travel(geoCoord);
24                         return;
25                     }
26                 });
27             }
28             
29         }
30         //Function to query latitude and longitude
31         function GetPoint(address, callback) {
32             var local = new BMap.LocalSearch(address,
33             { 
34                 //Intelligent search, which is originally a callback method
35                 "onSearchComplete": function (obj) {
36                     //Storage longitude and latitude
37                     var point = [];
38                     if (obj && obj.getPoi(0)) {
39                         var pp = obj.getPoi(0).point;
40                         point.push(pp.lng);
41                         point.push(pp.lat);
42                         //Callback method
43                         callback(address, point);
44                     }
45                 }
46             });
47             local.search(address);
48         }
49 </script>

 

My actual situation is that the allAddress of that address is complex and requires a secondary loop, which is not as simple as the above example, so I used this method.

Tags: Javascript

Posted on Sat, 04 Apr 2020 02:28:22 -0700 by allex01