本文记录调用百度API获取经纬度使用记录,包含2个,一个是获取某个地点的经纬度信息,一个是以某个经纬度为中心点,查询方圆内的坐标。
百度API地址:http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-placeapi
一.获取某个地点的经纬度信息
工具类:
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.utils.URIBuilder; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.RestController; import java.io.IOException; import java.math.BigDecimal; import java.net.URISyntaxException; @RestController public class ApiInvoker { static class Position { BigDecimal lat; BigDecimal lng; Position(BigDecimal lat, BigDecimal lng) { this.lat = lat; this.lng = lng; } public String toString() { return lng + "," + lat; } }
public static String getPositionXY(String keyword, String boundary) throws IOException, URISyntaxException { JSONObject result = ApiInvoker.callApi(keyword, boundary); if (result != null) { Position position = getPostion(result); if (position != null) { System.out.println("position : " + position.lng + "," + position.lat); return position.lng + "," + position.lat; }else { System.out.println("解析坐标失败"); return null; } } return null; } public static Position getPostion(JSONObject object) { if (object == null) return null; JSONArray array = (JSONArray) object.get("results"); if (array == null || array.size() < 1) return null; JSONObject location = null; location = (JSONObject) ((JSONObject)array.get(0)).get("location"); if (location == null) return null; try { return new Position((BigDecimal)location.get("lat"), (BigDecimal)location.get("lng")); } catch (Exception e) { e.printStackTrace(); } return null; } /** * * @param keyword 检索关键字 * @param boundary 检索行政区划区域 * @return * @throws IOException * @throws URISyntaxException */ public static JSONObject callApi(String keyword, String boundary) throws IOException, URISyntaxException { HttpClient client = HttpClients.createDefault(); URIBuilder uriBuilder = new URIBuilder("http://api.map.baidu.com/place/v2/search"); uriBuilder.setParameter("query", keyword) .setParameter("region", boundary) .setParameter("output", "json") .setParameter("ak", "nPQRbkNFGmz05mAwVn16gsM24VSV6mh7"); HttpGet httpGet = new HttpGet(uriBuilder.build()); HttpEntity entity = null; try { HttpResponse response = client.execute(httpGet); entity = response.getEntity(); } catch (IOException e) { System.out.println("调用失败"); } if (entity != null) { String jsonString = EntityUtils.toString(entity, "utf-8"); if (!StringUtils.isEmpty(jsonString)) return JSON.parseObject(jsonString); } return null; } public static String buildBoundary(String area1, String area2) { StringBuilder sb = new StringBuilder("region("); sb.append(area1); sb.append(area2); sb.append(",0)"); return sb.toString(); } }
二.以某个经纬度为中心点,查询方圆内的坐标
工具类:
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.utils.URIBuilder; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.RestController; import java.io.IOException; import java.math.BigDecimal; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; /** * 百度api获取范围地址 */ @RestController public class ApiNearInvoker { private Logger log = LoggerFactory.getLogger(this.getClass()); public static void main(String[] args) throws IOException, URISyntaxException { List<Position> postion = ApiNearInvoker.callApi("餐厅", "27.535303,109.950842", "20000"); if (null != postion || postion.size() < 1) for (Position position : postion) { String s = position.toString(); System.out.println(s); } else System.out.println("解析坐标失败"); } public static List<Position> getPostion(JSONObject object) { if (object == null) return null; JSONArray array = (JSONArray) object.get("results"); if (array == null || array.size() < 1) return null; List<Position> list = new ArrayList<>(); try { JSONObject location = null; Position position = null; for (Object o : array) { position = new Position(); JSONObject json = (JSONObject) o; location = (JSONObject) json.get("location"); position.setLat((BigDecimal) location.get("lat")); position.setLng((BigDecimal) location.get("lng")); position.setAddress((String) json.get("address")); position.setCity((String) json.get("city")); position.setArea((String) json.get("area")); position.setProvince((String) json.get("province")); position.setTelephone((String) json.get("telephone")); position.setName((String) json.get("name")); list.add(position); } return list; } catch (Exception e) { e.printStackTrace(); } return null; } /** * * @param query 查询名字:餐厅,饭店。。。 * @param location 中心点坐标:纬度,经度 * @param radius 方圆,单位米 * @return * @throws IOException * @throws URISyntaxException */ public static List<Position> callApi(String query, String location, String radius) throws IOException, URISyntaxException { HttpClient client = HttpClients.createDefault(); URIBuilder uriBuilder = new URIBuilder("http://api.map.baidu.com/place/v2/search"); uriBuilder.setParameter("query", query) .setParameter("location", location) .setParameter("radius", radius) .setParameter("output", "json") .setParameter("ak", "nPQRbkNFGmz05mAwVn16gsM24VSV6mh7"); HttpGet httpGet = new HttpGet(uriBuilder.build()); HttpEntity entity = null; try { HttpResponse response = client.execute(httpGet); entity = response.getEntity(); } catch (IOException e) { // log.info("调用失败", e); System.out.println("调用失败:" + e); } if (entity != null) { String jsonString = EntityUtils.toString(entity, "utf-8"); if (!StringUtils.isEmpty(jsonString)) { JSONObject jsonObject = JSON.parseObject(jsonString); if (jsonObject != null) { List<Position> postion = getPostion(jsonObject); return postion; } } } return null; } }
实体类:
import java.math.BigDecimal; public class Position { BigDecimal lat; BigDecimal lng; String area; String address; String province; String city; String name; String telephone; Position(){ } Position(BigDecimal lat, BigDecimal lng,String area,String address,String province,String city,String name,String telephone) { this.lat = lat; this.lng = lng; this.area=area; this.address=address; this.province=province; this.city=city; this.name=name; this.telephone=telephone; } public BigDecimal getLat() { return lat; } public void setLat(BigDecimal lat) { this.lat = lat; } public BigDecimal getLng() { return lng; } public void setLng(BigDecimal lng) { this.lng = lng; } public String getArea() { return area; } public void setArea(String area) { this.area = area; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTelephone() { return telephone; } public void setTelephone(String telephone) { this.telephone = telephone; } @Override public String toString() { return "Position{" + "lat=" + lat + ", lng=" + lng + ", area=‘" + area + ‘\‘‘ + ", address=‘" + address + ‘\‘‘ + ", province=‘" + province + ‘\‘‘ + ", city=‘" + city + ‘\‘‘ + ", name=‘" + name + ‘\‘‘ + ", telephone=‘" + telephone + ‘\‘‘ + ‘}‘; } }