通过高德api查询所有店铺地址信息

2024-06-24 13:28

本文主要是介绍通过高德api查询所有店铺地址信息,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

通过高德api查询所有店铺地址电话信息

  • 需求:通过高德api查询所有店铺地址信息
  • 需求分析
  • 具体实现
    • 1、申请高德appkey
    • 2、下载types city 字典值
    • 3、具体代码调用

需求:通过高德api查询所有店铺地址信息

需求分析

查询现有高德api发现现有接口关键字搜索API服务地址:

https://developer.amap.com/api/webservice/guide/api/search
参数需要主要参数:key、types、city
其中types city可以查询:
https://developer.amap.com/api/webservice/download

具体实现

1、申请高德appkey

地址:https://console.amap.com/dev/key/app

2、下载types city 字典值

并将字典值解析读取到代码中,如图
在这里插入图片描述

3、具体代码调用

类说明:

Api  调用方法
CityUtil 城市编码查询工具类
ReaderFile 文件读写工具类
Shop 地点实体类
ShopUtil 地点查询工具类

具体代码

package com.gaode;import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson2.JSON;import java.util.ArrayList;
import java.util.List;/*** 时间:2024/6/21* 描述:*/public class Api {/*** 需要设置自己的apikey*/public static String apiKey = "";public static void main(String[] args)  throws Exception{//查询单个城市List<Shop> AllShopList = new ArrayList<>();List<String> types = ReaderFile.getTypes();List<String> citys = ReaderFile.getCitys();for (int i = 0; i < types.size(); i++) {String type = types.get(i);for (int j = 0; j < citys.size(); j++) {String city = citys.get(i);List<Shop> shopList = ShopUtil.queryShops(city,type);if(CollectionUtil.isNotEmpty(shopList)){AllShopList.addAll(shopList);}if(CollectionUtil.isNotEmpty(AllShopList)){System.out.println(shopList.size());System.out.println(shopList);continue;}}}System.out.println("结束===================");System.out.println("=============="+AllShopList.size());System.out.println(JSON.toJSONString(AllShopList));}public static  List<Shop> queryAll() {List<Shop> shopList = new ArrayList<>();List<String> list = CityUtil.queryCitys();if (CollectionUtil.isNotEmpty(list)) {for (String city : list) {List<Shop> oneShopList = ShopUtil.queryShops(city,null);if (CollectionUtil.isNotEmpty(oneShopList)) {shopList.addAll(oneShopList);}}}return shopList;}
}
package com.gaode;/*** 描述:*/import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;public class CityUtil {public static List<String> queryCitys(){String apiKey  = Api.apiKey;List<String> ls = new ArrayList<>();// 获取所有城市列表String cityListUrl = "https://restapi.amap.com/v3/config/district?key=" + apiKey + "&subdistrict=1";String cityListResponse = null;try {cityListResponse = sendGetRequest(cityListUrl);} catch (Exception e) {throw new RuntimeException(e);}JSONObject cityListJson = new JSONObject(cityListResponse);JSONArray cityList = cityListJson.getJSONArray("districts").getJSONObject(0).getJSONArray("districts");for (int i = 0; i < cityList.size(); i++) {JSONObject city = cityList.getJSONObject(i);String cityName = city.get("name", String.class);ls.add(cityName);}return ls;}// 发送GET请求private static String sendGetRequest(String url) throws Exception {StringBuilder response = new StringBuilder();HttpURLConnection connection = null;BufferedReader reader = null;try {URL requestUrl = new URL(url);connection = (HttpURLConnection) requestUrl.openConnection();connection.setRequestMethod("GET");connection.setConnectTimeout(5000);connection.setReadTimeout(5000);reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));String line;while ((line = reader.readLine()) != null) {response.append(line);}} finally {if (reader != null) {reader.close();}if (connection != null) {connection.disconnect();}}return response.toString();}
}
package com.gaode;import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;/*** 时间:2024/6/23* 描述:*/public class ReaderFile {public static void main(String[] args)throws Exception {System.out.println(getTypes());System.out.println(getCitys());}public static  List<String> getCitys() throws Exception {String filePath = "D:\\workspace\\codeStr\\studyCode\\address\\src\\main\\resources\\citycode.xlsx";FileInputStream file = new FileInputStream(filePath);XSSFWorkbook workbook = new XSSFWorkbook(file);Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表Map<String,String> map = new HashMap<>();String cellValue = "";List<String> result = new ArrayList<>();try{for (Row row : sheet) {for (Cell cell : row) {int columnIndex = cell.getColumnIndex();String stringCellValue =null;if(columnIndex==2){try{stringCellValue = cell.getStringCellValue();}catch (Exception e){stringCellValue = String.valueOf(cell.getNumericCellValue());}stringCellValue = stringCellValue.replace(".0","");}if(stringCellValue!=null){String substring = stringCellValue;if(!substring.equals("citycode")){map.put(substring,substring);}continue;}}}for (String key : map.keySet()){result.add(key);}}catch (Exception e){System.out.println("==========="+cellValue);e.printStackTrace();}return result;}public static List<String> getTypes() throws Exception {String filePath = "D:\\workspace\\codeStr\\studyCode\\address\\src\\main\\resources\\poi.xlsx";List<String> list = new ArrayList<>();FileInputStream file = new FileInputStream(filePath);XSSFWorkbook workbook = new XSSFWorkbook(file);Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表Map<String,String> map = new HashMap<>();String cellValue = "";List<String> result = new ArrayList<>();try{for (Row row : sheet) {for (Cell cell : row) {int columnIndex = cell.getColumnIndex();String stringCellValue =null;if(columnIndex==1){try{stringCellValue = cell.getStringCellValue();}catch (Exception e){stringCellValue = String.valueOf(cell.getNumericCellValue());}stringCellValue = stringCellValue.replace(".0","");}if(stringCellValue!=null){String substring = stringCellValue.substring(0, 2);if(!substring.equals("NE")){map.put(substring,substring);}continue;}}}for (String key : map.keySet()){result.add(key);}}catch (Exception e){e.printStackTrace();}return result;}}
package com.gaode;/*** 时间:2024/6/21* 描述:*/import lombok.Data;import java.util.List;@Data
public class Shop {private String pname;private String cityname;private String address;private String type;private String location;private String tel;private String name;
}
package com.gaode;/*** 时间:2024/6/21* 描述:*/import com.alibaba.fastjson2.JSON;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.json.JSONArray;
import org.json.JSONObject;import java.io.IOException;
import java.util.ArrayList;
import java.util.List;public class ShopUtil {public static  List<Shop>   queryShops(String city,String types) {OkHttpClient client = new OkHttpClient();String apiKey = Api.apiKey;// 设置offset参数来获取更多的数据int pageSize = 100; // 每页显示的POI数量int currentPage = 0; // 当前页码JSONObject poi = null;List<Shop> shopList = new ArrayList<>();while (true) {int offset = currentPage * pageSize;String url = "https://restapi.amap.com/v3/place/text?key=" + apiKey + "&types="+types+"&city=" + city + "&offset=" + offset + "&page=" + (currentPage + 1);
//            String url = "https://restapi.amap.com/v3/place/text?key=" + apiKey +"&offset=" + offset + "&page=" + (currentPage + 1);Request request = new Request.Builder().url(url).build();try (Response response = client.newCall(request).execute()) {if (!response.isSuccessful()) {throw new IOException("Unexpected code " + response);}String responseBody = response.body().string();JSONObject jsonResponse = new JSONObject(responseBody);if (jsonResponse.has("pois")) {JSONArray pois = jsonResponse.getJSONArray("pois");if (pois.length() == 0) {// 如果当前页没有数据,说明已经查询完毕break;}for (int i = 0; i < pois.length(); i++) {poi = pois.getJSONObject(i);Shop shop = new Shop();shop.setPname(JSON.toJSONString(poi.get("pname")));shop.setCityname(JSON.toJSONString(poi.get("cityname")));shop.setAddress(JSON.toJSONString(poi.get("address")));shop.setType(JSON.toJSONString(poi.get("type")));shop.setLocation(JSON.toJSONString(poi.get("location")));shop.setTel(JSON.toJSONString(poi.get("tel")));shop.setName(JSON.toJSONString(poi.getString("name")));shopList.add(shop);}} else {System.out.println("没有找到相关的店铺信息。");break;}currentPage++;} catch (Exception e) {System.out.println("异常信息-----" + JSON.toJSONString(poi));e.printStackTrace();break;}}return shopList;}
}

以上为具体代码
pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>address</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><hutool.version>5.8.22</hutool.version><lombok.version>1.18.26</lombok.version><druid.version>1.1.20</druid.version><mybatis.springboot.version>3.0.2</mybatis.springboot.version><mysql.version>8.0.11</mysql.version><swagger3.version>2.2.0</swagger3.version><mapper.version>4.2.3</mapper.version><fastjson2.version>2.0.40</fastjson2.version><persistence-api.version>1.0.2</persistence-api.version><spring.boot.test.version>3.1.5</spring.boot.test.version><spring.boot.version>3.2.0</spring.boot.version><spring.cloud.version>2023.0.0</spring.cloud.version><spring.cloud.alibaba.version>2022.0.0.0-RC2</spring.cloud.alibaba.version></properties><dependencies><dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>3.1.1</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>4.0.1</version><scope>provided</scope></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>4.0.1</version></dependency><!--SpringCloud consul discovery --><!-- 引入自己定义的api通用包 --><dependency><groupId>com.atguigu.cloud</groupId><artifactId>cloud-api-commons</artifactId><version>1.0-SNAPSHOT</version></dependency><!--SpringBoot集成druid连接池--><!--mybatis和springboot整合--><!--hutool--><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>${hutool.version}</version></dependency><!-- fastjson2 --><dependency><groupId>com.alibaba.fastjson2</groupId><artifactId>fastjson2</artifactId><version>${fastjson2.version}</version></dependency><!--lombok--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.28</version><scope>provided</scope></dependency><!--test--><dependency><groupId>org.springdoc</groupId><artifactId>springdoc-openapi-starter-webmvc-ui</artifactId><version>${swagger3.version}</version></dependency><dependency><groupId>com.alibaba.fastjson2</groupId><artifactId>fastjson2</artifactId><version>${fastjson2.version}</version></dependency><!-- 其他依赖项 --><dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>2.2.11</version></dependency><dependency><groupId>com.squareup.okhttp3</groupId><artifactId>okhttp</artifactId><version>4.9.1</version></dependency><dependency><groupId>org.json</groupId><artifactId>json</artifactId><version>20210307</version></dependency></dependencies></project>

这篇关于通过高德api查询所有店铺地址信息的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1090277

相关文章

Python中使用正则表达式精准匹配IP地址的案例

《Python中使用正则表达式精准匹配IP地址的案例》Python的正则表达式(re模块)是完成这个任务的利器,但你知道怎么写才能准确匹配各种合法的IP地址吗,今天我们就来详细探讨这个问题,感兴趣的朋... 目录为什么需要IP正则表达式?IP地址的基本结构基础正则表达式写法精确匹配0-255的数字验证IP地

MySQL高级查询之JOIN、子查询、窗口函数实际案例

《MySQL高级查询之JOIN、子查询、窗口函数实际案例》:本文主要介绍MySQL高级查询之JOIN、子查询、窗口函数实际案例的相关资料,JOIN用于多表关联查询,子查询用于数据筛选和过滤,窗口函... 目录前言1. JOIN(连接查询)1.1 内连接(INNER JOIN)1.2 左连接(LEFT JOI

MySQL 中查询 VARCHAR 类型 JSON 数据的问题记录

《MySQL中查询VARCHAR类型JSON数据的问题记录》在数据库设计中,有时我们会将JSON数据存储在VARCHAR或TEXT类型字段中,本文将详细介绍如何在MySQL中有效查询存储为V... 目录一、问题背景二、mysql jsON 函数2.1 常用 JSON 函数三、查询示例3.1 基本查询3.2

MySQL中动态生成SQL语句去掉所有字段的空格的操作方法

《MySQL中动态生成SQL语句去掉所有字段的空格的操作方法》在数据库管理过程中,我们常常会遇到需要对表中字段进行清洗和整理的情况,本文将详细介绍如何在MySQL中动态生成SQL语句来去掉所有字段的空... 目录在mysql中动态生成SQL语句去掉所有字段的空格准备工作原理分析动态生成SQL语句在MySQL

MySQL中的交叉连接、自然连接和内连接查询详解

《MySQL中的交叉连接、自然连接和内连接查询详解》:本文主要介绍MySQL中的交叉连接、自然连接和内连接查询,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、引入二、交php叉连接(cross join)三、自然连接(naturalandroid join)四

mysql的基础语句和外键查询及其语句详解(推荐)

《mysql的基础语句和外键查询及其语句详解(推荐)》:本文主要介绍mysql的基础语句和外键查询及其语句详解(推荐),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋... 目录一、mysql 基础语句1. 数据库操作 创建数据库2. 表操作 创建表3. CRUD 操作二、外键

Mybatis 传参与排序模糊查询功能实现

《Mybatis传参与排序模糊查询功能实现》:本文主要介绍Mybatis传参与排序模糊查询功能实现,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、#{ }和${ }传参的区别二、排序三、like查询四、数据库连接池五、mysql 开发企业规范一、#{ }和${ }传参的

浅谈mysql的sql_mode可能会限制你的查询

《浅谈mysql的sql_mode可能会限制你的查询》本文主要介绍了浅谈mysql的sql_mode可能会限制你的查询,这个问题主要说明的是,我们写的sql查询语句违背了聚合函数groupby的规则... 目录场景:问题描述原因分析:解决方案:第一种:修改后,只有当前生效,若是mysql服务重启,就会失效;

MySQL多列IN查询的实现

《MySQL多列IN查询的实现》多列IN查询是一种强大的筛选工具,它允许通过多字段组合快速过滤数据,本文主要介绍了MySQL多列IN查询的实现,具有一定的参考价值,感兴趣的可以了解一下... 目录一、基础语法:多列 IN 的两种写法1. 直接值列表2. 子查询二、对比传统 OR 的写法三、性能分析与优化1.

基于Flask框架添加多个AI模型的API并进行交互

《基于Flask框架添加多个AI模型的API并进行交互》:本文主要介绍如何基于Flask框架开发AI模型API管理系统,允许用户添加、删除不同AI模型的API密钥,感兴趣的可以了解下... 目录1. 概述2. 后端代码说明2.1 依赖库导入2.2 应用初始化2.3 API 存储字典2.4 路由函数2.5 应