通过经纬度获取地理位置信息

最近做一个车载设备app,设备获取北斗gps数据上传的到后台,app通过后台提供的经纬度反取地理编码位置,支持Google和百度。

获取地理位置url

//Google
public String getGoogleUrl(boolean isCn, String longitude,String latitude) {
    if (!isCn) {
        return "http://maps.google.com/maps/api/geocode/json?latlng="+latitude+","+longitude+"&sensor=false&language="
                + Locale.getDefault().getLanguage() + "-" + Locale.getDefault().getCountry();
    } else {
        return "http://ditu.google.cn/maps/api/geocode/json?latlng="+latitude+","+longitude+"&sensor=false&language="
                + Locale.getDefault().getLanguage() + "-" + Locale.getDefault().getCountry();
    }
}

//百度,到百度地图获取ak密钥
public String getBaiduUrl(String longitude,String latitude) {
    return "http://api.map.baidu.com/geocoder/v2/?" +
            "ak=ahH7ICSO020gfifmGVog5OTimwq" +
            "&mcode=DC:B1:26:FF:66:49:62:85:98:16:AE:2A:E8:69:A9:EE:AC:14:3B;com.icar.taxi" +
            "&output=json" +
            "&pois=0&location="+latitude+","+longitude;
}

获取详细街道地址

/**
* @return 详细街道地址
*/
public String getAddress(String url){
    //定义一个HttpClient,用于向指定地址发送请求
    HttpClient client = new DefaultHttpClient();

    //向指定地址发送Get请求
    HttpGet hhtpGet = new HttpGet(url);
    StringBuilder sb = new StringBuilder();
    try {
        //获取服务器响应
        HttpResponse response = client.execute(hhtpGet);
        HttpEntity entity = response.getEntity();

        if(entity !=null){   
            BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(),"UTF-8"),8192);   
            String line =null;   
            while ((line= reader.readLine())!=null){   
                sb.append(line +"\n");   
            }   
            reader.close();   
        }   

        //将服务器返回的字符串转换为JSONObject  对象
        JSONObject jsonObject = new JSONObject(sb.toString());
        //从JSONObject 中取出location 属性
        if (AppConfig.mapType()) {
            return jsonObject.optJSONObject("result").optString("formatted_address");
        } else {
            return jsonObject.optJSONArray("results").optJSONObject(0).optString("formatted_address");
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) { 
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值