addrparser根據(jù)經(jīng)緯度解析省市區(qū)信息工具包
用于離線高效的解析中國范圍內(nèi)的經(jīng)緯度為省市區(qū)信息,省市區(qū)信息包括: 行政區(qū)劃編碼、行政區(qū)劃中文名稱、行政區(qū)域的中心點(diǎn)經(jīng)緯度,行政區(qū)域的邊界點(diǎn)經(jīng)緯度集合。
離線數(shù)據(jù)可基于文件,也可存于MySQL數(shù)據(jù)庫中。
依賴安裝
Maven
<dependency> <groupId>tech.spiro</groupId> <artifactId>addrparser</artifactId> <version>1.0</version> </dependency>
使用說明
下載數(shù)據(jù)文件
china-region-20180418.zip 解壓后得到文件:china-region.json,文件中包括了所有的省市區(qū)行政區(qū)域信息,包括編碼、名稱、中心點(diǎn)、邊界點(diǎn)集合。
經(jīng)緯度數(shù)據(jù)基于GCJ-02坐標(biāo)系
方式一 (文件)
直接使用china-region.json文件作為基礎(chǔ)數(shù)據(jù)。
示例代碼:
// china-region.json文件作為基礎(chǔ)數(shù)據(jù)
RegionDataInput regionDataInput = new JSONFileRegionDataInput("path/china-region.json");
// 創(chuàng)建并初始化位置解析引擎,一般配置為全局單例
LocationParserEngine engine = new LocationParserEngine(regionDataInput);
// 初始化,加載數(shù)據(jù),比較耗時(shí)
engine.init();
// 執(zhí)行解析操作
Location location = engine.parse(118.750934,32.038634);
// 獲取省市區(qū)信息
RegionInfo provInfo = location.getProv();
RegionInfo cityInfo = location.getCity();
RegionInfo districtInfo = location.getDistrict();
方式二 (MySQL)
將數(shù)據(jù)導(dǎo)入mysql數(shù)據(jù)庫,使用mysql中數(shù)據(jù)作為基礎(chǔ)數(shù)據(jù)。
下載數(shù)據(jù)導(dǎo)入工具: addrparser-1.0.tar.gz
解壓后,進(jìn)入addrparser/bin目錄,執(zhí)行jsonfile2mysql.sh (Linux) 或 jsonfile2mysql.bat (Windows)
usage: JSONFile2MySQL -a,--password <arg> MySQL password -d,--db <arg> MySQL database -f,--file <arg> Json region data file path 上述china-region.json文件的路徑 -h,--host <arg> MySQL host -i,--init Init table schema -p,--port <arg> MySQL port, default 3306 -t,--table-name <arg> Default: 'region_data', Region data table name -u,--user <arg> MySQL user
如果不指定-t,使用默認(rèn)表名region_data
示例代碼:
// javax.sql.DataSource 數(shù)據(jù)源 DataSource ds = ...; // 關(guān)系型數(shù)據(jù)庫區(qū)域數(shù)據(jù)輸入 RegionDataInput regionDataInput = new RdbmsRegionDataInput(ds); // 如果上述命令如果指定了定制的表名,則該處也要指定表名。 // RegionDataInput regionDataInput = new RdbmsRegionDataInput(ds, customTableName); // 創(chuàng)建并初始化位置解析引擎,一般配置為全局單例 LocationParserEngine engine = new LocationParserEngine(regionDataInput); // 初始化,加載數(shù)據(jù),比較耗時(shí) engine.init(); // 執(zhí)行解析操作 Location location = engine.parse(118.750934,32.038634); // 獲取省市區(qū)信息 RegionInfo provInfo = location.getProv(); RegionInfo cityInfo = location.getCity(); RegionInfo districtInfo = location.getDistrict();
評(píng)論
圖片
表情
