我试图使用NETWORK_PROVIDER获得国家名称,我写了一个方法
private int getCurrentLocation(Location location) throws IOException {
Geocoder geocoder = new Geocoder(this, getResources().getConfiguration().locale);
List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 5);
String CurrentCountryFullName = addresses.get(0).getCountryName();
if (CurrentCountryFullName != null) {
for (StandartCurrency standartCurrency: RecourcesForStandartCurrencies.getStandartCurrencyList()){
if (RecourcesForStandartCurrencies.getCountryFullName(standartCurrency.getPosition()).toLowerCase().contains(CurrentCountryFullName.toLowerCase()))
return standartCurrency.getPosition();
}
}
return AddedCurrency.getAddedCurrenciesCount();
}
我从
调用这个方法 LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
try {
leftFieldIndex = getCurrentLocation(locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER));
} catch (IOException e) {
e.printStackTrace();
My AdnroidManifest.xml包含
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
我正在测试Nexus 4,它有wifi连接,但仍然不能工作。
Geocoder
不稳定,有时会停止工作。您没有发布错误日志或异常,但根据经验,当Geocoder
中断时,它只是返回一个空列表。不幸的是,没有办法检查Geocoder
是否提前工作(只有一种方法来检查Geocoder
是否可用,即使它不起作用也会返回true)
周围的工作是连接到谷歌地图web api时,你的Geocoder
返回一个空列表。查看这篇文章中的代码示例。你要做的只是检查从Geocoder
返回的列表是否为空或空,如果它是使用AsyncTask
使一个web api调用谷歌地图api。最终结果是相同的,因此您不应该注意到功能上的差异。
private int getCurrentLocation(Location location) throws IOException {
Geocoder geocoder = new Geocoder(this, getResources().getConfiguration().locale);
List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 5);
if(addresses != null && addresses.size() > 0 {
//use addresses
} else {
//call web API async task
}
}
根据您对地址的处理,将Geocoder
实现分解为一个单独的类可能是有意义的。这样,您可以使用Interface
将您的Geocoder
类连接到您的活动或片段,并在您的AsyncTask
web api调用和Geocoder
之间进行无缝过渡,如果稍后停止工作(您显然不能在同一方法中返回AsyncTask
的结果,因为它在方法完成时未交付)
我有同样的问题,我切换到v3 web API。现在我正在使用java客户端,一切正常。
所有的信息都在谷歌地图页面