上次在學習意外系列中的第五季提到了有關GPS定位中發現的一個Geocoder處理由GPS定位的信息查詢地址信息的內容結果返回地址的信息列表爲空,導致了開發的應用程序異常終止,實在無語。當然我後面給的那個類還是可以解決問題的。
而就在今天一次偶然的機會我發現了這個問題的解決方案——修改運行配置,將模擬器由android的版本改爲google api的版本,問題就解決了。這個並不是google出現的bug,只是在這個地圖信息獲取的機制也許只有在google的 api 模擬器中才可以操作成功。
所以一個建議就是在每次設計到地圖的信息時,還是採用google的avd比較好,這樣子避免出現那些比較鬧心的事情,以免讓人揪心的“爲伊消得人憔悴”
最後還是給出這個轉換的代碼,幫大家方便使用:
// 獲取LocationManager的一個實例
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
10000, 0, locationListener);
Location location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
locationManager.setTestProviderEnabled("gps", true);
while (location != null) {
latitude = location.getLatitude();// 經度
longitude = location.getLongitude();// 緯度
refreshMapView();
Geocoder geo = new Geocoder(this, Locale.getDefault());
List<Address> adList = null;
try {
// 將gps信息類轉化成address信息類
adList = geo.getFromLocation(latitude, longitude, 1);
} catch (IOException e) {
e.printStackTrace();
}
if (adList.size() > 0) {
// showDialog(String.valueOf(adList.size()));
Address ad = adList.get(0);
// nowlocal= ad.getAdminArea();//取得省級名
// nowlocal=ad.getLocality();//取得區域名
if (ad.getLocality() == null) {
if (ad.getAdminArea() == null) {
if (ad.getCountryName() == null) {
nowlocal = "公海";
} else
nowlocal = ad.getCountryName();
} else {
nowlocal = ad.getAdminArea();
}
} else
nowlocal = ad.getLocality();
showDialog(ad.toString());
} else {
nowlocal = "查無此地~";
}
break;
}
其中的那個local 和nowlocal是String類型的 我是用它們來取得當前位置的一個級別的地址信息。
其中的那個local 和nowlocal是String類型的 我是用它們來取得當前位置的一個級別的地址信息。