java 百度高德实现地址转经纬度
来源:互联网 发布:键盘 编程 编辑:程序博客网 时间:2024/06/02 19:41
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection;import java.net.URLEncoder;import java.util.HashMap;import java.util.List;import java.util.Map;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.methods.GetMethod;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.alibaba.fastjson.JSONArray;import com.alibaba.fastjson.JSONObject;/** * 获取经纬度 * * @author jueyue 返回格式:Map<String,Object> map map.put("status", * reader.nextString());//状态 map.put("result", list);//查询结果 * list<map<String,String>> 密钥:f247cdb592eb43ebac6ccd27f796e2d2 */public class GetLatAndLngByBaidu { private static String loadJSON(String url) { StringBuilder json = new StringBuilder(); try { URL oracle = new URL(url); URLConnection yc = oracle.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream())); String inputLine = null; while ((inputLine = in.readLine()) != null) { json.append(inputLine); } in.close(); } catch (MalformedURLException e) { } catch (IOException e) { } return json.toString(); } /** * * 百度根据地址取经纬度 * * @param @param * address * @param @return * @param @throws * Exception * @return Map<String,Double> * @throws @author * ltj * @date 2017年9月20日下午9:50:00 */ public static Map<String, Double> getLngAndLat(String address) throws Exception { Map<String, Double> map = new HashMap<String, Double>(); String url = "http://api.map.baidu.com/geocoder/v2/?address=" + address + "&output=json&ak=你的AK"; String json = loadJSON(url); System.out.println(json); JSONObject obj = JSONObject.parseObject(json); if (obj != null) { if (obj.get("status").toString().equals("0")) { double lng = obj.getJSONObject("result").getJSONObject("location").getDouble("lng"); double lat = obj.getJSONObject("result").getJSONObject("location").getDouble("lat"); map.put("lng", lng); map.put("lat", lat); System.out.println("经度:" + lng + "---纬度:" + lat); } else { System.out.println("未找到相匹配的经纬度!"); throw new Exception(); } } return map; } /** * * 高德地图地址获取经纬度 * * @param @param * address * @param @return * @param @throws * Exception * @return Map<String,Double> * @throws @author * ltj * @date 2017年9月20日下午8:46:52 */ public static Map<String, Double> getLngAndLatByAmap(String address) throws Exception { address = address.trim(); Map<String, Double> map = new HashMap<String, Double>(); String url = "http://restapi.amap.com/v3/geocode/geo?address=" + URLEncoder.encode(address, "utf-8") + "&output=json&key=你的key"; GetMethod method = new GetMethod(url); method.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); HttpClient client = new HttpClient(); client.getHttpConnectionManager().getParams().setConnectionTimeout(10000); // 设置连接超时 int status = client.executeMethod(method); if (status == 200) { String json = method.getResponseBodyAsString(); System.out.println(json); JSONObject obj = JSONObject.parseObject(json); if (obj.get("status").toString().equals("1")) { JSONArray array = obj.getJSONArray("geocodes"); String str = array.getString(0); JSONObject locationjson = JSONObject.parseObject(str); str = locationjson.getString("location"); String[] location = str.split(","); double lng = Double.parseDouble(location[0]); double lat = Double.parseDouble(location[0]); map.put("lng", lng); map.put("lat", lat); System.out.println("经度:" + lng + "---纬度:" + lat); } else { System.out.println("未找到相匹配的经纬度!"); throw new Exception(); } } return map; } public static void main(String[] args) throws IOException { // memberMapInfo(); salesMapInfo(); } private static void salesMapInfo() { @SuppressWarnings("resource") ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); final SalesService service = (SalesServiceImpl) applicationContext.getBean(SalesServiceImpl.class); List<Sales> list = service.getListAddress(); int i = 0; for (Sales sale : list) { System.out.println(++i); Map<String, Double> map = new HashMap<>(); String address = sale.getAddress(); try { if (address.contains("(")) { address = address.substring(0, address.indexOf("(")); } if (address.contains("(")) { address = address.substring(0, address.indexOf("(")); } if (address.contains("黑龙江省")) { address = address.substring(address.lastIndexOf("黑龙江省")); } if (address.contains("上海")) { address = address.substring(address.lastIndexOf("上海")); if (address.contains("弄")) { address = address.substring(0, address.indexOf("弄")); } } if (address.lastIndexOf("黑龙江齐齐哈尔") > address.indexOf("黑龙江齐齐哈尔")) { address = address.substring(address.lastIndexOf("黑龙江齐齐哈尔")); } if (address.lastIndexOf("安徽省") > address.indexOf("安徽省")) { address = address.substring(address.lastIndexOf("安徽省")); } if (address.lastIndexOf("江苏省") > address.indexOf("江苏省")) { address = address.substring(address.lastIndexOf("江苏省")); } if (address.lastIndexOf("北京市") > address.indexOf("北京市")) { address = address.substring(address.lastIndexOf("北京市")); } if (address.lastIndexOf("北京") > address.indexOf("北京")) { address = address.substring(address.lastIndexOf("北京")); } if (address.lastIndexOf("甘肃省") > address.indexOf("甘肃省")) { address = address.substring(address.lastIndexOf("甘肃省")); } if (address.lastIndexOf("福建") > address.indexOf("福建")) { address = address.substring(address.lastIndexOf("福建")); } if (address.lastIndexOf("吉林省") > address.indexOf("吉林省")) { address = address.substring(address.lastIndexOf("吉林省")); } if (address.lastIndexOf("云南省") > address.indexOf("云南省")) { address = address.substring(address.lastIndexOf("云南省")); } if (address.lastIndexOf("内蒙古") > address.indexOf("内蒙古")) { address = address.substring(address.lastIndexOf("内蒙古")); } if (address.lastIndexOf("新疆维") > address.indexOf("新疆维")) { address = address.substring(address.lastIndexOf("新疆维")); } if (address.lastIndexOf("河北") > address.indexOf("河北")) { address = address.substring(address.lastIndexOf("河北")); } map = getLngAndLat(address); if (map.size() < 1) { System.out.println(address); map = getLngAndLatByAmap(address);//如果百度取不到就到高德取,高德限2000次每天,百度日上限30万,也可以用腾讯,日上限1万 } } catch (Exception e) { e.printStackTrace(); continue; } sale.setLongitude(map.get("lng").toString());// 经度 sale.setLatitude(map.get("lat").toString());// 纬度 service.updateByPrimaryKeySelective(sale); } }/** * * 从腾讯根据地址获取经纬度 * @param @param address * @param @return * @param @throws Exception * @return Map<String,Double> * @throws * @author longtj * @date 2017年9月21日 上午8:08:09 * @version 1.0.0 */ public static Map<String, Double> getLngAndLatByQQ(String address) throws Exception { address = address.trim(); Map<String, Double> map = new HashMap<String, Double>(); String url = "http://apis.map.qq.com/ws/geocoder/v1/?address=" + URLEncoder.encode(address, "utf-8") + "&key=你的key"; GetMethod method = new GetMethod(url); method.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); HttpClient client = new HttpClient(); client.getHttpConnectionManager().getParams().setConnectionTimeout(10000); // 设置连接超时 int status = client.executeMethod(method); if (status == 200) { String json = method.getResponseBodyAsString(); System.out.println(json); JSONObject obj = JSONObject.parseObject(json); if (obj.get("status").toString().equals("0")) { String msg=obj.getString("message"); System.out.println(msg); JSONObject result = obj.getJSONObject("result"); JSONObject location = result.getJSONObject("location"); double lng = location.getDouble("lng"); double lat = location.getDouble("lat"); map.put("lng", lng); map.put("lat", lat); System.out.println("经度:" + lng + "---纬度:" + lat); } else { System.out.println("未找到相匹配的经纬度!"); throw new Exception(); } } return map; }}
阅读全文
0 0
- java 百度高德实现地址转经纬度
- 根据经纬度取地址 高德、百度
- 服务端java实现根据地址从百度API获取经纬度
- 百度地图经纬度和地址互转(Java代码)
- ios系统经纬度转百度经纬度及经纬度转地址
- (java功能篇)百度获取地址经纬度
- java根据地址获取百度API经纬度
- 百度地图 java 根据地址获取经纬度
- java 根据经纬度获取百度地图地址
- java工具类(一)之服务端java实现根据地址从百度API获取经纬度
- python 百度地图 地址转经纬度
- python 百度地图 经纬度转地址
- 地址转经纬度(百度)function
- 百度、高德、Googe经纬度转换
- 百度获取地址经纬度
- 高德地图地址经纬度转化 URL
- 高德地图地址和经纬度转换
- java后台百度地图经纬度和地址之间的相互转换(通过经纬度获取地址、通过地址获取经纬度)
- ActiveMQ系列—ActiveMQ性能优化(下1)(消息存储方案 KahaDB)
- Hibernate框架中数据库多对多的演示
- SpringMVC 与Struts2的区别
- ZOJ
- java学习笔记--面向对象
- java 百度高德实现地址转经纬度
- AndroidStudio配置ndk环境
- Android 编译7.0sdk 应用时报错 (Unsupported class version number [52.0] (maximum 51.0, Java 1.7))处理
- 安防监控直播中无插件web直播方案中实现快照抓取的功能
- leetcode03. Find the Weak Connected Component in the Directed Graph
- msfvenom 生成shellcode
- Java练习
- mysql优化
- [备战软考]操作系统