高德地理编码

Java使用高德地理编码API

API说明

地理编码 API是通过 HTTP/HTTPS 协议访问远程服务的接口,提供结构化地址与经纬度之间的相互转化的能力。

  • 地理编码: 将详细的结构化地址转换为高德经纬度坐标。

  • 注: API地址高德 —— 地理编码,同时需要申请key,具体方法查看链接

步骤解析

  1. 申请key;

  2. 使用RestTemplate 调用RESt API;

  3. 返回结果信息,做对应的操作。

前提

1
2
3
4
5
6
7
8
9
10
11
<!-- lombok 插件,使用注解的方式简化实体类get/set 方法生成 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<!-- Gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>

部分编码

  • 创建外部配置文件,设置申请的 高德key等配置

配置文件

创建配置文件geocode.properties

1
2
3
# 高德——地理编码
geo.url= http://restapi.amap.com/v3/geocode/geo
geo.key= xxx #自己申请的key

修改启动类,增加该配置类注解

1
@EnableConfigurationProperties(GeocodingProperties.class)

使用实体类,获取配置文件内容

  • SpringMVC项目获取配置文件的方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

public class GeocodeUtil{

static Properties properties = new Properties();

static {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
InputStream in = new BufferedInputStream(cl.getResourceAsStream("gaode.properties"));
try {
properties.load(in);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

/**
* API 请求URL
*/
private final static String GD_URL = properties.getProperty("gaodeUrl");

/**
* 高德地图 key
*/
private final static String GD_KEY = properties.getProperty("gaodeKey");

}
  • SpringBoot使用实体类获取自定义配置代码块

配置属性实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.sanss.config;

import lombok.Data;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource("classpath:geocode.properties")
@ConfigurationProperties(prefix = "geo")
@Data
@ToString
public class GeocodingProperties {

private String url;

private String key;

}

工具类代码块

  • 说明

    1. 该类中的多个内部实体类,可以独立生成文件;
    2. 返回结果获取的实体类属性,可以自己增加或更改成与需求一致;
    3. 只是最简单的方式完成请求API,其他操作需根据自身需求进行更改。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package com.sanss.util.geocode;

import com.sanss.config.GeocodingProperties;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.annotation.PostConstruct;
import lombok.Data;
import lombok.ToString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
* @ClassName: GeoDto
* @Description: 调用高德地理编码API传递的参数实体类
* @date 2019/03/27
*/
class GeoDto {

private Boolean batch;

private String city;

private String address;

private String url;

private String key;

public GeoDto() {
}

public GeoDto(List<String> address, String city, Boolean batch, String url, String key) {
StringBuilder addressSb = new StringBuilder();
for (int i = 0, length = address.size(); i < length; i++) {
addressSb.append(address.get(i));
addressSb.append("|");
}
this.address = addressSb.substring(0, addressSb.lastIndexOf("|")).toString();
this.city = city;
this.batch = batch;
this.url = url;
this.key = key;
}

@Override
public String toString() {
return url + "?batch=" + batch + "&key=" + key + "&city=" + city + "&address=" + address;
}

}

/**
* @ClassName: Geocodes
* @Description: 返回结果实体类, 只获取了location:经纬度 API 地址:https://lbs.amap.com/api/webservice/guide/api/georegeo
* 返回结果示例: {"status":"1","info":"OK","infocode":"10000","count":"1","geocodes":[{"formatted_address":"上海市浦东新区东方明珠","country":"中国","province":"上海市","citycode":"021","city":"上海市","district":"浦东新区","township":[],"neighborhood":{"name":[],"type":[]},"building":{"name":[],"type":[]},"adcode":"310115","street":[],"number":[],"location":"121.499740,31.239853","level":"兴趣点"}]}
* @date 2019/03/27
*/

@Data
@ToString
class Geocodes {

private String location;

}

@Data
@ToString
class Geocode {

private int status;

private int count;

private List<Geocodes> geocodes;

}

/**
* @ClassName: GeoCodingUtil
* @Description: 高德地理编码/逆编码(将地址转换为经度,纬度) API: https://lbs.amap.com/api/webservice/guide/api/georegeo/#scene
* @date 2019/03/14
*/
@Component
public class GeoCodingUtil {

@Autowired
private RestTemplate restTemplate;

@Autowired
private GeocodingProperties properties;

private static GeoCodingUtil geoCodingUtil;

@PostConstruct
private void init() {
geoCodingUtil = this;
geoCodingUtil.properties = this.properties;
geoCodingUtil.restTemplate = this.restTemplate;
}

public List<String> getShanghaiGeocoding(List<String> address, boolean batch) {
List<String> point = new ArrayList<>();

System.out.println(geoCodingUtil.properties.toString());

GeoDto geoDto = new GeoDto(address, "上海", batch, geoCodingUtil.properties.getUrl(),
geoCodingUtil.properties.getKey());
String realUrl = geoDto.toString();
System.out.println(realUrl);
try {
Geocode geocode = geoCodingUtil.restTemplate.getForObject(realUrl, Geocode.class);
int count = geocode.getCount();
if (count > 0) {
List<Geocodes> geocodes = geocode.getGeocodes();
Iterator<Geocodes> iterator = geocodes.iterator();
String location;
while (iterator.hasNext()) {
location = iterator.next().getLocation();
if (location == null) {
point.add("");
continue;
}
point.add(location);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return point;
}
}

测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.sanss.util;

import com.sanss.config.GeocodingProperties;
import com.sanss.util.geocode.GeoCodingUtil;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
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;

@RunWith(SpringRunner.class)
@SpringBootTest
public class GeoCodingUtilTest {

@Autowired
private GeocodingProperties properties;

@Test
public void propertiesTest() {
System.out.println(properties.getKey() + "\t" + properties.getUrl());
}

@Test
public void getShanghaiGeocodingTest() {
// 目前最高支持十个地址查询
List<String> address = new ArrayList<String>();
address.add("东方明珠广播塔");
address.add("虹口足球场");
address.add("徐家汇地铁站");
List<String> points = new GeoCodingUtil().getShanghaiGeocoding(address, true);
System.out.println(points);
}

}
  • 打印结果
1
[121.499740,31.239853, 121.480592,31.271447, 121.438133,31.191698]