原文请见于:https://blog.csdn.net/Java_wucao/article/details/77800891 内容部分没有修改。
AddressLngLatExchange:
package dwz.web.gdmap;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import dwz.common.util.StringUtils;
import dwz.persistence.beans.gdmap.Shop;
import jxl.Cell;
import jxl.Workbook;
import jxl.read.biff.BiffException;
/**
* @author 作者姓名:cjw
* @version 创建时间:2018年4月19日 下午3:01:28 java 从高德地图上爬取数据类
*/
public class AddressLngLatExchange {
private static final String KEY = "7ff46e1a64fade07c3cbc5eee118ad1d";
private static final String OUTPUT = "JSON";
private static final String GET_LNG_LAT_URL = "http://restapi.amap.com/v3/geocode/geo";
private static final String GET_LNG_PIO_URL = "http://restapi.amap.com/v3/place/polygon";
private static final Logger LOGGER = LoggerFactory.getLogger(AddressLngLatExchange.class);
// 获取指定地点经纬度
public static String[] getLngLatFromOneAddr(String address) {
if (StringUtils.isBlank(address)) {
LOGGER.error("地址(" + address + ")为null或者空");
return null;
}
Map params = new HashMap();
params.put("address", address);
params.put("output", OUTPUT);
params.put("key", KEY);
String result = HttpUtils.URLPost(GET_LNG_LAT_URL, params, "");
JSONObject jsonObject = JSONObject.parseObject(result);
String[] lngLatArr = new String[2];
// 拿到返回报文的status值,高德的该接口返回值有两个:0-请求失败,1-请求成功;
int status = Integer.valueOf(jsonObject.getString("status"));
if (status == 1) {
JSONArray jsonArray = jsonObject.getJSONArray("geocodes");
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject json = jsonArray.getJSONObject(i);
String lngLat = json.getString("location");
lngLatArr = lngLat.split(",");
}
} else {
String errorMsg = jsonObject.getString("info");
LOGGER.error("地址(" + address + ")" + errorMsg);
}
return lngLatArr;
}
public static List initialData(String lonLat, String keyword, List shopListSon) {
if (StringUtils.isBlank(keyword)) {
LOGGER.error("地址(" + keyword + ")为null或者空");
}
Map params = new HashMap();
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
params.put("polygon", lonLat);// "118.21,29.11;120.30,30.33"
params.put("output", OUTPUT);
params.put("keywords", keyword);
params.put("offset", "20");
params.put("page", "1");
params.put("key", KEY);
String result = HttpUtils.URLGet(GET_LNG_PIO_URL, params, "UTF-8");
JSONObject jsonObject = JSONObject.parseObject(result);
int statusOne = Integer.valueOf(jsonObject.getString("status"));
// 第一次获取数据时做的判断
if (statusOne == 1) {
int count = Integer.valueOf(jsonObject.getString("count"));
int pageNumber = count / 20;
int remainder = count % 20;
if (remainder > 0)
pageNumber = pageNumber + 1;
for (int i = 1; i <= pageNumber; i++) {
params.put("page", String.valueOf(i));
result = HttpUtils.URLGet(GET_LNG_PIO_URL, params, "UTF-8");
JSONObject jsonObject2 = JSONObject.parseObject(result);
System.out.println("+++++++++" + result);
// 拿到返回报文的status值,高德的该接口返回值有两个:0-请求失败,1-请求成功;
int status = Integer.valueOf(jsonObject2.getString("status"));
if (status == 1) {
JSONArray jsonArray = jsonObject2.getJSONArray("pois");
if (jsonArray.size() > 0) {
for (int j = 0; j < jsonArray.size(); j++) {
Shop shop = new Shop();
JSONObject jsonObject1 = jsonArray.getJSONObject(j);
shop.setShopName(jsonObject1.getString("name"));
shop.setSpecificAddress(jsonObject1.getString("address"));
shop.setId(jsonObject1.getString("id"));
String[] initLonLat = jsonObject1.getString("location").split(",");
shop.setLongitude(initLonLat[0]);
shop.setLatitude(initLonLat[1]);
shopListSon.add(shop);
// DBObject doci = new BasicDBObject("shopId",
// "300"+i).append("shopName",
// "人生得意"+i).append("shopStatus",0).append("specificAddress","天堂"+i).append("gps",
// new Point(new Position(lon, lat)));
}
}
} else {
String errorMsg = jsonObject.getString("info");
LOGGER.error("地址(" + keyword + ")" + errorMsg);
}
}
}
return shopListSon;
}
// 从高德地图上取数据
// public static void main(String[] args) {
// List listShop =new ArrayList<>();
// //东经118°21′-120°30′,北纬29°11′-30°33′。杭州位置
// for(double i=118.20;i<=120.31;i=i+0.1){
// for(double j=29.10;j<=30.33;j=j+0.1){
// List listShopSon =new ArrayList<>();
// double lonHead=i;
// double latHead=j;
// double lonTail=i+0.1;
// double latTail=j+0.1;
// String LonLat=lonHead+","+latHead+";"+lonTail+","+latTail;
// listShopSon =initialData(LonLat,"便利店",listShopSon);
// for(int n=0;n0){
// listShop.addAll(listShopSon);
// }
// System.out.println("ListShop的大小:"+listShop.size());
// double d =Distance(lonHead,latHead,lonTail,latTail);
// System.out.println("两点距离"+d);
//
// }
//
// }
//
// System.out.println("ListShop的大小:"+listShop.size());
// creatExcel(listShop);
// }
public static void main(String[] args) {
readFile("D:\\geode\\高德便利店地图数据.xls");
}
// 写入excel中
public static void creatExcel(List shopList) {
HSSFWorkbook workbook = new HSSFWorkbook();
// 第二部,在workbook中创建一个sheet对应excel中的sheet
HSSFSheet sheet = workbook.createSheet("高德地图数据");
// 第三部,在sheet表中添加表头第0行,老版本的poi对sheet的行列有限制
HSSFRow row = sheet.createRow(0);
// 第四步,创建单元格,设置表头
HSSFCell cell = row.createCell(0);
cell.setCellValue("店铺id");
cell = row.createCell(1);
cell.setCellValue("店铺名称");
cell = row.createCell(2);
cell.setCellValue("店铺地址");
cell = row.createCell(3);
cell.setCellValue("经度");
cell = row.createCell(4);
cell.setCellValue("纬度");
// 第五步,写入实体数据,实际应用中这些数据从数据库得到,对象封装数据,集合包对象。对象的属性值对应表的每行的值
for (int i = 0; i < shopList.size(); i++) {
HSSFRow row1 = sheet.createRow(i + 1);
Shop shop = shopList.get(i);
// 创建单元格设值
row1.createCell(0).setCellValue(shop.getId());
row1.createCell(1).setCellValue(shop.getShopName());
row1.createCell(2).setCellValue(shop.getSpecificAddress());
row1.createCell(3).setCellValue(shop.getLongitude());
row1.createCell(4).setCellValue(shop.getLatitude());
}
// 将文件保存到指定的位置
try {
FileOutputStream fos = new FileOutputStream("D:\\geode\\高德便利店地图数据.xls");
workbook.write(fos);
System.out.println("写入成功");
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static double Distance(double long1, double lat1, double long2, double lat2) {
double a, b, R;
R = 6371; // 地球半径 6371km
lat1 = lat1 * Math.PI / 180.0;
lat2 = lat2 * Math.PI / 180.0;
a = lat1 - lat2;
b = (long1 - long2) * Math.PI / 180.0;
double d;
double sa2, sb2;
sa2 = Math.sin(a / 2.0);
sb2 = Math.sin(b / 2.0);
d = 2 * R * Math.asin(Math.sqrt(sa2 * sa2 + Math.cos(lat1) * Math.cos(lat2) * sb2 * sb2));
BigDecimal bigDecimal = new BigDecimal(d * 1000);
Double din = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
return din;
}
public static List readFile(String filename) {
List shopList = new ArrayList<>();
Workbook wb = null;
Cell cell = null;
try {
File f = new File(filename);
InputStream in = new FileInputStream(f); // 创建输入流
wb = Workbook.getWorkbook(in); // 获取Excel文件对象
jxl.Sheet s = wb.getSheet(0); // 获取文件的指定工作表,默认为第一个
String value = null;
for (int i = 1; i < s.getRows(); i++) {// 表头目录不需要,从第一行开始
Shop shop = new Shop();
for (int j = 0; j < s.getColumns(); j++) {
cell = s.getCell(j, i);
value = cell.getContents();
if (j == 0) {
shop.setId(value);
} else if (j == 1) {
shop.setShopName(value);
} else if (j == 2) {
shop.setSpecificAddress(value);
} else if (j == 3) {
shop.setLongitude(value);
} else if (j == 4) {
shop.setLatitude(value);
}
// System.out.println("value:"+value);
}
shopList.add(shop);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BiffException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return shopList;
}
}
HttpUtils:
package dwz.web.gdmap;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
/**
* @author 作者姓名:cjw
* @version 创建时间:2018年4月19日 下午3:12:33 类说明
*/
public class HttpUtils {
private static Log log = LogFactory.getLog(HttpUtils.class);
/**
* 定义编码格式 UTF-8
*/
public static final String URL_PARAM_DECODECHARSET_UTF8 = "UTF-8";
/**
* 定义编码格式 GBK
*/
public static final String URL_PARAM_DECODECHARSET_GBK = "GBK";
private static final String URL_PARAM_CONNECT_FLAG = "&";
private static final String EMPTY = "";
private static MultiThreadedHttpConnectionManager connectionManager = null;
private static int connectionTimeOut = 25000;
private static int socketTimeOut = 25000;
private static int maxConnectionPerHost = 20;
private static int maxTotalConnections = 20;
private static HttpClient client;
static {
connectionManager = new MultiThreadedHttpConnectionManager();
connectionManager.getParams().setConnectionTimeout(connectionTimeOut);
connectionManager.getParams().setSoTimeout(socketTimeOut);
connectionManager.getParams().setDefaultMaxConnectionsPerHost(maxConnectionPerHost);
connectionManager.getParams().setMaxTotalConnections(maxTotalConnections);
client = new HttpClient(connectionManager);
}
/**
* POST方式提交数据
*
* @param url
* 待请求的URL
* @param params
* 要提交的数据
* @param enc
* 编码
* @return 响应结果
* @throws IOException
* IO异常
*/
public static String URLPost(String url, Map params, String enc) {
enc = URL_PARAM_DECODECHARSET_UTF8;
String response = EMPTY;
PostMethod postMethod = null;
try {
postMethod = new PostMethod(url);
postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=" + enc);
// 将表单的值放入postMethod中
Set keySet = params.keySet();
for (String key : keySet) {
String value = params.get(key);
postMethod.addParameter(key, value);
}
// 执行postMethod
int statusCode = client.executeMethod(postMethod);
if (statusCode == HttpStatus.SC_OK) {
response = postMethod.getResponseBodyAsString();
} else {
log.error("响应状态码 = " + postMethod.getStatusCode());
}
} catch (HttpException e) {
log.error("发生致命的异常,可能是协议不对或者返回的内容有问题", e);
e.printStackTrace();
} catch (IOException e) {
log.error("发生网络异常", e);
e.printStackTrace();
} finally {
if (postMethod != null) {
postMethod.releaseConnection();
postMethod = null;
}
}
return response;
}
/**
* GET方式提交数据
*
* @param url
* 待请求的URL
* @param params
* 要提交的数据
* @param enc
* 编码
* @return 响应结果
* @throws IOException
* IO异常
*/
public static String URLGet(String url, Map params, String enc) {
String response = EMPTY;
GetMethod getMethod = null;
StringBuffer strtTotalURL = new StringBuffer(EMPTY);
if (strtTotalURL.indexOf("?") == -1) {
strtTotalURL.append(url).append("?").append(getUrl(params, enc));
} else {
strtTotalURL.append(url).append("&").append(getUrl(params, enc));
}
log.debug("GET请求URL = \n" + strtTotalURL.toString());
try {
getMethod = new GetMethod(strtTotalURL.toString());
getMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=" + enc);
// 执行getMethod
int statusCode = client.executeMethod(getMethod);
if (statusCode == HttpStatus.SC_OK) {
response = getMethod.getResponseBodyAsString();
} else {
log.debug("响应状态码 = " + getMethod.getStatusCode());
}
} catch (HttpException e) {
log.error("发生致命的异常,可能是协议不对或者返回的内容有问题", e);
e.printStackTrace();
} catch (IOException e) {
log.error("发生网络异常", e);
e.printStackTrace();
} finally {
if (getMethod != null) {
getMethod.releaseConnection();
getMethod = null;
}
}
return response;
}
/**
* 据Map生成URL字符串
*
* @param map
* Map
* @param valueEnc
* URL编码
* @return URL
*/
private static String getUrl(Map map, String valueEnc) {
if (null == map || map.keySet().size() == 0) {
return (EMPTY);
}
StringBuffer url = new StringBuffer();
Set keys = map.keySet();
for (Iterator it = keys.iterator(); it.hasNext();) {
String key = it.next();
if (map.containsKey(key)) {
String val = map.get(key);
String str = val != null ? val : EMPTY;
try {
str = URLEncoder.encode(str, valueEnc);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
url.append(key).append("=").append(str).append(URL_PARAM_CONNECT_FLAG);
}
}
String strURL = EMPTY;
strURL = url.toString();
if (URL_PARAM_CONNECT_FLAG.equals(EMPTY + strURL.charAt(strURL.length() - 1))) {
strURL = strURL.substring(0, strURL.length() - 1);
}
return (strURL);
}
}
Shop:
package dwz.persistence.beans.gdmap;
/**
* @author 作者姓名:cjw
* @version 创建时间:2018年4月19日 下午3:16:44 商店实体类
*/
public class Shop {
/**
* 店铺id
*/
private String id;
/**
* 店铺名称
*/
private String shopName;
/**
* 店铺地址
*/
private String specificAddress;
/**
* 经度
*/
private String longitude;
/**
* 纬度
*/
private String latitude;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getShopName() {
return shopName;
}
public void setShopName(String shopName) {
this.shopName = shopName;
}
public String getSpecificAddress() {
return specificAddress;
}
public void setSpecificAddress(String specificAddress) {
this.specificAddress = specificAddress;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public Shop(String id, String shopName, String specificAddress, String longitude, String latitude) {
super();
this.id = id;
this.shopName = shopName;
this.specificAddress = specificAddress;
this.longitude = longitude;
this.latitude = latitude;
}
public Shop() {
super();
}
@Override
public String toString() {
return "Shop [id=" + id + ", shopName=" + shopName + ", specificAddress=" + specificAddress + ", longitude="
+ longitude + ", latitude=" + latitude + "]";
}
}
上面的代码有写入excel和读取excel的代码。不过要注意一下 我用得jar包不同。写入用的poi,读取用的是jxl.
---------所以需要的jar包: