Skip to content

Commit 10ffe18

Browse files
committed
[NewFeature] Add locationg util.
1 parent 1fae3a4 commit 10ffe18

File tree

1 file changed

+202
-0
lines changed

1 file changed

+202
-0
lines changed
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
/**
2+
* Copyright 2014 Zhenguo Jin (jingle1267@163.com)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.worthed.utils;
17+
18+
import java.io.InputStream;
19+
20+
import org.apache.http.HttpEntity;
21+
import org.apache.http.HttpResponse;
22+
import org.apache.http.client.HttpClient;
23+
import org.apache.http.client.methods.HttpGet;
24+
import org.apache.http.impl.client.DefaultHttpClient;
25+
import org.apache.http.params.BasicHttpParams;
26+
import org.apache.http.params.HttpConnectionParams;
27+
import org.apache.http.params.HttpParams;
28+
import org.json.JSONArray;
29+
import org.json.JSONException;
30+
import org.json.JSONObject;
31+
32+
import android.text.TextUtils;
33+
import android.util.Log;
34+
35+
/**
36+
* 根据经纬度查询地址信息和根据地址信息查询经纬度
37+
*
38+
* @author jingle1267@163.com
39+
*
40+
*/
41+
public class LocationUtils {
42+
43+
private final static boolean DEBUG = true;
44+
private final static String TAG = "LocationUtils";
45+
46+
/**
47+
* 根据地址获取对应的经纬度
48+
* @param address
49+
* @return
50+
*/
51+
public static double[] getLocationInfo(String address) {
52+
if (TextUtils.isEmpty(address)) {
53+
return null;
54+
}
55+
if (DEBUG) {
56+
LogUtils.d(TAG, "address : " + address);
57+
}
58+
// 定义一个HttpClient,用于向指定地址发送请求
59+
HttpClient client = new DefaultHttpClient();
60+
// 向指定地址发送GET请求
61+
HttpGet httpGet = new HttpGet("http://maps.google."
62+
+ "com/maps/api/geocode/json?address=" + address
63+
+ "ka&sensor=false");
64+
StringBuilder sb = new StringBuilder();
65+
try {
66+
// 获取服务器的响应
67+
HttpResponse response = client.execute(httpGet);
68+
HttpEntity entity = response.getEntity();
69+
// 获取服务器响应的输入流
70+
InputStream stream = entity.getContent();
71+
int b;
72+
// 循环读取服务器响应
73+
while ((b = stream.read()) != -1) {
74+
sb.append((char) b);
75+
}
76+
// 将服务器返回的字符串转换为JSONObject对象
77+
JSONObject jsonObject = new JSONObject(sb.toString());
78+
// 从JSONObject对象中取出代表位置的location属性
79+
JSONObject location = jsonObject.getJSONArray("results")
80+
.getJSONObject(0).getJSONObject("geometry")
81+
.getJSONObject("location");
82+
// 获取经度信息
83+
double longitude = location.getDouble("lng");
84+
// 获取纬度信息
85+
double latitude = location.getDouble("lat");
86+
if (DEBUG) {
87+
LogUtils.d(TAG, "location : (" + longitude + "," + latitude + ")");
88+
}
89+
// 将经度、纬度信息组成double[]数组
90+
return new double[] { longitude, latitude };
91+
} catch (Exception e) {
92+
e.printStackTrace();
93+
}
94+
return null;
95+
}
96+
97+
/**
98+
* 根据经纬度获取对应的地址
99+
* @param longitude
100+
* @param latitude
101+
* @param lang
102+
* @return
103+
* @throws Exception
104+
*/
105+
public static String getAddress(double longitude, double latitude,
106+
String lang) throws Exception {
107+
if (DEBUG) {
108+
LogUtils.d(TAG, "location : (" + longitude + "," + latitude + ")");
109+
}
110+
if (lang == null) {
111+
lang = "en";
112+
}
113+
// 设定请求的超时时间
114+
HttpParams params = new BasicHttpParams();
115+
HttpConnectionParams.setConnectionTimeout(params, 10 * 1000);
116+
HttpConnectionParams.setSoTimeout(params, 10 * 1000);
117+
// 定义一个HttpClient,用于向指定地址发送请求
118+
HttpClient client = new DefaultHttpClient(params);
119+
// 向指定地址发送GET请求
120+
HttpGet httpGet = new HttpGet("https://maps.googleapis.com/maps/api/"
121+
+ "geocode/json?latlng=" + latitude + "," + longitude
122+
+ "&sensor=false&language=" + lang);
123+
if (DEBUG) {
124+
LogUtils.d(TAG,
125+
"URL : " + httpGet.getURI());
126+
}
127+
StringBuilder sb = new StringBuilder();
128+
// 执行请求
129+
HttpResponse response = client.execute(httpGet);
130+
HttpEntity entity = response.getEntity();
131+
// 获取服务器响应的字符串
132+
InputStream stream = entity.getContent();
133+
int b;
134+
while ((b = stream.read()) != -1) {
135+
sb.append((char) b);
136+
}
137+
// 把服务器相应的字符串转换为JSONObject
138+
JSONObject jsonObj = new JSONObject(sb.toString());
139+
Log.d("ConvertUtil", "getAddress:" + sb.toString());
140+
// 解析出响应结果中的地址数据
141+
JSONObject addressObject = jsonObj.getJSONArray("results")
142+
.getJSONObject(0);
143+
String address = decodeLocationName(addressObject);
144+
if (DEBUG) {
145+
LogUtils.d(TAG, "address : " + address);
146+
}
147+
return address;
148+
}
149+
150+
/**
151+
* 根据Google API 解析出国家和城市名称
152+
* https://developers.google.com/maps/documentation/geocoding
153+
*
154+
* @param jsonObject
155+
* @return
156+
*/
157+
public static String decodeLocationName(JSONObject jsonObject) {
158+
JSONArray jsonArray;
159+
String country = "", city = "";
160+
String TYPE_COUNTRY = "country";
161+
String TYPE_LOCALITY = "locality";
162+
String TYPE_POLITICAL = "political";
163+
boolean hasCountry = false;
164+
try {
165+
jsonArray = jsonObject.getJSONArray("address_components");
166+
for (int i = 0; i < jsonArray.length(); i++) {
167+
JSONObject jo = jsonArray.getJSONObject(i);
168+
JSONArray types = jo.getJSONArray("types");
169+
boolean hasLocality = false, hasPolicical = false;
170+
171+
for (int j = 0; j < types.length(); j++) {
172+
String type = types.getString(j);
173+
if (type.equals(TYPE_COUNTRY) && !hasCountry) {
174+
country = jo.getString("long_name");
175+
} else {
176+
if (type.equals(TYPE_POLITICAL)) {
177+
hasPolicical = true;
178+
}
179+
if (type.equals(TYPE_LOCALITY)) {
180+
hasLocality = true;
181+
}
182+
if (hasPolicical && hasLocality) {
183+
city = jo.getString("long_name");
184+
}
185+
}
186+
}
187+
}
188+
return city + "," + country;
189+
} catch (JSONException e) {
190+
e.printStackTrace();
191+
}
192+
if (jsonObject.has("formatted_address")) {
193+
try {
194+
return jsonObject.getString("formatted_address");
195+
} catch (JSONException e) {
196+
e.printStackTrace();
197+
}
198+
}
199+
return null;
200+
}
201+
202+
}

0 commit comments

Comments
 (0)