package com.stylefeng.guns.modular.system.util;
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONObject;
|
import org.apache.http.HttpEntity;
|
import org.apache.http.client.config.RequestConfig;
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.HttpClients;
|
import org.apache.http.util.EntityUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Component;
|
import org.springframework.web.client.RestTemplate;
|
|
import java.io.IOException;
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
@Component
|
public class GaoDeMapUtil {
|
|
|
@Autowired
|
private RestTemplate restTemplate;
|
private String key = "WQhfsluNzEeUHUxoH4jc4JiCQOXw4Mnx";
|
|
/**
|
* 根据地址查询经纬度
|
*
|
* @param address
|
* @return
|
*/
|
public JSONObject getLngAndLat(String address) {
|
JSONObject positionObj = new JSONObject();
|
Map<String, Object> map = new HashMap<>();
|
String url = "https://api.map.baidu.com/geocoding/v3/?address" + address + "&output=json&ak=" + key;
|
String forObject = restTemplate.getForObject(url, String.class);
|
JSONObject jsonObject = JSON.parseObject(forObject);
|
String status = jsonObject.getString("status");
|
if (status.equals("0")) {
|
JSONObject result = jsonObject.getJSONObject("result");
|
JSONObject location = result.getJSONObject("location");
|
String lng = location.getString("lng");
|
String lat = location.getString("lat");
|
positionObj.put("longitude", lng);
|
positionObj.put("latitude", lat);
|
}
|
return positionObj;
|
}
|
|
/**
|
* 发送Get请求
|
*
|
* @param url
|
* @return
|
*/
|
public static String sendHttpGet(String url) {
|
HttpGet httpGet = new HttpGet(url);
|
RequestConfig defaultRequestConfig = RequestConfig.custom()
|
.setConnectTimeout(3000)
|
.setSocketTimeout(10000)
|
.build();
|
CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
|
String result = "";
|
try {
|
CloseableHttpResponse closeableHttpResponse = httpClient.execute(httpGet);
|
HttpEntity entity = closeableHttpResponse.getEntity();
|
result = EntityUtils.toString(entity, "UTF-8");
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
return result;
|
}
|
|
}
|