我正在嘗試獲取用戶的位置longitude以及l(fā)atitude從GPS/network哪個(gè)位置可用,我想快速獲取它,否則,如果現(xiàn)在無法獲取新的位置坐標(biāo),則會返回以前的位置坐標(biāo)。這是我的嘗試:public class GPSTracker extends Service{ // The minimum distance to change Updates in meters private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters // The minimum time between updates in milliseconds private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute private final Context mContext; // Declaring a Location Manager protected LocationManager locationManager; // flag for GPS status boolean isGPSEnabled = false; // flag for network status boolean isNetworkEnabled = false; // flag for GPS status boolean canGetLocation = false; Location location = null; // location double latitude; // latitude double longitude; // longitude myLocationLitener locationLitener; public GPSTracker(Context context) { this.mContext = context; locationLitener = new myLocationLitener(); getLocation(); } public Location getLocation() { try { locationManager = (LocationManager) mContext .getSystemService(LOCATION_SERVICE); // getting GPS status isGPSEnabled = locationManager .isProviderEnabled(LocationManager.GPS_PROVIDER); // getting network status isNetworkEnabled = locationManager .isProviderEnabled(LocationManager.NETWORK_PROVIDER); if (!isGPSEnabled && !isNetworkEnabled) { // no network provider is enabled } else { this.canGetLocation = true; if (isNetworkEnabled) { locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES,locationLitener); Log.d("Network", "Network Enabled"); if (locationManager != null) { location = locationManager我如何使用該課程?
- 3 回答
- 0 關(guān)注
- 364 瀏覽
添加回答
舉報(bào)
0/150
提交
取消