高德地图web服务api反坐标查询/逆地理编码
生活随笔
收集整理的這篇文章主要介紹了
高德地图web服务api反坐标查询/逆地理编码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
官方API:https://lbs.amap.com/api/webservice/gettingstarted
1、pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.3.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.api</groupId><artifactId>gaodeapi</artifactId><version>0.0.1-SNAPSHOT</version><name>gaodeapi</name><description>Demo project for Spring Boot</description><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.0.0</version></dependency><dependency><groupId>org.postgresql</groupId><artifactId>postgresql</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.postgresql</groupId><artifactId>postgresql</artifactId><version>42.1.2</version></dependency><!-- alibaba的druid數據庫連接池 --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.0</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp --><dependency><groupId>com.squareup.okhttp3</groupId><artifactId>okhttp</artifactId><version>3.10.0</version></dependency><!-- https://mvnrepository.com/artifact/com.squareup.okio/okio --><dependency><groupId>com.squareup.okio</groupId><artifactId>okio</artifactId><version>1.13.0</version></dependency><!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.47</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.1.4.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-test</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>2、代碼
package com.tyxx.action;import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.tyxx.GaodeapiApplication; import com.tyxx.model.GdGPS; import com.tyxx.service.GdGPSService; import okhttp3.Call; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner;import java.io.IOException; import java.util.List;/*** Created by cqy on 2019/3/18.*/ @RunWith(SpringRunner.class) @SpringBootTest(classes = {GaodeapiApplication.class})// 指定啟動類 public class Test {@Autowiredprivate GdGPSService gdGPSService; //數據查詢和更新@org.junit.Testpublic void testSelect() {List<GdGPS> lt = gdGPSService.selAll();for (int i = 0; i < lt.size(); i++) {GdGPS gdGPS = lt.get(i);System.out.println("id:" + gdGPS.getId());String str = Test.gdapi(gdGPS.getJingdu(), gdGPS.getWeidu());System.out.println("位置:" + str);gdGPS.setAddress(str);Integer row = gdGPSService.updatePri(gdGPS);System.out.println(row + "-------------");}}public static String gdapi(String jingdu, String weidu) {String url = "https://restapi.amap.com/v3/geocode/regeo?output=json&location=" +jingdu + "," + weidu + "&key=換成自己申請的key&radius=1000&extensions=all";OkHttpClient okHttpClient = new OkHttpClient();Request request = new Request.Builder().url(url).get().build();Call call = okHttpClient.newCall(request);String results = "";try {Response response = call.execute();String result = response.body().string();// System.out.println(result);JSONObject json = JSONArray.parseObject(result);// System.out.println(json);// System.out.println(json.get("regeocode"));JSONObject obj = JSONObject.parseObject(json.get("regeocode").toString());System.out.println(obj.get("formatted_address"));results = obj.get("formatted_address").toString();} catch (IOException e) {e.printStackTrace();}return results;}}總結
以上是生活随笔為你收集整理的高德地图web服务api反坐标查询/逆地理编码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 升级iOS10后,AVPlayer有时候
- 下一篇: 震撼!七大议题全数入选!九州云与您相约温