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
原创粉丝点击