您當前的位置:首頁 > 遊戲

高德地圖呼叫

作者:由 java後端指南 發表于 遊戲時間:2020-10-24

原文:

環境

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。0。0

org。springframework。boot

spring-boot-starter-parent

2。3。4。RELEASE

<!—— lookup parent from repository ——>

com。KING

gaode_map

0。0。1-SNAPSHOT

gaode_map

Demo project for Spring Boot

1。8

org。springframework。boot

spring-boot-starter-thymeleaf

org。springframework。boot

spring-boot-starter-web

org。springframework。boot

spring-boot-devtools

runtime

true

org。projectlombok

lombok

true

org。springframework。boot

spring-boot-starter-test

test

org。junit。vintage

junit-vintage-engine

org。springframework。boot

spring-boot-maven-plugin

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>

地圖初始IP城市定位

在初始化地圖時,如果center屬性預設,地圖預設定位到使用者所在城市的中心


啟動springboot,訪問localhost:埠,就可以看到下面這個,但是這種定位不準,他只能知道你在哪個城市

高德地圖呼叫

7、瀏覽器定位

我們可以透過高德JS API提供了AMap。Geolocation外掛來實現定位

<!doctype html>

瀏覽器精確定位



由於眾多瀏覽器已不再支援非安全域的定位請求,為保位成功率和精度,請升級您的站點到HTTPS。

這種圖中多了一個定位按鈕,定位更加準確了

高德地圖呼叫

8、IP定位獲取當前城市資訊

由於我的瀏覽器關閉了定位許可權,所以他無法獲取我的經緯度,大家可以在程式碼中自己寫入對應的經緯度就可以定位了

<!doctype html>

根據ip定位

高德地圖呼叫

好的,到這裡就結束了,有時間再和大家探討更深入的東西吧

標簽: 定位  Result  amap  map  var