我想顯示我的當前位置的經度和緯度.. 爲此,而不是在Google中搜索,我搜索了SO。在獲取當前位置的Android中的問題
我只想顯示當前位置的經緯度。
見下面我的代碼:
public class LocationGetter extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
LocationManager mlocManager =
(LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new LocationManagerHelper();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 100,mlocListener);
if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
tv.append("Latitude:- " + LocationManagerHelper.getLatitude()
+ '\n');
tv.append("Longitude:- " + LocationManagerHelper.getLongitude()
+ '\n');
Log.i("MyLocation1",Double.toString(LocationManagerHelper.getLatitude())+" "+Double.toString(LocationManagerHelper.getLongitude()));
} else {
tv.setText("GPS is not turned on...");
}
/** set the content view to the TextView */
setContentView(tv);
}
/* Class My Location Listener */
public static class LocationManagerHelper implements LocationListener {
private static double latitude;
private static double longitude;
@Override
public void onLocationChanged(Location loc) {
latitude = loc.getLatitude();
longitude = loc.getLongitude();
Log.i("MyLocation",Double.toString(latitude)+" "+Double.toString(longitude));
}
@Override
public void onProviderDisabled(String provider) { }
@Override
public void onProviderEnabled(String provider) { }
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
public static double getLatitude() {
return latitude;
}
public static double getLongitude() {
return longitude;
}
}
}
我也清單文件添加權限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
看到輸出我得到:
其中i我錯了嗎?我不明白,它是一個簡單的代碼,爲什麼它不工作?
您是在模擬器上輸入「gpx」數據還是在設備上啓用了GPS? – Swaroop 2011-04-07 09:45:24
我在模擬器上使用它。我也試過這個:telnet localhost 5554,然後geo fix 12 72 ..它不工作。我哪裏錯了? – 2011-04-07 09:47:26
您是否嘗試過使用Eclipse ADT的DDMS仿真器控件?手動修復一個點? – Swaroop 2011-04-07 10:04:20