高德地圖呼叫
原文:
環境
IDEA
springboot
maven3
實現過程
1、首先我們需要登入高德開發平臺:
https://lbs。amap。com/
2、
控制檯
->
應用管理
->
建立新應用
,這裡會產生一個
key
,我們後面開發中會用到
3、大家建立一個
springboot
工程,根據自己需要匯入一些座標,我的座標如下:
<?xml version=“1。0” encoding=“UTF-8”?>
xsi:schemaLocation=“http://maven。apache。org/POM/4。0。0 https://maven。apache。org/xsd/maven-4。0。0。xsd”>
4、大家可以參考一下我的工程目錄結構
5、我就偷懶了,直接將
控制器
寫在
啟動類
中
@SpringBootApplication
@Controller
public class GaodeMapApplication {
@GetMapping(“/”)
public String toIndex(){
return “index。html”;
}
public static void main(String[] args) {
SpringApplication。run(GaodeMapApplication。class, args);
}
}
map。css:
#container {width:800px; height: 500px; text-align: center}
index。html: 這個介面就要分情況寫了
參考了官方開發文件:
https://lbs。amap。com/api/javascript-api/guide/services/geolocation
我們今天講的是怎麼呼叫他的定位功能,定位方式分為以下幾種:
地圖初始化載入定位到當前城市
瀏覽器定位
IP定位獲取當前城市資訊
現在咋們分別來看看怎麼寫
這三種方式都要引入css檔案和js檔案
<!——key填寫自己的——>
6、地圖初始化載入定位到當前城市
<!doctype html>
html,body,#container{
height:100%;
}
。info{
width:20rem;
}
在初始化地圖時,如果center屬性預設,地圖預設定位到使用者所在城市的中心
//初始化地圖時,若center屬性預設,地圖預設定位到使用者所在城市的中心
var map = new AMap。Map(‘container’, {
resizeEnable: true
});
document。getElementById(‘adcode’)。innerHTML=‘當前城市adcode:’+map。getAdcode()+‘
’+
‘當前中心點:’+map。getCenter()
啟動springboot,訪問localhost:埠,就可以看到下面這個,但是這種定位不準,他只能知道你在哪個城市
7、瀏覽器定位
我們可以透過高德JS API提供了AMap。Geolocation外掛來實現定位
<!doctype html>
html,body,#container{
height:100%;
}
。info{
width:26rem;
}
由於眾多瀏覽器已不再支援非安全域的定位請求,為保位成功率和精度,請升級您的站點到HTTPS。
var map = new AMap。Map(‘container’, {
resizeEnable: true
});
AMap。plugin(‘AMap。Geolocation’, function() {
var geolocation = new AMap。Geolocation({
enableHighAccuracy: true,//是否使用高精度定位,預設:true
timeout: 10000, //超過10秒後停止定位,預設:5s
buttonPosition:‘RB’, //定位按鈕的停靠位置
buttonOffset: new AMap。Pixel(10, 20),//定位按鈕與設定的停靠位置的偏移量,預設:Pixel(10, 20)
zoomToAccuracy: true, //定位成功後是否自動調整地圖視野到定位點
});
map。addControl(geolocation);
geolocation。getCurrentPosition(function(status,result){
if(status==‘complete’){
onComplete(result)
}else{
onError(result)
}
});
});
//解析定位結果
function onComplete(data) {
document。getElementById(‘status’)。innerHTML=‘定位成功’
var str = [];
str。push(‘定位結果:’ + data。position);
str。push(‘定位類別:’ + data。location_type);
if(data。accuracy){
str。push(‘精度:’ + data。accuracy + ‘ 米’);
}//如為IP精確定位結果則沒有精度資訊
str。push(‘是否經過偏移:’ + (data。isConverted ? ‘是’ : ‘否’));
document。getElementById(‘result’)。innerHTML = str。join(‘
’);
}
//解析定位錯誤資訊
function onError(data) {
document。getElementById(‘status’)。innerHTML=‘定位失敗’
document。getElementById(‘result’)。innerHTML = ‘失敗原因排查資訊:’+data。message;
}
這種圖中多了一個定位按鈕,定位更加準確了
8、IP定位獲取當前城市資訊
由於我的瀏覽器關閉了定位許可權,所以他無法獲取我的經緯度,大家可以在程式碼中自己寫入對應的經緯度就可以定位了
<!doctype html>
html,body,#container{
height:100%;
}
var map = new AMap。Map(“container”, {
resizeEnable: true,
//寫需要定位的經緯度座標
center: [116。397428, 39。90923],
zoom: 13
});
//獲取使用者所在城市資訊
function showCityInfo() {
//例項化城市查詢類
var citysearch = new AMap。CitySearch();
//自動獲取使用者IP,返回當前城市
citysearch。getLocalCity(function(status, result) {
if (status === ‘complete’ && result。info === ‘OK’) {
if (result && result。city && result。bounds) {
var cityinfo = result。city;
var citybounds = result。bounds;
document。getElementById(‘info’)。innerHTML = ‘您當前所在城市:’+cityinfo;
//地圖顯示當前城市
map。setBounds(citybounds);
}
} else {
document。getElementById(‘info’)。innerHTML = result。info;
}
});
}
showCityInfo();
好的,到這裡就結束了,有時間再和大家探討更深入的東西吧
上一篇:為什麼會有民族主義呢?
下一篇:尊重孩子的不高興