There is a problem with using Baidu map to obtain longitude and latitude according to multiple addresses. Brother and sister, take a look. I don't know what to do.

look at the picture

"" "" Promise
ddaa16fcecb34632b9c6c4f950c52f2a.jpg

post a copy of the source code:


< html lang= "en" >

<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="http://api.map.baidu.com/api?v=1.3"></script>

< / head >
< body >

<div id="container"></div>

< / body >
< script type= "text/javascript" >

var map = new BMap.Map("container");
var localSearch = new BMap.LocalSearch(map);
var address=["",""]
for(let i=0;i<address.length;iPP){
    fn(i,function (d){
        console.log(d)
    })
}
function fn(i,callback){
        var keyword = address[i];
        localSearch.setSearchCompleteCallback(
            function (searchResult) {
                var poi = searchResult.getPoi(0);
                var result = poi.point.lng + "," + poi.point.lat;
                callback(result)
            }
        );
        localSearch.search(keyword); 
    }

< / script >
< / html >


Don't save two names in your array. You save an object {Chongqing: []}, {Guiyang: []}, and find the corresponding assignment when assigning values, then you can't be wrong


has finally been solved. I have been looking for it for a long time. Thank you very much for the reply from my brother a few years ago. I did not expect that I could use

for

today.

just read the document and pass it directly into address

.
setSearchCompleteCallback () none sets the callback function after retrieval. If the parameter: results: LocalResult
or Array is retrieved by multiple keywords, the callback function parameter is the array of LocalResult, and the order of the results in the array is the same as that in the array of multiple keywords in retrieval

the following doesn't matter, unless you must have a query address

< hr > The return value of

setSearchCompleteCallback contains keyword , which corresponds to the passed address value, which allows you to establish a relationship with the incoming address
or execute the following code under chrome. The results correspond to the variables in address sequentially, but only one address request is sent at a time. The advantage is that the result corresponds to the value in the address variable

one by one.
address.reduce((a,addr)=>a.then(data=>new Promise((resolve, reject)=>{
    localSearch.setSearchCompleteCallback(searchResult=>{
        var poi = searchResult.getPoi(0);
        var result = poi.point.lng + "," + poi.point.lat;
        resolve([...data, result]);
    });
    localSearch.search(addr);
})), Promise.resolve([])).then(data=>console.log(data));
Menu