Baidu API Query the latitude and longitude of multiple address issues

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.