2017-08-30 10 views
-2
私は自分の位置を検出したい

、私はACCESS_FINE_LOCATION許可を使用していますが、私はアプリを実行すると、それがクラッシュすると、画面にエラーメッセージ発生します。動作を停止しましたアンドロイドでGPSを使用して位置を取得する方法は?

「APPNAME」を

以下は私のコードです、私は上記を実装するために使用しています:

public class MainActivity extends AppCompatActivity { 
    LocationManager locationManager; 
    Location location; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     locationManager = (LocationManager) 
         getSystemService(Context.LOCATION_SERVICE); 
     if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      return; 
     } 
     location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     Log.v("My Coordinates are: ", location.getLatitude() + " " + location.getLongitude()); 
    } 
} 

私はここで間違っていますか?

お願いします。あなたがロケーションマネージャインスタンス化する他の活動に再びあなたがやったそのコードに

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

を持っていないので、

+0

チェックこの[アンドロイド-GPSロケーションマネージャ](https://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/) –

+0

あなたはAndroidの6を使用している場合+これは、実行時のアクセス許可のために発生する可能性があります。承認された回答はhttps://stackoverflow.com/questions/40142331/how-to-request-location-permission-on-android-6で読むことができます。また、スタックトレースを提供できる方が簡単です。 – BajajG

+0

問題について詳しく説明するlogcatにstacktraceがあります。 Check https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this/23353174#23353174 –

答えて

0

はマニフェストにあなたの許可を置く:GPSがある場合

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

は、座標を取得します有効:これは完全な実装のサンプルがある

LocationListener locationListener = new MyLocationListener(); 
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 5000, 10, locationListener); 

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 Current City is: " 
      + cityName; 
     editLocation.setText(s); 
    } 

    @Override 
    public void onProviderDisabled(String provider) {} 

    @Override 
    public void onProviderEnabled(String provider) {} 

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

あなたの助けていただきありがとうございますが、移動中はlocationListenerの座標が更新され、停止していても毎回自分の位置を取得したいと考えています。 – Mathew

+0

はい、必要なときにいつでもあなたの場所を取得するために 'onLocationChanged'から収集したデータを使用することができます。 – Orvenito

関連する問題