Android 如何检查横向和纵向位置
如果我有一个特定位置的lat和long,我如何检查该位置是否处于特定状态 例如: 如果lat和long指的是圣克拉拉,我如何检查它是否在加利福尼亚 谢谢使用类从您的lat/lon获取地址。国家应该是地址的一部分 注意:这不适用于没有地址的地方(国家、森林等) 更新: 有一个很好的服务使用谷歌地图反向地理编码数据库(与Android内部使用的相同):Android 如何检查横向和纵向位置,android,Android,如果我有一个特定位置的lat和long,我如何检查该位置是否处于特定状态 例如: 如果lat和long指的是圣克拉拉,我如何检查它是否在加利福尼亚 谢谢使用类从您的lat/lon获取地址。国家应该是地址的一部分 注意:这不适用于没有地址的地方(国家、森林等) 更新: 有一个很好的服务使用谷歌地图反向地理编码数据库(与Android内部使用的相同): 随便翻找实际上可以提供任何一点(甚至格陵兰岛中部)的国家数据,所以你应该没问题。我的答案袋里有三件事: android中的反向地理编码: Fours
随便翻找实际上可以提供任何一点(甚至格陵兰岛中部)的国家数据,所以你应该没问题。我的答案袋里有三件事:
对于您来说,第一个是最快速、最简单的,第三个是最可靠、最定制的。使用地理编码器和地址类来实现解决方案。此代码可能有助于您。。祝你一切顺利
Geocoder gc = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = gc.getFromLocation(lat, lng, 1);
StringBuilder sb = new StringBuilder();
if (addresses.size() > 0) {
Address address = addresses.get(0);
for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
String addr=address.getAddressLine(i);
//use this addr string and compare . u will get the solution
}
} catch (IOException e) {}
Geocoder gc=new Geocoder(这个,Locale.getDefault());
试试{
列表地址=gc.getFromLocation(lat,lng,1);
StringBuilder sb=新的StringBuilder();
如果(addresses.size()>0){
地址=地址。获取(0);
对于(int i=0;i
尝试使用以下代码
Geocoder gc = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = gc.getFromLocation(lat, lng, 1);
if (addresses.size() > 0) {
Address address = addresses.get(0);
String state = address.getLocality();
}
} catch (IOException e) {}
Geocoder gc=new Geocoder(这个,Locale.getDefault());
试试{
列表地址=gc.getFromLocation(lat,lng,1);
如果(addresses.size()>0){
地址=地址。获取(0);
字符串状态=address.getLocation();
}
}捕获(IOE){}
如何实现你给我的链接……你有没有教程的链接或示例代码?来吧Geocode.get..”只是一个命令。你应该想清楚。
public static String getUserAddress(String lat, String lon) {
String userlocation = null;
String userCountry = null;
String userCountryCode = null;
String readUserFeed = readUserLocationFeed(lat.trim() + ","+ lon.trim());
try {
JSONObject Strjson = new JSONObject(readUserFeed);
JSONArray jsonArray = new JSONArray(Strjson.getString("results"));
JSONArray array = new JSONArray(jsonArray.getJSONObject(0).getString("address_components"));
userCountry = array.getJSONObject(2).getString("long_name").toString();
userCountryCode = array.getJSONObject(2).getString("short_name").toString();
userlocation = jsonArray.getJSONObject(1).getString("formatted_address").toString();
System.out.println("User Address : "+userlocation +"\nUser Country : "+userCountry+"\nUser CountryCode : "+ userCountryCode);
} catch (Exception e) {
e.printStackTrace();
}
Log.i("User Location ", userlocation);
return userlocation;
}
public static String readUserLocationFeed(String address) {
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?latlng="+ address + "&sensor=false");
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
Log.e(ReverseGeocode.class.toString(), "Failed to download file");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return builder.toString();
}