获取经纬度

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/shuimofengyang/article/details/85323453

package com.bigdata.std;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.log4j.Logger;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;

public class LngAndLatUtil
{
private static Logger logger = Logger.getLogger(LngAndLatUtil.class);
private static String ak = “xxxxx”;

public static Map<String, Double> getLngAndLat(String address)
{
    Map<String, Double> map = new HashMap();

    String url = "http://api.map.baidu.com/geocoder/v2/?address=" + address + "&output=json&ak=" + ak;
    String json = loadJSON(url);
    System.out.println("打印出来 ---"+json);
    logger.warn("get json data " + json);

    JsonParser parse = new JsonParser();
    JsonObject obj = (JsonObject) parse.parse(json);
    if(obj.get("status").toString().equals("0")){
        obj.getAsJsonObject("result").getAsJsonObject("location").get("lng");
        double lng = obj.getAsJsonObject("result").getAsJsonObject("location").get("lng").getAsDouble();
        double lat = obj.getAsJsonObject("result").getAsJsonObject("location").get("lat").getAsDouble();
       map.put("lng",lng);
       map.put("lat",lat);
    }else{
        logger.warn("get json data error ");
    }

    return map;
}

public static String loadJSON(String url)
{
    StringBuilder json = new StringBuilder();
    logger.warn("url" + url);
    try
    {
        URL oracle = new URL(url);
        URLConnection yc = oracle.openConnection();
        yc.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
        yc.setDoOutput(true);
        yc.setConnectTimeout(5000);
        BufferedReader in = new BufferedReader(new InputStreamReader(
                yc.getInputStream(), "UTF-8"));
        String inputLine = null;
        while ((inputLine = in.readLine()) != null) {
            json.append(inputLine);
        }
        in.close();
    }
    catch (MalformedURLException e)
    {
        logger.warn("MalformedURL error" + e);
    }
    catch (IOException e)
    {
        logger.warn("IOException error" + e);
    }
    return json.toString();
}

public static void main(String[] args)
{
    Map<String, Double> map = getLngAndLat("万科");
    System.out.println(map.get("lng") + "----" + map.get("lat"));
}

}
结果:
18/12/28 15:43:34 WARN std.LngAndLatUtil: urlhttp://api.map.baidu.com/geocoder/v2/?address=万科&output=json&ak=xxxxxxxxx
打印出来 —{“status”:0,“result”:{“location”:{“lng”:120.75169741624853,“lat”:31.337948709826035},“precise”:1,“confidence”:90,“comprehension”:100,“level”:“UNKNOWN”}}
18/12/28 15:43:34 WARN std.LngAndLatUtil: get json data {“status”:0,“result”:{“location”:{“lng”:120.75169741624853,“lat”:31.337948709826035},“precise”:1,“confidence”:90,“comprehension”:100,“level”:“UNKNOWN”}}
120.75169741624853----31.337948709826033

猜你喜欢

转载自blog.csdn.net/shuimofengyang/article/details/85323453