Java 通过Android上的LocationManager轻松获取城市信息?
我试图找出用户使用其位置的城市。使用LocationManager和Geocoder,我从经度和纬度获得了一些不错的数据,但我一点也得不到。亚德米纳雷亚又称城市。它总是为我返回null,即使收到了包括邮政编码在内的所有内容。我有什么遗漏吗 基本上,这是我用来获取数据的方法Java 通过Android上的LocationManager轻松获取城市信息?,java,android,geolocation,Java,Android,Geolocation,我试图找出用户使用其位置的城市。使用LocationManager和Geocoder,我从经度和纬度获得了一些不错的数据,但我一点也得不到。亚德米纳雷亚又称城市。它总是为我返回null,即使收到了包括邮政编码在内的所有内容。我有什么遗漏吗 基本上,这是我用来获取数据的方法 public String getLocation(Locale locale, Context context, boolean city, boolean postal, boolean state_prov) throw
public String getLocation(Locale locale, Context context, boolean city, boolean postal, boolean state_prov) throws IOException{
LocationManager locMan = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
LocationListener locList = new MyLocList();
Geocoder gC = new Geocoder(context,locale);
Location gpsLocation = locMan.getLastKnownLocation(locMan.GPS_PROVIDER);
locMan.requestLocationUpdates(locMan.NETWORK_PROVIDER, 500, 200, locList);
Location networkLocation = locMan.getLastKnownLocation(locMan.NETWORK_PROVIDER);
if (city)
return (gC.getFromLocation(networkLocation.getLatitude(), networkLocation.getLongitude(), 1).get(0).getSubAdminArea());
else if (postal)
return (gC.getFromLocation(networkLocation.getLatitude(), networkLocation.getLongitude(), 1).get(0).getPostalCode());
else if (state_prov)
return (gC.getFromLocation(networkLocation.getLatitude(), networkLocation.getLongitude(), 1).get(0).getAdminArea());
else
return "";
}
对该方法的调用通过以下方式完成:
String city = getLocation(Locale.getDefault(),getBaseContext(),true,false,false);
通过一些研究,我发现唯一的其他选择是向
http://maps.googleapis.com/maps/api/geocode/json?address=POSTALCODE&sensor=true
它给了我一个基于邮政编码的那个位置的JSON,然后我可以解析它,但是要找到这个城市似乎需要很多工作
我会错过什么吗?还是我做错了什么?我是android定位服务的新手。试试这个:
Geocoder gc = new Geocoder(context, Locale.getDefault());
List<Address> addresses = gc.getFromLocation(latitude, longitude, maxResults);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < addresses.getMaxAddressLineIndex(); i++)
Log.d("=Adress=",addresses.getAddressLine(i));
}
Geocoder gc=new Geocoder(context,Locale.getDefault());
列表地址=gc.getFromLocation(纬度、经度、maxResults);
StringBuilder sb=新的StringBuilder();
对于(int i=0;i
您将获得邮政编码、城市、国家等。请尝试以下操作:
Geocoder gc = new Geocoder(context, Locale.getDefault());
List<Address> addresses = gc.getFromLocation(latitude, longitude, maxResults);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < addresses.getMaxAddressLineIndex(); i++)
Log.d("=Adress=",addresses.getAddressLine(i));
}
Geocoder gc=new Geocoder(context,Locale.getDefault());
列表地址=gc.getFromLocation(纬度、经度、maxResults);
StringBuilder sb=新的StringBuilder();
对于(int i=0;i
您将获得邮政编码、城市、国家等。我使用过,它工作得非常好。尽管我最初在这个问题上遇到了一些问题,它还是能够找到位置。我使用了它,它工作得非常好。尽管我最初在这个问题上遇到了一些问题,但它还是能够找到位置。似乎把我的位置改到另一个地区就得到了一个城市的读数。这很奇怪。但这仍然会为家中的subAdminArea返回null。不确定是什么原因造成的。好像把我的位置换到另一个地方得到了一个城市的读数。这很奇怪。但这仍然会为家中的subAdminArea返回null。不知道是什么原因造成的。