(一)、腾讯地图接口
1.搭建环境
(1).导入相关依赖
<dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>2.0.26</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.24</version> </dependency> </dependencies>
(2).获取腾讯地图接口Key
官网-> https://lbs.qq.com/dev/console/application/mine
2.根据经纬度获取地址 (逆地址解析)
官方文档: https://lbs.qq.com/service/webService/webServiceGuide/webServiceGcoder
package com.jsxs.TentcentMap.inverseAddressResolution; /** * @Author Jsxs * @Date 2023/4/4 19:39 * @PackageName:com.jsxs * @ClassName: Test * @Description: TODO 经纬度转地址 * @Version 1.0 */ import com.alibaba.fastjson.JSONObject; import java.io.*; import java.net.URL; public class inverseAddressResolution { /** * 通过腾讯地图将经纬度转成详细地址 * * @param lat 纬度 * @param lng 经度 * @param key 开发密钥(Key) 需自行申请 * @return */ public static JSONObject getLatAndLng(String lat, String lng, String key) throws Exception { // 1. 腾讯地图密钥 String hsUrl = "https://apis.map.qq.com/ws/geocoder/v1/?location=" + lat + "," + lng + "&key=" + key + "&get_poi=1"; // 2. 设置URL URL url = null; // 3. 设置缓存输入流() BufferedReader in = null; // 4. 设置缓冲区 StringBuffer sb = new StringBuffer(); try { // 5. 初始化URL对象 url = new URL(hsUrl); // 6. 初始化初始化 in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8")); // 7. String str = null; // 8. 进行读取数据并写入数据(一行行读取) while ((str = in.readLine()) != null) { // 9. 添加到我们的字符串缓冲区 sb.append(str); } } catch (Exception ex) { System.out.println("出现错误"); } finally { try { // 10. 关闭流 if (in != null) { in.close(); } } catch (IOException ex) { } } // 1.这里是JSON字符串 String result = sb.toString(); // 2.JSON字符串转换为JSON对象 JSONObject object = JSONObject.parseObject(result); // 3.通过JSON对象获取result这个对象属性 JSONObject result1 = (JSONObject) object.get("result"); // 4.然后再次获取address_component这个对象。 JSONObject address_component = (JSONObject) result1.get("address_component"); return address_component; } public static void main(String[] args) throws Exception { System.out.println(getLatAndLng("43.828281", "125.29985", "你的密钥")); // 密钥⭐ } }
3.封装的万能工具类 💮
package com.jsxs.TencentMapUtils; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; /** * @Author Jsxs * @Date 2023/4/5 13:37 * @PackageName:com.jsxs.TencentMapUtils * @ClassName: TencentMapUtils * @Description: TODO * @Version 1.0 */ public class TencentMapUtils { // 1. 工具类 public static StringBuffer getTencentMap(String keyUrl){ //请求的url URL url = null; //请求的输入流 BufferedReader in = null; //输入流的缓冲 StringBuffer sb = new StringBuffer(); try { url = new URL(keyUrl); in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8")); String str = null; //一行一行进行读入 while ((str = in.readLine()) != null) { sb.append(str); } } catch (Exception ex) { System.out.println("读取出现异常..."); } finally { try { if (in != null) { in.close(); //关闭流 } } catch (IOException ex) { System.out.println("关闭资源出现异常"); }finally { return sb; } } } }
4.根据地址获取经纬度 (地址解析)
官方文档: https://lbs.qq.com/service/webService/webServiceGuide/webServiceGeocoder
package com.jsxs.TentcentMap.addressResolution; import com.alibaba.fastjson.JSONObject; import com.jsxs.TencentMapUtils.TencentMapUtils; import java.io.*; import java.net.URL; import java.util.HashMap; import java.util.Map; /** * @Author Jsxs * @Date 2023/4/4 20:28 * @PackageName:com.jsxs * @ClassName: getLineByAdress * @Description: TODO 地址转经纬度 * @Version 1.0 */ public class addressResolution { public static Map<String, Object> getURLContent(String address) { //这里需要使用你的key值⭐ String urlStr = "https://apis.map.qq.com/ws/geocoder/v1/?address=" + address + "&key=xxxx"; // 1.这里是JSON字符串-通过哇能工具类获取 String result = TencentMapUtils.getTencentMap(urlStr).toString(); // 2.JSON字符串转JSON对象 JSONObject jsonObject = JSONObject.parseObject(result); // 3. 强制转换 Object转换为 JSONObject-获取键值是: result的值 JSONObject result1 = (JSONObject)jsonObject.get("result"); // 4.在result的基础上继续获取键值是: location的值 JSONObject location = (JSONObject)result1.get("location"); // 5.在location的基础上获取: 经纬度的值 String lng = location.get("lng")+""; String lat = location.get("lat")+""; Map<String, Object> map = new HashMap<>(); map.put("lat", lat); map.put("lng", lng); return map; } public static void main(String[] args) { Map<String, Object> map = getURLContent("吉林省长春市净月国家高新技术产业开发区福祉大路1016号长春工业大学人文信息学院"); System.out.println("lat=" + map.get("lat") + ",lng=" + map.get("lng")); } }
5.查找周边 (地点搜索)
package com.jsxs.TentcentMap.locationSearch; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.jsxs.TencentMapUtils.TencentMapUtils; /** * @Author Jsxs * @Date 2023/4/5 13:55 * @PackageName:com.jsxs.TentcentMap.locationSearch * @ClassName: locationSearch * @Description: TODO 地点搜索 * @Version 1.0 */ public class locationSearch { public static JSONArray getLocationSearch(String latitude, String longitude, String radius, String keyword, String page_size, String page_index, String key) { // latitude->维度 经度->longitude String keyURL="https://apis.map.qq.com/ws/place/v1/search?boundary=nearby("+latitude+","+longitude+","+radius+")&keyword="+keyword+"&page_size="+page_size+"&page_index="+page_index+"&key="+key; // 1.JSON字符串 String s = TencentMapUtils.getTencentMap(keyURL).toString(); // 2.JSON字符串转换为JSON对象 JSONObject jsonObject = JSONObject.parseObject(s); // 3.获取JSON数组 JSONArray data = (JSONArray)jsonObject.get("data"); return data; } public static void main(String[] args) { // 经度 维度 半径/米 关键字 显示条数 显示页数 密钥 JSONArray array = getLocationSearch("40.040589", "116.273543", "1000", "公园", "10", "1", "你的密钥"); // 遍历Json数组 for (int i = 0; i < array.size(); i++) { System.out.println(array.getJSONObject(i).get("address")); } } }