通过百度提供的api和经纬度信息,查询省份信息,用java语言实现,需要百度开发者密钥,这个可以自行申请
代码如下
public String locationInfo(String x,String y) throws Exception{//x:纬度,y:经度
JSONObject json=null;
BufferedReader in = null;
String url = "http://api.map.baidu.com/geocoder/v2/?output=json&ak=你的密钥&location="+x+","+y;
String result="";
URL realUrl = new URL(url);
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
in = new BufferedReader(
new InputStreamReader(conn.getInputStream(),"UTF-8"));//gbk什么的也行吧这里我就没做实验了
String line;
while ((line = in.readLine()) != null) {
result += line;
}
json=JSONObject.fromObject(result);
String jsonSts=json.getString("status");
if("0".equals(jsonSts)){
String province=json.getJSONObject("result").getJSONObject("addressComponent").getString("province");
result=province;
System.err.println(province);
}
return result;
}
返回的json串的样式是这个样子的
/*{"status":0,
* "result":{"location":{"lng":******,"lat":********},
* "formatted_address":"******",
* "business":"***,***,***",
* "addressComponent":{"city":"*****",
* "country":"***",
* "direction":"",
* "distance":"",
* "district":"*****",
* "province":"******",
* "street":"******",
* "street_number":"",
* "country_code":0},
* "poiRegions":[],
* "sematic_description":"*************",
* "cityCode":131}}
*/