经纬度获取地址/地址解析经纬度

package com.vtradex.tms.server.utils;


import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;


import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.json.JSONException;
import org.json.JSONObject;


/**
 */
public class BaiduGeocoder {

public static String  URL_ADDRESS = "http://api.map.baidu.com/geocoder?address={0}&output=json&key=76fc7754b6eeea437b33eed76150946c";
public static String  URL_LOCATION = "http://api.map.baidu.com/geocoder?output=json&location={0}&key=37492c0ee6f924cb5e934fa08c6b1676";


public static void main(String[] args) throws Exception {
//经纬度获取地址
double lat = 31.218782;
double lng = 121.403227;
JSONObject result = parseLocation(lat, lng);
JSONObject location = result.getJSONObject("location");
System.out.println("纬度:" + location.getDouble("lat"));
System.out.println("经度:" + location.getDouble("lng"));


String formatted_address = result.getString("formatted_address");
System.out.println("地址:" + formatted_address);
JSONObject addressComponent = result.getJSONObject("addressComponent");
System.out.println(addressComponent);


//地址解析经纬度
// String address = "上海市天山路";
// JSONObject result = parseAddress(address);
// JSONObject location = result.getJSONObject("location");
// System.out.println("纬度:" + location.getDouble("lat"));
// System.out.println("经度:" + location.getDouble("lng"));
}

public static String getURL(boolean f,String value ){
String token = "{0}" ;
if(f){
return URL_ADDRESS.replace(token, value);
}
return URL_LOCATION.replace(token, value);
}


//经纬度反解析地址
public static JSONObject parseLocation(Double lat, Double lng) throws UnsupportedEncodingException, JSONException{
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(getURL(false,lat + "," + lng));
try {
int statusCode = httpClient.executeMethod(postMethod);
if (statusCode == 200) {
String str = postMethod.getResponseBodyAsString();
JSONObject json = new JSONObject(str);
return json.getJSONObject("result");
}
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}


// 地址解析经,纬度
public static JSONObject parseAddress(String address) throws UnsupportedEncodingException, JSONException{
address = URLEncoder.encode(address, "UTF-8");
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(getURL(true,address));
try {
int statusCode = httpClient.executeMethod(postMethod);
if (statusCode == 200) {
String str = postMethod.getResponseBodyAsString();
JSONObject json = new JSONObject(str);
return json.getJSONObject("result");
}
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值