0

我在GoogleApiClient的本地化中遇到了很大的麻煩。我已經實施內置GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListenerLocationListener根本不起作用。似乎這些接口尚未註冊到我的應用程序中。以下是我的代碼。請看方法:onLocationChanged,onConnectionSuspended,onConnected, onConnectionFailed。在我添加的每個示例中,打印LOG的一些文本,並且在這些示例中的任何一個logs都未打印。Android中的GoogleApiClient無法在應用程序中註冊

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener { 

    private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000; 

    private GoogleMap mMap; 
    private Location location; 

    ///////////////////////////////////////////// 
    private GoogleApiClient mGoogleApiClient; 
    public static final String TAG = MapsActivity.class.getSimpleName(); 
    private LocationRequest mLocationRequest; 
    private LatLng latLng; 

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


     // Create the LocationRequest object 
     mLocationRequest = LocationRequest.create() 
       .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) 
       .setInterval(10 * 1000)  // 10 seconds, in milliseconds 
       .setFastestInterval(1 * 1000); // 1 second, in milliseconds 


     dataBaseHelper = new DataBaseHelper(getApplicationContext()); 

     SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 


     /* some code here */ 

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


    @Override 
    public void onMapReady(final GoogleMap googleMap) { 
     mMap = googleMap; 


    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     setUpMapIfNeeded(); 
     mGoogleApiClient.connect(); 

    } 

    private void setUpMapIfNeeded() { 

     if (mMap == null) { 
      ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)) 
        .getMapAsync(this); 
      if (mMap != null) { 
       //setUpMap(); 
      } 
     } 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     if (mGoogleApiClient.isConnected()) { 
      LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
      mGoogleApiClient.disconnect(); 
     } 
    } 


    private void handleNewLocation(Location location) { 
     Log.d(TAG, location.toString()); 

     double currentLatitude = location.getLatitude(); 
     double currentLongitude = location.getLongitude(); 
     final LatLng latLng = new LatLng(currentLatitude, currentLongitude); 
     setLatLng(latLng); 

     MarkerOptions options = new MarkerOptions() 
       .position(latLng) 
       .title("I am here!"); 
     mMap.addMarker(options); 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
     mMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null); 

     generateMap(getLatLng(), "3000"); 

     sItems.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 

      @Override 
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
       washLocations.clear(); 
       washLocations.size(); 
       String latitude = String.valueOf(latLng.latitude); 
       String longitude = String.valueOf(latLng.longitude); 

       generateMap(getLatLng(), null); 
      } 

      @Override 
      public void onNothingSelected(AdapterView<?> parent) { 
      } 
     }); 
    } 


    private void setUpMap() { 
     mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker")); 
    } 

    @Override 
    public void onConnected(@Nullable Bundle bundle) { 
     Log.d("location test", "tes1"); 
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      return; 
     } 
     Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
     if (location == null) { 
      LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
     } else { 
      handleNewLocation(location); 
     } 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 
     Log.i("faild", "Location services suspended. Please reconnect."); 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 
     if (connectionResult.hasResolution()) { 
      try { 
       // Start an Activity that tries to resolve the error 
       connectionResult.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST); 
      } catch (IntentSender.SendIntentException e) { 
       e.printStackTrace(); 
      } 
     } else { 
      Log.i("failds", "Location services connection failed with code " + connectionResult.getErrorCode()); 
     } 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     Log.d("location test", "tes2"); 
     handleNewLocation(location); 
    } 
} 

正如您在上面看到:

mLocationRequest = LocationRequest.create() 
       .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) 
       .setInterval(10 * 1000)  // 10 seconds, in milliseconds 
       .setFastestInterval(1 * 1000); 

地圖應該刷新每10秒 - 所以這意味着它也應該打印Log.d("location test", "tes2");每10秒 - 但事實並非如此。

在這裏,這是我的build.gradle,我把每一個需要libriaries: 應用插件: 'com.android.application'

android { 
    compileSdkVersion 25 
    buildToolsVersion "25.0.2" 
    defaultConfig { 
     applicationId "com.example.micha.locationtest" 
     minSdkVersion 15 
     targetSdkVersion 25 
     versionCode 1 
     versionName "1.0" 
     testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
    useLibrary 'org.apache.http.legacy' 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 
     exclude group: 'com.android.support', module: 'support-annotations' 
    }) 
    compile 'com.android.support:appcompat-v7:25.2.0' 
    compile 'com.google.android.gms:play-services-maps:10.2.0' 
    testCompile 'junit:junit:4.12' 
    compile 'com.android.support:recyclerview-v7:25.2.0' 
    compile 'com.google.android.gms:play-services-location:10.2.0' 
} 

回答

0

首先科瑞googleApiClient對象像下面方法

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

之後獲得如下位置代碼

if (mGoogleApiClient.isConnected()) { 
    mLocationRequest = new LocationRequest(); 
    mLocationRequest.setInterval(2000); 
    mLocationRequest.setFastestInterval(10); 
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 


if (mLastLocation != null) { 
double lon = mLastLocation.getLongitude(); 
double lat = mLastLocation.getLatitude(); 

} 
+0

不起作用。什麼是'Utilis'?我根本無法導入它... – bielas

+0

忽略那個util只看到我編輯的帖子 –

0

最後我通過改變方法解決了這個情況:

 @Override 
    public void onConnected(@Nullable Bundle bundle) { 

     mLocationRequest = LocationRequest.create(); 
     mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
     mLocationRequest.setInterval(10000); 
     mLocationRequest.setFastestInterval(10000); 
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      return; 
     } 
     LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
     Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
     handleNewLocation(location); 
    } 
相關問題