| 承载协议 | HTTPS POST |
|---|---|
| 接口方向 | 开发者服务器 -> 华为地图服务器 |
| 接口URL | https://mapapi.cloud.huawei.com/mapApi/v1/elevation/getElevation |
| 数据格式 | 请求消息:Content-Type: application/json 响应消息:Content-Type: application/json |
Request Header中的 Authorization 和Query String中的 key 至少需要存在一个。
Request Header
| 参数 | 是否必选 | 参数类型 | 描述 |
|---|---|---|---|
| Content-Type | 是 | application/json | 请求消息的数据格式。 |
| Authorization | 否 | String | AT或者API Key,推荐用AT。 说明: |
Query String
| 参数 | 是否必选 | 参数类型 | 描述 |
|---|---|---|---|
| key | 否 | String | 鉴权密钥,申请参见获取API密钥。 说明:
|
Request Body
参数优先级:locations>encodedLocation>path+samples>encodedPath+samples。详细传参说明:
| 参数 | 是否必选 | 参数类型 | 描述 |
|---|---|---|---|
| locations | 否 | 根据经纬度计算海拔,一个或多个位置点的集合,最多可以传入512个经纬度。 | |
| encodedLocation | 否 | String | 使用GeoHash算法编码后的经纬度。 允许传入字符串数组,通过“;”符号来分隔不同的编码字符串。 |
| path | 否 | 根据指定路线计算海拔,传入沿路线上路径点的经纬度集合。 | |
| encodedPath | 否 | String | 使用GeoHash算法编码后的路径点经纬度。 允许传入字符串数组,通过“;”符号来分隔不同的编码字符串。 |
| samples | 否 | Integer | 路线的采样点个数。 用于结合path或encodedPath参数对应的路径,沿着整条采样路径,生成等距离指定数量的采样点,包括采样路径的起点和终点。 当请求参数传入path或encodedPath时,samples为必选。 |
POST https://mapapi.cloud.huawei.com/mapApi/v1/elevation/getElevation?key=API KEY HTTP/1.1
Content-Type: application/json
Accept: application/json
{
"locations": [
{
"lat": 21.0,
"lng": 21.0
},
{
"lat": 21.5,
"lng": 21.5
}
]
} 状态码为200时:
Response Header
| 参数 | 是否必选 | 参数类型 | 描述 |
|---|---|---|---|
| Content-Type | 是 | application/json | 响应消息的数据格式。 |
Response Body
| 参数 | 参数类型 | 描述 |
|---|---|---|
| returnCode | String | 返回码,具体请参见错误码。 |
| returnDesc | String | 返回值描述。 |
| results | 如果查询成功,返回海拔信息。 |
状态码为200时:
HTTP/1.1 200 OK
Content-type: application/json
{
"returnCode": "0",
"results": [
{
"elevation": 572.0,
"location": {
"lng": 21.0,
"lat": 21.0
},
"resolution": 28.868248865094273
},
{
"elevation": 513.0,
"location": {
"lng": 21.5,
"lat": 21.5
},
"resolution": 28.77044670027833
}
],
"returnDesc": "OK"
} “AT认证”
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
@Slf4j
class TestElevation {
private static final Logger log = LoggerFactory.getLogger(TestElevation.class);
public static final String ROOT_URL = "https://mapapi.cloud.huawei.com/mapApi/v1/elevation/getElevation";
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
public static void getElevation(String accessToken) throws UnsupportedEncodingException {
JSONObject json = new JSONObject();
try {
JSONArray locations = new JSONArray();
JSONObject startingPoint_1 = new JSONObject();
JSONObject startingPoint_2 = new JSONObject();
startingPoint_1.put("lng", 21.0);
startingPoint_1.put("lat", 21.0);
startingPoint_2.put("lng", 21.5);
startingPoint_2.put("lat", 21.5);
locations.add(0, startingPoint_1);
locations.add(1, startingPoint_2);
json.put("locations", locations);
} catch (JSONException e) {
log.error("getElevation failed : {}", e.getMessage());
}
RequestBody body = RequestBody.create(JSON, json.toJSONString());
OkHttpClient client = new OkHttpClient();
Request request =
new Request.Builder().url(ROOT_URL).addHeader("Authorization", "Bearer " + accessToken)
.post(body)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
log.error("getElevation onFailure : {}", e.toString());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
log.debug("getElevation onResponse : {}", response.body().string());
}
});
}
} “API KEY认证”
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
@Slf4j
class TestElevation {
private static final Logger log = LoggerFactory.getLogger(TestElevation.class);
public static final String ROOT_URL = "https://mapapi.cloud.huawei.com/mapApi/v1/elevation/getElevation";
public static final String connection = "?key=";
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
public static void getElevation(String apiKey) throws UnsupportedEncodingException {
JSONObject json = new JSONObject();
try {
JSONArray locations = new JSONArray();
JSONObject startingPoint_1 = new JSONObject();
JSONObject startingPoint_2 = new JSONObject();
startingPoint_1.put("lng", 21.0);
startingPoint_1.put("lat", 21.0);
startingPoint_2.put("lng", 21.5);
startingPoint_2.put("lat", 21.5);
locations.add(0, startingPoint_1);
locations.add(1, startingPoint_2);
json.put("locations", locations);
} catch (JSONException e) {
log.error("getElevation failed : {}", e.getMessage());
}
RequestBody body = RequestBody.create(JSON, json.toJSONString());
OkHttpClient client = new OkHttpClient();
Request request =
new Request.Builder().url(ROOT_URL + connection + URLEncoder.encode(apiKey, "UTF-8"))
.post(body)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
log.error("getElevation onFailure : {}", e.toString());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
log.debug("getElevation onResponse : {}", response.body().string());
}
});
}
} /*
* Copyright (c) Huawei Technologies Co., Ltd. 2020-2021. All rights reserved.
*/
package elevation;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import java.util.Arrays;
import java.util.BitSet;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
/**
* GeoHash编码
*/
public class GeoHashUtil {
private static final int NUM_BITS = 3 * 5;
private static final char[] DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
// 定义编码映射关系
private static final HashMap<Character, Integer> lookup = new HashMap<>();
static {
// 初始化编码映射内容
int index = 0;
for (char c : DIGITS) {
lookup.put(c, index++);
}
}
/**
* 编码
*
* @param lat double
* @param lng double
* @return String
*/
public static String encode(double lat, double lng) {
BitSet latbits = getBits(lat, -90, 90);
BitSet lonbits = getBits(lng, -180, 180);
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < NUM_BITS; i++) {
buffer.append((lonbits.get(i)) ? '1' : '0');
buffer.append((latbits.get(i)) ? '1' : '0');
}
return base32(Long.parseLong(buffer.toString(), 2));
}
private static BitSet getBits(double lat, double floor, double ceiling) {
BitSet buffer = new BitSet(NUM_BITS);
for (int i = 0; i < NUM_BITS; i++) {
double mid = (floor + ceiling) / 2;
if (lat >= mid) {
buffer.set(i);
floor = mid;
} else {
ceiling = mid;
}
}
return buffer;
}
private static String base32(long i) {
char[] buf = new char[65];
int charPos = 64;
boolean negative = (i < 0);
if (!negative) {
i = -i;
}
while (i <= -32) {
buf[charPos--] = DIGITS[(int) (-(i % 32))];
i /= 32;
}
buf[charPos] = DIGITS[(int) (-i)];
if (negative) {
buf[--charPos] = '-';
}
return new String(buf, charPos, (65 - charPos));
}
private static double decode(BitSet bs, double floor, double ceiling) {
double mid = 0;
for (int i = 0; i < bs.length(); i++) {
mid = (floor + ceiling) / 2;
if (bs.get(i)) {
floor = mid;
} else {
ceiling = mid;
}
}
return mid;
}
/**
* 对编码后的字符串解码
*
* @param geohash String
* @return double[]
*/
public static List<CoordinateDTO> decode(String geohash) {
return Arrays.stream(geohash.split(";")).map(GeoHashUtil::decodeSingle).collect(Collectors.toList());
}
private static CoordinateDTO decodeSingle(String geohash) {
StringBuilder buffer = new StringBuilder();
for (char c : geohash.toCharArray()) {
int i = lookup.get(c) + 32;
buffer.append(Integer.toString(i, 2).substring(1));
}
BitSet lngset = new BitSet();
BitSet latset = new BitSet();
int j = 0;
for (int i = 0; i < NUM_BITS * 2; i += 2) {
boolean isSet = false;
if (i < buffer.length()) {
isSet = buffer.charAt(i) == '1';
}
lngset.set(j++, isSet);
}
j = 0;
for (int i = 1; i < NUM_BITS * 2; i += 2) {
boolean isSet = false;
if (i < buffer.length()) {
isSet = buffer.charAt(i) == '1';
}
latset.set(j++, isSet);
}
double lng = decode(lngset, -180, 180);
double lat = decode(latset, -90, 90);
CoordinateDTO coordinateDTO = new CoordinateDTO();
coordinateDTO.setLat(lat);
coordinateDTO.setLng(lng);
return coordinateDTO;
}
/**
* 经纬度
*/
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public static class CoordinateDTO {
private Double lat;
private Double lng;
}
}