我試圖檢索我的當前位置按鈕單擊使用GPS或網絡的編輯框..我嘗試了所有可能的方法,我發現在教程和舊帖子....但我的位置總是空的.. 我沒有得到我錯的地方。? 我對真實的設備有網絡,但沒有互聯網/ GPRS測試它...當前位置始終爲空
這就是我很using..i嘗試同樣與GPS_PROVIDER以及..please幫助我的代碼..
protected void showCurrentLocation()
{
geocoder = new Geocoder(this);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
new MyLocationListener()
);
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
System.out.println("loc.."+location);
if (location != null)
{
String message = String.format("Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude());
Toast.makeText(ShowActivity.this, message,
Toast.LENGTH_LONG).show();
//acTextView.setText(message);
try {
List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 10); //<10>
for (Address address : addresses) {
System.out.println("my location .."+address.getAddressLine(0));
acTextView.setText(address.getAddressLine(0));
}
} catch (IOException e) {
Log.e("LocateMe", "Could not get Geocoder data", e);
}
}
else
{
AlertDialog.Builder alertbox1 = new AlertDialog.Builder(this);
alertbox1.setMessage("No GPS or network ..Signal please fill the location manually!");
alertbox1.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1)
{}});
alertbox1.show();
}
}
我只是編輯的問題,而不是問新的一個..因爲我的問題仍然是相同的......我沒有改變代碼中的任何東西,現在工作完全正常.... 。但問題是,當我使用GPS_PROVIDER時,它返回準確的地址以及經度和緯度的值..但是當我使用NETWORK_PROVIDER時,它只返回經度和緯度的值,沒有地址...我想檢索完整的地址使用NET因爲GPS不能在室內工作,所以WORK_PROVIDER ...我該怎麼做?
謝謝...... !!
你在模擬器上運行代碼嗎?你總是會在lastKnownLocaton中得到空值...所以試試從開始的默認值或使用Shared Preference。 – Hanry
no ..i在問題中提到「我在具有網絡但沒有互聯網/ gprs的真實設備上測試它......」 –
耆那教徒的回答之後仍然存在問題嗎? – Hanry