【问题标题】:Getting location offline离线获取位置
【发布时间】:2018-04-04 23:38:15
【问题描述】:

我想在离线时获取当前位置的经度和纬度值,并将当前位置保存到其数据库中。当移动数据和wifi关闭但GPS打开时,是否可以获得设备的经度和纬度?

【问题讨论】:

  • 是的,你可以。正常的位置侦听器类本身将起作用。但您无法在没有互联网的情况下将这些值更新到您的服务器或数据库。
  • 嗨@RakeshPolo,感谢您的评论,我只想获取当前/实时位置并将其保存在数据库中
  • 您可以将其保存在物理设备的数据库中。
  • 是的,但我的意思是它是实时的吗?就像一分钟后我的位置发生了变化,纬度和经度会在不打开移动连接或 wifi 但 GPS 开启的情况下发生变化
  • 是的,你可以。请检查我的答案。

标签: android android-location android-gps


【解决方案1】:

只需使用此代码。确保用户没有在其位置设置中选择仅 wifi 或仅网络选项。它必须是高精度或仅 GPS。这段代码将起作用。

public class Location extends AppCompatActivity {
LocationManager locationManager;
Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_location);
    mContext=this;
    locationManager=(LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER,
            2000,
            10, locationListenerGPS);
    isLocationEnabled();

}

LocationListener locationListenerGPS=new LocationListener() {
    @Override
    public void onLocationChanged(android.location.Location location) {
        double latitude=location.getLatitude();
        double longitude=location.getLongitude();
        String msg="New Latitude: "+latitude + "New Longitude: "+longitude;
        Toast.makeText(mContext,msg,Toast.LENGTH_LONG).show();
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
};


protected void onResume(){
    super.onResume();
    isLocationEnabled();
}

private void isLocationEnabled() {

    if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
        AlertDialog.Builder alertDialog=new AlertDialog.Builder(mContext);
        alertDialog.setTitle("Enable Location");
        alertDialog.setMessage("Your locations setting is not enabled. Please enabled it in settings menu.");
        alertDialog.setPositiveButton("Location Settings", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int which){
                Intent intent=new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);
            }
        });
        alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int which){
                dialog.cancel();
            }
        });
        AlertDialog alert=alertDialog.create();
        alert.show();
    }
    else{
        AlertDialog.Builder alertDialog=new AlertDialog.Builder(mContext);
        alertDialog.setTitle("Confirm Location");
        alertDialog.setMessage("Your Location is enabled, please enjoy");
        alertDialog.setNegativeButton("Back to interface",new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int which){
                dialog.cancel();
            }
        });
        AlertDialog alert=alertDialog.create();
        alert.show();
    }
}
}

requestLocationUpdates方法的参数如下:

提供者:我们要注册的提供者的名称。 minTime:位置更新之间的最小时间间隔(以毫秒为单位)。 minDistance:位置更新之间的最小距离(以米为单位)。 listener:一个 LocationListener,每次更新位置都会调用其 onLocationChanged(Location) 方法。

权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

为低于 lollipop 的版本添加上述权限到清单文件,对于棉花糖及更高版本使用运行时权限。

【讨论】:

  • 非常感谢...这段代码拯救了我的一天!就是这么简单,不需要运行任何服务或任何东西...... !!!
  • 我不得不恢复使用它,因为我需要在没有网络的情况下获取位置。即使谷歌文档说使用 fusedLocationClient.
  • 如果我的设备上没有 SIM 卡,甚至没有互联网,我可以获取位置吗?
猜你喜欢
  • 2016-04-24
  • 1970-01-01
  • 2018-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多