废话不说了直接上代码
import java.io.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by cw on 2017/8/29.
*
*/
public class AddressLngLatExchange {
private static final String KEY = "389880a06e3f893ea46036f030c94700";
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
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
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;
}
具体代码请去博客内查看,这里被字数限制了.