0
A
回答
1
是的,你可以隨時通過GPS獲得的位置,因爲使用GPS,你不需要互聯網連接。
3
是的。使用GPS_PROVIDER,並使用LocationManager,而不是播放服務位置api
1
您可以使用此示例。剛開始並使用getLatitude()和getLongitude()用於獲取位置
import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;
import com.pakban.mobile.warmish.Activityes.MapsActivity;
public class GPSTracker extends Service implements LocationListener {
private final Context mContext;
// flag for GPS status
boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
// flag for GPS status
boolean canGetLocation = false;
Location location; // location
double latitude; // latitude
double longitude; // longitude
// 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
// Declaring a Location Manager
protected LocationManager locationManager;
public GPSTracker(Context context) {
this.mContext = context;
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;
// First get location from Network Provider
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
/**
* Stop using GPS listener
* Calling this function will stop using GPS in your app
* */
public void stopUsingGPS(){
if(locationManager != null){
locationManager.removeUpdates(GPSTracker.this);
}
}
/**
* Function to get latitude
* */
public double getLatitude(){
if(location != null){
latitude = location.getLatitude();
}
// return latitude
return latitude;
}
/**
* Function to get longitude
* */
public double getLongitude(){
if(location != null){
longitude = location.getLongitude();
}
// return longitude
return longitude;
}
/**
* Function to check GPS/wifi enabled
* @return boolean
* */
public boolean canGetLocation() {
return this.canGetLocation;
}
/**
* Function to show settings alert dialog
* On pressing Settings button will lauch Settings Options
* */
public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
// Setting Dialog Title
alertDialog.setTitle("GPS is settings");
// Setting Dialog Message
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
// On pressing Settings button
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
@Override
public void onLocationChanged(Location location) {
// this method will execute if you location changed. use it //for update location
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
你應該寫這樣的事情:
GPSTracker gPSTracker =new GPSTracker(context);
然後就可以像這樣得到的位置:
if (gPSTracker.canGetLocation()) {
Log.d("your lat",""+ gPSTracker.getLatitude());
Log.d("your lang",""+ gPSTracker.getLongitude());
}
,您必須在清單中添加此權限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
相關問題
- 1. IPHONE:我可以使用沒有互聯網的GPS來獲取當前的緯度和經度
- 2. 有沒有辦法獲得緯度和經度沒有GPS接收器和CellID
- 3. 只有在沒有互聯網可用的情況下,我該如何使用設備的GPS?
- 4. Silverlight可以在沒有互聯網連接的情況下使用LAN嗎?
- 5. CLLocationManager在沒有互聯網的情況下工作嗎?
- 6. 我們可以在沒有DataAdapter的情況下填充DataSet嗎?
- 7. 如何獲取我的當前位置在緯度和經度? (不帶GPS,但帶有互聯網服務)
- 8. 有沒有辦法在給定經度和緯度的情況下獲得一個隨機的aproximation點
- 9. 只有在有互聯網連接的情況下nock才能工作嗎?
- 10. Android:使GSON的LazyLoading ListView可以在沒有互聯網的情況下使用
- 11. 緯度經度通過互聯網
- 12. 我可以在沒有授權的情況下獲得曲目嗎?
- 13. 如何在不使用GPS的情況下每秒查找經度和緯度
- 14. 我沒有在我的MapView中獲得我的經度和緯度?
- 15. 沒有數據連接時可以獲取經緯度嗎?
- 16. 我可以使用OpenStreet Tiles在沒有互聯網訪問的情況下運行Google Maps API嗎?
- 17. 我可以在沒有互聯網連接的情況下訪問Flash安全管理器嗎?
- 18. 在沒有map的情況下獲取GoogleMaps光標下的經度和緯度.onMouseMove
- 19. 有沒有任何方法可以在沒有互聯網的情況下從源碼構建TensorFlow?
- 20. 沒有得到緯度和龍通過GPS或網絡
- 21. 我的團隊和我能夠在沒有互聯網連接的情況下在Android Studio中工作嗎?
- 22. 沒有互聯網的Phonegap GPS
- 23. 通過GPS獲取緯度和經度
- 24. 我可以在沒有CALayers的情況下使用Core Animation嗎?
- 25. 我可以在沒有Visual Studio的情況下創建ClickOnce嗎?
- 26. 我可以在沒有api_key的情況下訪問Facebook API嗎?
- 27. 我可以在沒有Node.js的情況下使用Tumblr.js嗎?
- 28. 我可以在沒有pgAdmin4的情況下安裝postgresql嗎?
- 29. 我可以在沒有Zend_Application的情況下使用zfdebug嗎?
- 30. 我可以在沒有ADFS的情況下設置SAML IDP嗎?