2017-02-09 180 views
0

我想寫一個android應用程序,很明顯,它使用用戶的位置在地圖活動上放置標記,或者在這種情況下打印經緯度。但是,我找不到適合我的教程或以前的問題。我正在尋找絕對最簡單的方式,以位置對象或緯度和經度的形式使用gps來獲取用戶的當前位置。我當前的代碼如下所示,直接從this tutorial獲取當前GPS位置android studio

public class MainActivity extends AppCompatActivity 
     implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener { 
    private TextView lat, lng; 
    private GoogleApiClient mGoogleApiClient; 
    private Location mCurrentLocation; 
    private boolean mRequestingLocationUpdates; 
    private LocationRequest mLocationRequest; 

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

     lat = (TextView) findViewById(R.id.lat); 
     lng = (TextView) findViewById(R.id.lng); 

     // Create an instance of GoogleAPIClient. 
     if (mGoogleApiClient == null) { 
      mGoogleApiClient = new GoogleApiClient.Builder(this) 
        .addConnectionCallbacks(this) 
        .addOnConnectionFailedListener(this) 
        .addApi(LocationServices.API) 
        .build(); 
     } 
    } 

    protected void onStart() { 
     mGoogleApiClient.connect(); 
     super.onStart(); 
    } 

    protected void onStop() { 
     mGoogleApiClient.disconnect(); 
     super.onStop(); 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
     if (mGoogleApiClient.isConnected() && !mRequestingLocationUpdates) { 
      startLocationUpdates(); 
     } 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     stopLocationUpdates(); 
    } 

    protected void stopLocationUpdates() { 
     LocationServices.FusedLocationApi.removeLocationUpdates(
       mGoogleApiClient, this); 
    } 

    @Override 
    public void onConnected(Bundle bundle) { 
     mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(
       mGoogleApiClient); 
     if (mCurrentLocation != null) { 
      lat.setText(String.valueOf("Lat: " + mCurrentLocation.getLatitude())); 
      lng.setText(String.valueOf("Lng: " + mCurrentLocation.getLongitude())); 
     } 
     else 
      Log.i("Problem", "mCurrentLocation is null"); 

     if (mRequestingLocationUpdates) { 
      startLocationUpdates(); 
     } 
    } 

    protected void startLocationUpdates() { 
     LocationServices.FusedLocationApi.requestLocationUpdates(
       mGoogleApiClient, mLocationRequest, this); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     mCurrentLocation = location; 
     updateUI(); 
    } 

    private void updateUI() { 
     lat.setText(String.valueOf("Lat: " + mCurrentLocation.getLatitude())); 
     lng.setText(String.valueOf("Lng: " + mCurrentLocation.getLongitude())); 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 

    } 

    @Override 
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 

    } 
} 

和我的AndroidManifest.xml是:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.root.locationtest"> 

    <meta-data android:name="com.google.android.gms.version" 
     android:value="@integer/google_play_services_version" /> 

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

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="LocationTest" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

我似乎總是得到日誌消息「mCurrentLocation爲空」,因此沒有在應用更改。行mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);上還有一條警告,指出「呼叫需要可能被用戶拒絕的權限」。任何幫助表示感謝,提前謝謝。

編輯: 我周圍所有的線,或與「呼叫需要權限可以被用戶拒絕」與if語句:

if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == 
       PackageManager.PERMISSION_GRANTED) 

從而解決了這個警告。但是,onConnect()方法中的權限被拒絕,並且mCurrentLocation仍然爲空。

+0

添加你的AndroidManifest.xml文件 – user2025187

+0

只需按照我的教程在這個答案。這將幫助您獲取當前位置,緯度,經度,並且實時更新當前的緯度和經度。 http://stackoverflow.com/a/42131361/4201474 – TranHieu

回答

0

通話需要用戶可能會拒絕的權限。這個錯誤是由於android的更高版本,在運行時需要獲得用戶的許可。只需在清單中聲明不會有幫助。所以你需要添加代碼的權限。

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    }