0

I am doing LBS application which requires to include this outside map source to get location's address:

<script type="text/javascript"
        src="https://mapapi.qq.com/web/mapComponents/geoLocation/xxxx.js"></script>

and there is my code:

    <script>
        var geolocation = new qq.maps.Geolocation("xxx-xdss-ddf-xx-xxx-xxxx", "myapp");
    
        var showLocation; // the global variable
    
        window.onload = function () {
            //use getLocation method
            geolocation.getLocation(getCityLocation, showErr, options)
    
            //callback function
            function getCityLocation(loc) {
                console.log(loc);
               // print address correctly like:{address:xxx}
                showLocation = loc;
    
            }
            function showErr() {
    
            }
            var options = 4000;

            console.log(showLocation);
       // print undefined 
    
        }
    
    
    </script>

how do I get the return address to the variable showLocation? I know is async ,I have read some answers about javascript async solution but how could I use something like await, then ,promise in this case? since I could not modify the outside source. please let me know what can I do, thanks!

well here is my answer after stack overflow close my question: var geolocation = new qq.maps.Geolocation("xxxxxxxxxx", "myapp");

    let getLoc = new Promise((resolve, reject) => {
        geolocation.getLocation(good, bad, options)
       

        function good(p) {
            console.log(p);
            return resolve(p)

        }
        function bad(p) {
            return reject(p)
        }
        var options = 4000;


    })

    getLoc.then(
        res => {
            console.log(res);
            //correctly print out the address!
        }
    ).catch(
        res => {
            console.log(res);
        }
    )

0 Answers0