您好,登錄后才能下訂單哦!
怎么實(shí)現(xiàn)移動(dòng)端的城市定位以及城市區(qū)域代碼adcode?這個(gè)問(wèn)題可能是我們?nèi)粘W(xué)習(xí)或工作經(jīng)常見(jiàn)到的。希望通過(guò)這個(gè)問(wèn)題能讓你收獲頗深。下面是小編給大家?guī)?lái)的參考內(nèi)容,讓我們一起來(lái)看看吧!
使用高德定位API :
AMap.Map('iCenter')
AMap.CitySearch()
先在高德開(kāi)放平臺(tái)注冊(cè)申請(qǐng)定位權(quán)限的key。 網(wǎng)站;高德開(kāi)放平臺(tái)
在需要定位的頁(yè)面引入有定位key的script
<!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>基本引入展示</title> <script src="https://webapi.amap.com/maps?v=1.4.8&key=您申請(qǐng)的key值"></script> </head> <body> <p id="container"></p> </body> </html>
使用高德定位API
需求:蘋(píng)果設(shè)備使用IP定位,其他使用gps定位
function getlocation() { var u = navigator.userAgent; var ua = navigator.userAgent.toLowerCase(); var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android終端 var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios終端 //GPS定位 function locationForGPS() { /*********跟蹤標(biāo)記**********/ console.log('GPS定位開(kāi)始+++'); var map, geolocation; //加載地圖,調(diào)用瀏覽器定位服務(wù) map = new AMap.Map('iCenter'); map.plugin('AMap.Geolocation', function() { geolocation = new AMap.Geolocation({ enableHighAccuracy: false,//是否使用高精度定位,默認(rèn):true timeout: 10000, //超過(guò)10秒后停止定位,默認(rèn):無(wú)窮大 }); map.addControl(geolocation); geolocation.getCurrentPosition(); //getCityInfo是高德GPS定位里面的一個(gè)方法,先返回ip定位數(shù)據(jù),由于IP定位有可能不準(zhǔn)確,所以后面用GPS數(shù)據(jù)修正 // geolocation.getCityInfo(function(status,result){ /*********跟蹤標(biāo)記**********/ console.log('與GPS同步IP定位開(kāi)始,進(jìn)行數(shù)據(jù)寫(xiě)入+++'); if (status === 'complete' && result.info === 'SUCCESS') { /*********跟蹤標(biāo)記**********/ console.log('同步IP定位成功,進(jìn)行數(shù)據(jù)寫(xiě)入+++'); if(isNull(sessionStorage.getItem('autouserchooselocationct'))){ /*********跟蹤標(biāo)記**********/ console.log('未檢測(cè)檢查有上一次定位數(shù)據(jù),進(jìn)行數(shù)據(jù)寫(xiě)入+++'); let cityAdcode=result.adcode; let cityName=result.city; sessionStorage.setItem('autouserchooselocationct', cityName); sessionStorage.setItem('autouserchooselocationcode', cityAdcode); /*********跟蹤標(biāo)記**********/ console.log('同步IP定位結(jié)束,進(jìn)行數(shù)據(jù)寫(xiě)入完成+++'); } } }); AMap.event.addListener(geolocation, 'complete', onComplete);//返回定位信息 AMap.event.addListener(geolocation, 'error', onError); //返回定位出錯(cuò)信息 }); //GPS定位成功 function onComplete(data) { console.log(data) /*********跟蹤標(biāo)記**********/ console.log('GPS定位啟動(dòng)+++'); //gsp定位精確到區(qū),返回的adcode精確到了城市下屬的區(qū)域, //想要獲取城市的abcode還需要使用高德的另一個(gè)API,城市區(qū)域查詢(xún) let cityName=data.addressComponent.city; let geocoder = new AMap.Geocoder({}); //地理編碼,返回地理編碼結(jié)果, geocoder.getLocation(cityName, function(status, result) { if (status === 'complete' && result.info === 'OK') { /*********跟蹤標(biāo)記**********/ console.log('GPS定位成功,處理定位數(shù)據(jù)+++'); /*********跟蹤標(biāo)記**********/ console.log('進(jìn)行數(shù)據(jù)寫(xiě)入,覆蓋同步IP的數(shù)據(jù)+++'); let cityAdcode=result.geocodes[0].adcode; sessionStorage.setItem('autouserchooselocationct', cityName); sessionStorage.setItem('autouserchooselocationcode', cityAdcode); /*********跟蹤標(biāo)記**********/ console.log('GPS定位結(jié)束,進(jìn)行數(shù)據(jù)寫(xiě)入完成+++'); } }); } //GPS定位失敗 function onError() { /*********跟蹤標(biāo)記**********/ console.log('GPS定位失敗開(kāi)始啟用ip定位+++'); locationForIp(true); /*********跟蹤標(biāo)記**********/ console.log('gps-ip++...') } } //IP定位 function locationForIp(tap) { /*********跟蹤標(biāo)記**********/if(tap){ console.log('GPS定位失敗開(kāi)始啟用ip定位+++');} /*********跟蹤標(biāo)記**********/ console.log('ip定位開(kāi)始+++'); var citysearch = new AMap.CitySearch(); //自動(dòng)獲取用戶(hù)IP,返回當(dāng)前城市 citysearch.getLocalCity(function (status, result) { if (status === 'complete' && result.info === 'OK') { if (result && result.city && result.bounds) { let GetUserLocation = result.city, GetUserLocationcode = result.adcode; /*********跟蹤標(biāo)記**********/console.log('ip定位成功,開(kāi)始檢查是否有上一次定位數(shù)據(jù)+++'); if(isNull(sessionStorage.getItem('autouserchooselocationct'))){ /*********跟蹤標(biāo)記**********/console.log('ip定位成功,未檢測(cè)檢查有上一次定位數(shù)據(jù),進(jìn)行數(shù)據(jù)寫(xiě)入+++'); sessionStorage.setItem('autouserchooselocationct', GetUserLocation); sessionStorage.setItem('autouserchooselocationcode', GetUserLocationcode); /*********跟蹤標(biāo)記**********/console.log('ip定位成功,進(jìn)行數(shù)據(jù)寫(xiě)入結(jié)束+++'); } } } else { /*********跟蹤標(biāo)記**********/console.log('ip定位失敗,進(jìn)行數(shù)據(jù)寫(xiě)入結(jié)束+++'); } }) } if(isiOS){ /*********跟蹤標(biāo)記**********/console.log('ios設(shè)備啟用IP定位'); locationForIp(false); }else { /*********跟蹤標(biāo)記**********/console.log('非ios設(shè)備啟用GPS定位'); locationForGPS() } } };
感謝各位的閱讀!看完上述內(nèi)容,你們對(duì)怎么實(shí)現(xiàn)移動(dòng)端的城市定位以及城市區(qū)域代碼adcode大概了解了嗎?希望文章內(nèi)容對(duì)大家有所幫助。如果想了解更多相關(guān)文章內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。