设置电子围栏 高德地图_Java实现高德地图地理围栏设置功能工具类

[Java] 纯文本查看 复制代码import com.alibaba.fastjson.JSONObject;

import org.apache.http.client.methods.*;

import org.apache.http.client.utils.URIBuilder;

import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.util.EntityUtils;

import java.io.IOException;

import java.net.URI;

import java.nio.charset.Charset;

/**

* @Description 高德电子围栏工具类

* [url=home.php?mod=space&uid=686208]@AuThor[/url] Veddy

* [url=home.php?mod=space&uid=686237]@date[/url] 2020-07-10

*/

public class AMapFenceUtils {

/**

* 高德地图地理围栏创建

* [url=home.php?mod=space&uid=952169]@Param[/url] key 高德地图Web服务API类型Key

* @param name 围栏名称

* @param points 电子围栏经纬度 例"117.317159,31.852989;117.297525,31.847862;117.296071,31.86483"

* @param desc 电子围栏描述

* @return

*/

public static String createFence(String key,String name,String points,String desc){

//组装url

String url = "https://restapi.amap.com/v4/geofence/meta?key=" + key;

// 创建httpClient

CloseableHttpClient httpClient = HttpClients.createDefault();

// 创建post请求方式实例

HttpPost httpPost = new HttpPost(url);

// 设置请求头 发送的是json数据格式

httpPost.setHeader("Content-type", "application/json;charset=utf-8");

httpPost.setHeader("Connection", "Close");

JSONObject jsonObject = new JSONObject();

//围栏名称

jsonObject.put("name",name);

//围栏经纬度

jsonObject.put("points",points);

//电子围栏有效期

jsonObject.put("valid_time","2050-12-30");

//电子围栏监控频率

jsonObject.put("repeat","Mon,Tues,Wed,Thur,Fri,Sat,Sun");

//备注

if (desc != null){

jsonObject.put("desc",desc);

}

// 设置参数---设置消息实体 也就是携带的数据

StringEntity entity = new StringEntity(jsonObject.toString(), Charset.forName("UTF-8"));

// 设置编码格式

entity.setContentEncoding("UTF-8");

// 发送Json格式的数据请求

entity.setContentType("application/json");

// 把请求消息实体塞进去

httpPost.setEntity(entity);

// 执行http的post请求

CloseableHttpResponse httpResponse;

String result = null;

try {

httpResponse = httpClient.execute(httpPost);

result = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");

} catch (IOException e) {

e.printStackTrace();

}

return result;

}

/**

* 更新电子围栏的经纬度

* @param key 高德地图Web服务API类型key

* @param gid 围栏全局ID 创建围栏后返回

* @param name 围栏名称

* @param points 电子围栏经纬度 例"117.317159,31.852989;117.297525,31.847862;117.296071,31.86483"

* @return

*/

public static String updateFence(String key,String gid,String name,String points){

//组装URL

String url = "https://restapi.amap.com/v4/geofence/meta?key="+ key +"&gid=" + gid;

// 创建httpClient

CloseableHttpClient httpClient = HttpClients.createDefault();

// 创建post请求方式实例

// HttpPost httpPost = new HttpPost(url);

HttpPatch httpPatch = new HttpPatch(url);

// 设置请求头 发送的是json数据格式

httpPatch.setHeader("Content-type", "application/json;charset=utf-8");

httpPatch.setHeader("Connection", "Close");

JSONObject jsonObject = new JSONObject();

//围栏名称

jsonObject.put("name",name);

//围栏经纬度数组

jsonObject.put("points",points);

//电子围栏有效期

jsonObject.put("valid_time","2050-12-30");

//电子围栏监控频率

jsonObject.put("repeat","Mon,Tues,Wed,Thur,Fri,Sat,Sun");

// 设置参数---设置消息实体 也就是携带的数据

StringEntity entity = new StringEntity(jsonObject.toString(), Charset.forName("UTF-8"));

// 设置编码格式

entity.setContentEncoding("UTF-8");

// 发送Json格式的数据请求

entity.setContentType("application/json");

// 把请求消息实体塞进去

httpPatch.setEntity(entity);

// 执行http的post请求

CloseableHttpResponse httpResponse;

String result = null;

try {

httpResponse = httpClient.execute(httpPatch);

result = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");

} catch (IOException e) {

e.printStackTrace();

}

return result;

}

/**

* 验证是否在电子围栏内部

* @param key 高德地图Web服务Api类型Key

* @param diu 设备唯一标识

* @param locations 设备经纬度

* @return

*/

public static String checkFence(String key,String diu,String locations){

//获取unix时间戳

String dateStr = Long.toString(System.currentTimeMillis()/1000L);

String url = "https://restapi.amap.com/v4/geofence/status?key="+ key +"&diu="+ diu +"&locations=" + locations+","+dateStr;

String result = null;

CloseableHttpResponse response = null;

CloseableHttpClient httpclient = HttpClients.createDefault();

try {

// 创建uri

URIBuilder builder = new URIBuilder(url);

URI uri = builder.build();

// 创建http GET请求

HttpGet httpGet = new HttpGet(uri);

// 执行请求

response = httpclient.execute(httpGet);

if (response.getStatusLine().getStatusCode() == 200) {

result = EntityUtils.toString(response.getEntity(), "UTF-8");

}

} catch (Exception e) {

e.printStackTrace();

}

return result;

}

/**

* 删除地理围栏

* @param key 高德地图Web服务Api类型Key

* @param gid 地理围栏全局ID 创建围栏后返回

* @return

*/

public static String deleteFence(String key,String gid){

//组装URL

String url = "https://restapi.amap.com/v4/geofence/meta?key="+ key +"&gid=" + gid;

String result = null;

CloseableHttpResponse response = null;

CloseableHttpClient httpclient = HttpClients.createDefault();

try {

// 创建uri

URIBuilder builder = new URIBuilder(url);

URI uri = builder.build();

// 创建http DELETE请求

HttpDelete httpDelete = new HttpDelete(uri);

// 执行请求

response = httpclient.execute(httpDelete);

if (response.getStatusLine().getStatusCode() == 200) {

result = EntityUtils.toString(response.getEntity(), "UTF-8");

}

} catch (Exception e) {

e.printStackTrace();

}

return result;

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值