Недавно разработчики столкнулись с проблемой преобразования широты, долготы и географического положения, поэтому я просто написал класс инструмента для его использования.
Запрос широты и долготы в зависимости от местоположения
package com.nbomb.town.util;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
/**
* 根据位置查询经纬度
*/
@Component
public class LngAndLatUtil {
public Map<String, Object> getURLContent(String address) {
//这里需要使用你的key值
String urlStr = "https://apis.map.qq.com/ws/geocoder/v1/?address=" + address + "&key=你自己的key";
//请求的url
URL url = null;
//请求的输入流
BufferedReader in = null;
//输入流的缓冲
StringBuffer sb = new StringBuffer();
try {
url = new URL(urlStr);
in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
String str = null;
//一行一行进行读入
while ((str = in.readLine()) != null) {
sb.append(str);
}
} catch (Exception ex) {
} finally {
try {
if (in != null) {
in.close(); //关闭流
}
} catch (IOException ex) {
}
}
String result = sb.toString();
String r = JSONObject.parseObject(result).getString("result");
String location = JSONObject.parseObject(r).getString("location");
String lng = JSONObject.parseObject(location).getString("lng");
String lat = JSONObject.parseObject(location).getString("lat");
Map<String, Object> map = new HashMap<>();
map.put("lng", lng);
map.put("lat", lat);
return map;
}
/*public static void main(String[] args) {
Map<String, Object> map = getURLContent("浙江省,金华市,罗店镇");
System.out.println("lng=" + map.get("lng") + ",lat=" + map.get("lat"));
}*/
}
Получить адрес по широте и долготе
package com.nbomb.town.util;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
* @author SunWenKe
* @version v1.0
* @date 2022-11-17 15:17
*/
/**
* 通过经纬度获取地址
*/
@Component
public class JWd {
public JSONObject getHttpJson(String latitude, String longitude) throws Exception {
String url = "https://apis.map.qq.com/ws/geocoder/v1/?location="+latitude+","+longitude+"&key=你自己的key";
try {
URL realUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立实际的连接
connection.connect();
//请求成功
if (connection.getResponseCode() == 200) {
InputStream is = connection.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//10KB的缓存
byte[] buffer = new byte[10240];
int len = 0;
while ((len = is.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
String jsonString = baos.toString();
baos.close();
is.close();
JSONObject jsonArray = JSONObject.parseObject(jsonString);
return jsonArray;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
return null;
}
/* public static void main(String[] args) throws Exception {
String latitude = "29.099771";
String longitude = "119.804802";
JSONObject httpJson = getHttpJson(latitude,longitude);
//获取data
String province = httpJson.getJSONObject("result").getJSONObject("ad_info").get("province").toString();
String city = httpJson.getJSONObject("result").getJSONObject("ad_info").get("city").toString();
String name = province+","+city;
System.out.println(name);
System.out.println(province);
//获取deptCode
System.out.println(httpJson);
}*/
}
Просто зайдите на открытую платформу Tencent Map, чтобы подать заявку самостоятельно, есть много свободного времени.