Android 应用程序中的反向地理编码
我正在尝试使用 Geocoder 类将当前的地理编码纬度/经度数据反转为管理区域和子管理区域。 我使用 NETWORK_PROVIDER 作为位置提供程序。这是我使用的代码并且它有效。问题是有时它不给我任何位置,有时却给我任何位置。
我正在使用的 Android 清单权限。
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"/>
这是代码的其余部分,它确实时不时地给我正确的位置,但其他时候却什么也没有。
double latPoint = 0;
double lngPoint = 0;
List<Address> myList = null;
getApplicationContext();
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location recentLoc = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (recentLoc != null){
latPoint = recentLoc.getLatitude();
lngPoint = recentLoc.getLongitude();
Geocoder myLocation = new Geocoder(getApplicationContext(), Locale.getDefault());
if (myLocation != null){
try {
myList = myLocation.getFromLocation(latPoint, lngPoint, 1);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (myList.size()> 0){
String AdminArea = myList.get(0).getAdminArea();
String SubAdminArea = myList.get(0).getSubAdminArea();
String Zipcode = myList.get(0).getPostalCode();
String urlocation = SubAdminArea + ", " + AdminArea + " " + Zipcode;
Context context1 = getApplicationContext();
int duration1 = Toast.LENGTH_LONG;
CharSequence text1 = urlocation;
Toast toast1 = Toast.makeText(context1, text1, duration1);
toast1.show();
}
}
网络提供商对您请求位置数据的频率是否有限制?
我如何确保每次运行都能为我提供有效的位置数据。
预先感谢您对此的任何见解。
PS:我已经在运行 Froyo 2.2 (Verizon Wireless) 的真实设备上运行了此代码。目标 API 级别 8。
这是我从调试中得到的结果。
纬度经度始终会被获取,但 getFromLocation 通常会为传递给它的纬度经度数据返回 null。那么解决方法应该是什么?可以将纬度/经度信息传递给 Google Maps API 进行反向地理编码。将很高兴收到任何意见。
I am trying to Reverse GeoCode current Lat/Long data to just Admin Area and Sub Admin Area using the Geocoder class.
I am using the NETWORK_PROVIDER as the Location provider. Here is the code that I use and it works. The problem is sometimes it does not give me any location sometimes it does.
Android Manifest permissions I am using.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"/>
Here is the rest of the code that does give me correct location from time to time but nothing at other times.
double latPoint = 0;
double lngPoint = 0;
List<Address> myList = null;
getApplicationContext();
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location recentLoc = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (recentLoc != null){
latPoint = recentLoc.getLatitude();
lngPoint = recentLoc.getLongitude();
Geocoder myLocation = new Geocoder(getApplicationContext(), Locale.getDefault());
if (myLocation != null){
try {
myList = myLocation.getFromLocation(latPoint, lngPoint, 1);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (myList.size()> 0){
String AdminArea = myList.get(0).getAdminArea();
String SubAdminArea = myList.get(0).getSubAdminArea();
String Zipcode = myList.get(0).getPostalCode();
String urlocation = SubAdminArea + ", " + AdminArea + " " + Zipcode;
Context context1 = getApplicationContext();
int duration1 = Toast.LENGTH_LONG;
CharSequence text1 = urlocation;
Toast toast1 = Toast.makeText(context1, text1, duration1);
toast1.show();
}
}
Are there restrictions with the network providers how frequently you can request location data?
How can I make sure that everytime its run this can give me valid location data.
Thanks in advance for any insight into this.
PS: I have run this code on my real device running Froyo 2.2 (Verizon Wireless). Target API Level 8.
This is what I got from my debugging.
Latitude Longitude is always fetched but the getFromLocation often returns null for the latitude longitude data passed to it. So what should be the workaround? May be pass the Lat/Long info to Google Maps API for reverse geocoding. Will be glad to receive any input.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,这就是我解决它的方法。
由于 NETWORK_PROVIDER 每次都会提供纬度经度信息,因此我首先获取用户当前位置的纬度经度信息。
latPoint=recentLoc.getLatitude();
lngPoint = recentLoc.getLongitude();
我获取了 Google Maps API 密钥,然后用它对来自网络提供商的纬度、经度数据进行反向地理编码。这每次都会返回所需的地址和邮政编码。
Well this is how I solved it.
Since the NETWORK_PROVIDER provides the latitude longitude information without fail everytime, I fetch that for the user's current location first.
latPoint = recentLoc.getLatitude();
lngPoint = recentLoc.getLongitude();
I obtained the Google Maps API Key and then used it to Reverse Geocode the latitude, longitude data from the Network Provider. This returned the required address and the zipcode everytime without fail.