2017-10-24 5 views
0

オフライン時に現在のロケートの経度と緯度の値を取得し、現在の場所をデータベースに保存したいと考えています。モバイルデータとWi-FiがオフでGPSがオンのときにデバイスの経度と緯度を取得することは可能ですか?ロケーションをオフラインにする

+0

はいできます。通常の位置リスナクラス自体が動作します。インターネットなしでは、これらの値をサーバーやDBに更新することはできません。 –

+0

こんにちは@RakeshPolo、あなたのコメントのおかげで、私はちょうど現在/リアルタイムの場所を取得し、データベースに保存したい – MNGorospe

+0

あなたの物理デバイスのデータベースに保存することができます。 –

答えて

1

このコードを使用してください。ユーザーが自分の場所の設定でWi-Fiまたはネットワークのみのオプションを選択していないことを確認します。高精度またはGPSのみでなければなりません。このコードは機能します。

プロバイダ::私たちが登録したいいると、プロバイダの名前を次のようにrequestLocationUpdates方法の

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(); 
    } 
} 
} 

のパラメータがあります。 minTime:ロケーションの更新間隔の最小間隔(ミリ秒単位)。 minDistance:ロケーションアップデート間の最小距離(メートル単位)。 リスナー:各ロケーション更新に対してonLocationChanged(Location)メソッドが呼び出されるLocationListener。

権限:

<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" /> 

バージョンロリポップより低いとマシュマロと高いバージョンの使用ランタイム許可のためのファイルをマニフェストに上記の権限を追加します。

+0

ありがとうございます! – MNGorospe

0

経度と緯度の価値を得るためにインターネット接続は必要ありませんが、その場所の言葉を完全なアドレスにしたい場合、または地図上に表示したい場合はインターネット接続が必要です。

Check this link for better explanation

関連する問題