2016-04-29 60 views
1

我試圖使用Google Play服務創建Android位置API。但是我一直持續將mLastLocation值設置爲null,並在消息中顯示「無法獲取位置。請確保設備上的位置已啓用」,我已給它顯示mLastLocation的空值。誰能告訴我我做錯了什麼?這是我一直在嘗試的代碼。提前致謝。mLastLocation值在使用GPS創建Android位置API時變爲空。

CODE:

package com.example.jamshi.locationapi; 

import android.app.Activity; 
import android.location.Location; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.GooglePlayServicesUtil; 
import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks; 
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener; 
import com.google.android.gms.location.LocationListener; 
import com.google.android.gms.location.LocationRequest; 
import com.google.android.gms.location.LocationServices; 

public class MainActivity extends Activity implements ConnectionCallbacks,OnConnectionFailedListener { 

    private static final String TAG = MainActivity.class.getSimpleName(); 
    private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 1000; 
    private Location mLastLocation; 
    private GoogleApiClient mGoogleApiClient; 

    private TextView tvLocation; 
    private Button btnShowLocation,btnLocationUpdates; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     tvLocation = (TextView) findViewById(R.id.tvLocation); 
     btnShowLocation = (Button) findViewById(R.id.btnShowLocation); 
     btnLocationUpdates = (Button) findViewById(R.id.btnLocationUpdates); 

     if(checkPlayServices()){ 
      buildGoogleApiClient(); 
     } 

     btnShowLocation.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       displayLocation(); 
      } 
     }); 

    } 

    private void displayLocation() { 

     mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
     if (mLastLocation != null) { 
      double latitude = mLastLocation.getLatitude(); 
      double longitude = mLastLocation.getLongitude(); 
      tvLocation.setText(latitude + ", " + longitude); 

     } else { 

      tvLocation.setText("(Couldn't get the location. Make sure location is enabled on the device)"); 
     } 
    } 

    private boolean checkPlayServices(){ 

     int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 
     if(resultCode != ConnectionResult.SUCCESS){ 
      if(GooglePlayServicesUtil.isUserRecoverableError(resultCode)){ 
       GooglePlayServicesUtil.getErrorDialog(resultCode,this,PLAY_SERVICES_RESOLUTION_REQUEST).show(); 
      }else{ 
       Toast.makeText(getApplicationContext(),"This device is not supported",Toast.LENGTH_LONG).show(); 
       finish(); 
      } 
      return false; 
     } 
     return true; 
    } 

    protected synchronized void buildGoogleApiClient() { 

     if (mGoogleApiClient == null) { 
      mGoogleApiClient = new GoogleApiClient.Builder(this) 
        .addConnectionCallbacks(this) 
        .addOnConnectionFailedListener(this) 
        .addApi(LocationServices.API).build(); 
     } 
    } 

    @Override 
    protected void onStart() { 

     if (mGoogleApiClient != null) { 
      mGoogleApiClient.connect(); 
     } 
     super.onStart(); 
    } 

    @Override 
    protected void onResume() { 

     super.onResume(); 
     checkPlayServices(); 
    } 


    @Override 
    public void onConnectionFailed(ConnectionResult result) { 

     Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " 
       + result.getErrorCode()); 
    } 

    @Override 
    public void onConnected(Bundle arg0) { 

     displayLocation(); 
    } 

    @Override 
    public void onConnectionSuspended(int arg0) 
    { 
     mGoogleApiClient.connect(); 
    } 

} 
+0

您在設備或模擬器測試? b'z模擬器沒有以前的位置數據。 – androidnoobdev

+0

我在模擬器中測試 – user6239257

+0

在onStart中,爲什麼您嘗試連接到GoogleApiClient客戶端,如果它不爲null? 此外,單擊按鈕或開始活動時是否顯示錯誤消息? –

回答

0

如果您在模擬器中運行,然後打開谷歌地圖應用程序,然後再嘗試你的應用程序在設備

+0

我在模擬器中測試...................我已經打開谷歌地圖應用程序,並再次重複同樣的錯誤............ – user6239257

+0

我已經在android設備上測試過..................再次重複相同的錯誤 – user6239257

+0

嘗試在** displayLocation()**頂部添加此代碼。 ** mLocationRequest = LocationRequest.create(); ** ** mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); ** 和不要忘記聲明變量**私人LocationRequest mLocationRequest; ** –

0

測試,如果你想在第一時間和genymotion仿真器的數據從側面菜單中設置位置,然後嘗試。如果你正在使用android studio模擬器設置模擬位置,然後嘗試。

詳細檢查這一點 - How to emulate GPS location in the Android Emulator?

+0

我已經在測試android設備..............但同樣的錯誤重複.................. – user6239257