官方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 {
@Autowired
private GdGPSService gdGPSService; //数据查询和更新
@org.junit.Test
public 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;
}
}