一、准备工作:
- 申请一个企业的账号(必须是企业的账号)
-
申请key:
-
获取开发文档:
二、定位sdk的使用:
高德地图支持wifi、基站定位
- wifi定位:
/**
* @author ww
* @version 1.0
* @Date 2020/1/6
*/
@RestController
public class PositionController {
/**
*
*/
static class Constants {
/**
*
*/
public static final String QUESTION_MARK = "?";
/**
*
*/
public static final String EQUALS = "=";
/**
*
*/
public static final String AND = "&";
/**
*
*/
public static final String CHARSET = "UTF-8";
/**
* url
*/
private static final String BASE_URL = "https://apilocate.amap.com/position";
}
/**
* 高德wifi 定位
* @return
*/
@RequestMapping(value = "/position")
public String position() {
Map<String, String> params = new HashMap<>();
// wifi定位参数
params.put("macs", "4c:48:da:25:0b:11,-59,alibaba-inc%7c4c:48:da:25:1a:11,-77,alibaba-inc");
// 是否为移动网 0 表示移动网 1表示是wifi
params.put("accesstype", "1");
// 申请的api的key
params.put("key", "修改成申请的api key");
// 输出内容为json字符串
params.put("output", "json");
StringBuilder sb = new StringBuilder();
sb.append(Constants.BASE_URL);
if (!CollectionUtils.isEmpty(params)) {
sb.append(Constants.QUESTION_MARK);
for (Map.Entry<String, String> entry : params.entrySet()) {
sb.append(entry.getKey()).append(Constants.EQUALS).append(entry.getValue()).append(Constants.AND);
}
sb.deleteCharAt(sb.length() - 1);
}
// 请求定位sdk
HttpGet get = new HttpGet(sb.toString());
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
HttpEntity entity;
try {
httpClient = HttpClients.createDefault();
response = httpClient.execute(get);
entity = response.getEntity();
return EntityUtils.toString(entity, Constants.CHARSET);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
if (httpClient != null) {
httpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
注意问题:
- 包含多个macs的列表的时候高德给的sdk里面有一个特殊字符"|",这个字符用httpclient请求定位的sdk的时候,会报错如:
java.net.URISyntaxException: Illegal character in query at index 97。此时需要将"|" 更换成%7c(%加上16进制的该字符的ASCII值)