2016-12-15 7 views
-2

こんにちは、みんな地図活動をしています。私は私の正確な場所の住所、すなわち名前を取得し、それを保存し、後で情報を取得したいと思います。現在の場所を保存して検索する

public class GetCurrentLocation extends Activity 
     implements OnClickListener { 

    private LocationManager locationMangaer = null; 
    private LocationListener locationListener = null; 

    private Button btnGetLocation = null; 
    private EditText editLocation = null; 
    private ProgressBar pb = null; 

    private static final String TAG = "Debug"; 
    private Boolean flag = false; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_get_current_location); 


     //if you want to lock screen for always Portrait mode 
     setRequestedOrientation(ActivityInfo 
       .SCREEN_ORIENTATION_PORTRAIT); 

     pb = (ProgressBar) findViewById(R.id.progressBar1); 
     pb.setVisibility(View.INVISIBLE); 

     editLocation = (EditText) findViewById(R.id.editTextLocation); 

     btnGetLocation = (Button) findViewById(R.id.btnLocation); 
     btnGetLocation.setOnClickListener(this); 

     locationMangaer = (LocationManager) 
       getSystemService(Context.LOCATION_SERVICE); 

    } 

    @Override 
    public void onClick(View v) { 
     flag = displayGpsStatus(); 
     if (flag) { 

      Log.v(TAG, "onClick"); 

      editLocation.setText("Please!! move your device to" + 
        " see the changes in coordinates." + "\nWait.."); 

      pb.setVisibility(View.VISIBLE); 
      locationListener = new MyLocationListener(); 

      locationMangaer.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, locationListener); 

     } else { 
      alertbox("Gps Status!!", "Your GPS is: OFF"); 
     } 

    } 

    /*----Method to Check GPS is enable or disable ----- */ 
    private Boolean displayGpsStatus() { 
     ContentResolver contentResolver = getBaseContext() 
       .getContentResolver(); 
     boolean gpsStatus = Settings.Secure 
       .isLocationProviderEnabled(contentResolver, 
         LocationManager.GPS_PROVIDER); 
     if (gpsStatus) { 
      return true; 

     } else { 
      return false; 
     } 
    } 

    /*----------Method to create an AlertBox ------------- */ 
    protected void alertbox(String title, String mymessage) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setMessage("Your Device's GPS is Disable") 
       .setCancelable(false) 
       .setTitle("** Gps Status **") 
       .setPositiveButton("Gps On", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int id) { 
           // finish the current activity 
           // AlertBoxAdvance.this.finish(); 
           Intent myIntent = new Intent(
             Settings.ACTION_SECURITY_SETTINGS); 
           startActivity(myIntent); 
           dialog.cancel(); 
          } 
         }) 
       .setNegativeButton("Cancel", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int id) { 
           // cancel the dialog box 
           dialog.cancel(); 
          } 
         }); 
     AlertDialog alert = builder.create(); 
     alert.show(); 
    } 

    /*----------Listener class to get coordinates ------------- */ 
    private class MyLocationListener implements LocationListener { 
     @Override 
     public void onLocationChanged(Location loc) { 

      editLocation.setText(""); 
      pb.setVisibility(View.INVISIBLE); 
      Toast.makeText(getBaseContext(), "Location changed : Lat: " + 
          loc.getLatitude() + " Lng: " + loc.getLongitude(), 
        Toast.LENGTH_SHORT).show(); 
      String longitude = "Longitude: " + loc.getLongitude(); 
      Log.v(TAG, longitude); 
      String latitude = "Latitude: " + loc.getLatitude(); 
      Log.v(TAG, latitude); 

    /*----------to get City-Name from coordinates ------------- */ 
      String cityName = null; 
      Geocoder gcd = new Geocoder(getBaseContext(), 
        Locale.getDefault()); 
      List<Address> addresses; 
      try { 
       addresses = gcd.getFromLocation(loc.getLatitude(), loc 
         .getLongitude(), 1); 
       if (addresses.size() > 0) 
        System.out.println(addresses.get(0).getLocality()); 
       cityName = addresses.get(0).getLocality(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

      String s = longitude + "\n" + latitude + 
        "\n\nMy Currrent City is: " + cityName; 
      editLocation.setText(s); 
     } 

     @Override 
     public void onProviderDisabled(String provider) { 
      // TODO Auto-generated method stub 
     } 

     @Override 
     public void onProviderEnabled(String provider) { 
      // TODO Auto-generated method stub 
     } 

     @Override 
     public void onStatusChanged(String provider, 
            int status, Bundle extras) { 
      // TODO Auto-generated method stub 
     } 
    } 
} 
+0

申し訳ありませんが、あなたが –

+0

Mishustinをしたいです私は何をしたいクリアしません現在の場所を保存し、後でその保存された情報を取得するコード –

+0

SharedPreferencesを使用します。正確に何が問題になっていますか? –

答えて

0

ストアあなたの緯度/経度値:

PreferenceManager.getDefaultSharedPreferences(context).getString("Latitude", "No Latitude Value Stored"); 

PreferenceManager.getDefaultSharedPreferences(context).getString("Longitude", "No Longitude Value Stored"); 
+0

単一の文字列を使用してOPが要求するものではないかもしれない最後のデータを除いてすべてを消去します。 – Rippr

+0

真ですが、私は彼の "現在"の場所を保存する必要があることに基づいて前提を作りました。これは、緯度/経度の唯一のセットであると理解しています。 – JamesSwinton

+1

"それを保存して後で情報を取得する"私の解釈の情報は最後に保存されたデータ以上のものですが、間違っているかもしれません...とにかく、あなたの答えは間違っていません。彼は単にコードを微調整したり、必要があれば援助を求めることができます。 – Rippr

0
あなたのデータを格納するための

使用する共有環境設定を:

PreferenceManager.getDefaultSharedPreferences(context).edit().putString("Latitude", Latitude).commit(); 

PreferenceManager.getDefaultSharedPreferences(context).edit().putString("Longitude", Longitude).commit(); 

はその後使用して、それを取り出します。配列リストを作成し、座標リストを更新して保存します。 Here's参照のためのShared preferencesドキュメント。コードスニペットや何かの説明が必要な場合は、お気軽にお問い合わせください。

UPDATE

Set<String> Lats = new HashSet<String>(); 
Set<String> Longs = new HashSet<String>(); 
Lats.add(Latitude); 
Longs.add(Longitude); 
Context C = getApplicationContext(); 
SharedPreferences SP = C.getSharedPreferences("My_Prefs",MODE_PRIVATE); 
SharedPreferences.Editor E = SP.edit(); 
E.clear(); 
E.putStringSet("Lats",Lats); 
E.putStringSet("Longs",Longs); 
E.commit(); 

これは、あなたが最初にそれを保存するときのためです。あなたはすでに、新しいものを追加し、それを保存し、古い値を取得し、一つ以上のペアを保存したとき:

Context C = getApplicationContext(); 
SharedPreferences SP = C.getSharedPreferences("My_Prefs",MODE_PRIVATE); 
Set<String> Lats = SP.getStringSet("Lats",null); 
Set<String> Longs = SP.getStringSet("Longs",null); 

希望を私が助けた:D

+0

Thanlあなたすべて。リップルコードスニペットは –

+0

を更新しました。答えが満足できるものであれば、それを受け入れられたものとしてマークする:D – Rippr

関連する問題