Mobile terminal city positioning, city area code adcode

Time:2020-10-23

Using the goto positioning API:

  1. AMap.Map(‘iCenter’)
  2. AMap.CitySearch()

First, register the key to apply for location permission in Gaode open platform. Website; Gaode open platform

The script with positioning key is introduced into the page to be located

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    < title > basic introduction presentation
    <script></script>

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

</body>
</html>

Using goto positioning API

Demand: Apple devices use IP positioning, others use GPS positioning

function getlocation() {


  
            var u = navigator.userAgent;

            var ua = navigator.userAgent.toLowerCase();

            Var isaandroid = u.indexof ('android ') > - 1 | u.indexof ('adr') > - 1; // Android terminal
            Var isios =!! u.match (/ \ (I [^;] +; (U;)? CPU. + Mac OS X /); // IOS terminal


            //GPS positioning
            function locationForGPS() {
            /*********Tracking mark**********/ console.log ('gps positioning start + + ');
                var map, geolocation;
                //Load the map and call the browser location service
                map = new AMap.Map('iCenter');
                map.plugin('AMap.Geolocation', function() {
                    geolocation = new AMap.Geolocation({
                        Enable highaccuracy: false, // whether to use high precision positioning, default: true
                        Timeout: 10000, // stop positioning after more than 10 seconds. Default: infinity
                    });
                    map.addControl(geolocation);
                    geolocation.getCurrentPosition();
                    
                    //Getcityinfo is a method of GPS positioning in Gaode. It returns IP positioning data first. Because IP positioning may not be accurate, GPS data is used for correction later
                    //
                    geolocation.getCityInfo(function(status,result){
                    /*********Tracking mark**********/ console.log ('Start IP positioning synchronization with GPS, and write data to + + + ');
                        if (status === 'complete' && result.info === 'SUCCESS') {
                    /*********Tracking mark**********/ console.log ('synchronize IP location successfully, write data to + + + ');
                            if(isNull(sessionStorage.getItem('autouserchooselocationct'))){
                                /*********Tracking mark**********/ console.log ('No detection, check the last positioning data, write the data to + + + ');
                                let cityAdcode=result.adcode;
                                let cityName=result.city;
                                
                                sessionStorage.setItem('autouserchooselocationct', cityName);
                                sessionStorage.setItem('autouserchooselocationcode', cityAdcode);

                           
                                 /*********Tracking mark**********/ console.log ('end of synchronization IP positioning, completion of data writing + + + ');
                            }
                        }
                    });


                    AMap.event.addListener (geolocation, 'complete', oncomplete); // returns location information
                    AMap.event.addListener (geolocation, 'error', onerror); // the location error is returned




                });

                //GPS positioning成功
                function onComplete(data) {
                    console.log(data)
                     /*********Tracking mark**********/ console.log ('gps positioning start + + ');
                     //GSP positioning is accurate to the District, and the returned adcode is accurate to the area under the city,
                     //If you want to get the abcode of a city, you need to use another API of Golder, city region query
                   
                    let cityName=data.addressComponent.city;

                    let geocoder = new AMap.Geocoder({});
                    //Geocoding, return geocoding results,
                    
                    geocoder.getLocation(cityName, function(status, result) {
                        if (status === 'complete' && result.info === 'OK') {
                        /*********Tracking mark**********/ console.log ('gps positioning successful, processing positioning data + + + ');

                        /*********Tracking mark**********/ console.log ('write the data to cover the synchronized IP data + + + ');
                                let cityAdcode=result.geocodes[0].adcode;
                                sessionStorage.setItem('autouserchooselocationct', cityName);
                                sessionStorage.setItem('autouserchooselocationcode', cityAdcode);
                     
                               /*********Tracking mark**********/ console.log ('gps positioning is finished, data writing is completed + + + ');
                            }


                    });

                }

                //GPS positioning失败
                function onError() {
                    /*********Tracking mark**********/ console.log ('gps positioning failure, start to enable IP positioning + + + ');
                    locationForIp(true);
                     /*********Tracking mark**********/ console.log ('gps-ip++...')
                }

            }

            //IP positioning
            function locationForIp(tap) {
                 /*********Trace mark ≡ / (TAP){ console.log ('gps positioning failure, start to enable IP positioning + + + ');}
                     /*********Tracking mark**********/ console.log ('Ip location start + + + ');
                var citysearch = new AMap.CitySearch();
                //Automatically get user IP and return to current city
                citysearch.getLocalCity(function (status, result) {
                    if (status === 'complete' && result.info === 'OK') {
                        if (result && result.city && result.bounds) {
                            let GetUserLocation = result.city,
                                GetUserLocationcode = result.adcode;
                                /*********Tracking mark**********/ console.log ('Ip location is successful, start to check whether there is the last location data + + + ');
                            if(isNull(sessionStorage.getItem('autouserchooselocationct'))){
                             /*********Tracking mark**********/ console.log ('Ip positioning is successful, the last positioning data is not detected and checked, and the data is written to + + + ');
                                sessionStorage.setItem('autouserchooselocationct', GetUserLocation);
                                sessionStorage.setItem('autouserchooselocationcode', GetUserLocationcode);

                     
                             /*********Tracking mark**********/ console.log ('Ip location is successful, data writing ends + + + ');
                            }
                        }
                    } else {

                   
                 /*********Tracking mark**********/ console.log ('Ip location failed, data writing ended + + + ');
                    }

                })

            }

            if(isiOS){
                /*********Tracking mark**********/ console.log ('ios device enables IP location ');
                locationForIp(false);

            }else {
                /*********Tracking mark**********/ console.log ('GPS positioning is enabled for non IOS devices');
                locationForGPS()
            }

        }


};