代码如下:
第一步,获取经纬度,第二步,把经纬度转换为地址。
第一步,获取经纬度,第二步,把经纬度转换为地址。
01 |
/** |
02 |
* |
03 |
* 由街道信息转换为经纬度 |
04 |
* @param address 街道信息 |
05 |
* @return 包含经纬度的一个double 数组,{longtitude,latitude} |
06 |
*/ |
07 |
public static double [] getLocationInfoByGoogle(String address){
|
08 |
//定义一个HttpClient,用于向指定地址发送请求 |
09 |
HttpClient client = new DefaultHttpClient(); |
10 |
11 |
//向指定地址发送Get请求 |
12 |
HttpGet hhtpGet = new HttpGet( "http://maps.google.com/maps/api/geocode/json?address=" +address+ "ka&sensor=false" ); |
13 |
14 |
StringBuilder sb = new StringBuilder(); |
15 |
16 |
17 |
try {
|
18 |
//获取服务器响应 |
19 |
HttpResponse response = client.execute(hhtpGet); |
20 |
21 |
HttpEntity entity = response.getEntity(); |
22 |
23 |
//获取服务器响应的输入流 |
24 |
InputStream stream = entity.getContent(); |
25 |
26 |
int b; |
27 |
//循环读取服务器响应 |
28 |
while ((b = stream.read()) != - 1 ){
|
29 |
sb.append(( char )b); |
30 |
} |
31 |
32 |
//将服务器返回的字符串转换为JSONObject 对象 |
33 |
JSONObject jsonObject = new JSONObject(sb.toString()); |
34 |
35 |
//从JSONObject 中取出location 属性 |
36 |
JSONObject location = jsonObject.getJSONObject( "results" ).getJSONObject( "0" ).getJSONObject( "geometry" ).getJSONObject( "location" ); |
37 |
|
38 |
//获取经度信息 |
39 |
double longtitude = location.getDouble( "lng" ); |
40 |
double latitude = location.getDouble( "lat" ); |
41 |
42 |
return new double []{longtitude,latitude}; |
43 |
44 |
} catch (ClientProtocolException e) {
|
45 |
e.printStackTrace(); |
46 |
} catch (IOException e) {
|
47 |
e.printStackTrace(); |
48 |
} catch (JSONException e) { |
49 |
e.printStackTrace(); |
50 |
} |
51 |
52 |
53 |
return null ; |
54 |
} |