2017-08-14 57 views

回答

0

希望这将帮助你...

把你的清单文件

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

获得API版本23及以上的运行时间许可。

然后把这个代码在你的java文件

private synchronized void buildGoogleApiClient() { 
Log.i("TAG", "Building GoogleApiClient"); 
    mGoogleApiClient = new GoogleApiClient.Builder(getActivity().getApplicationContext()) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(LocationServices.API) 
      .build(); 
    createLocationRequest();} 

private void createLocationRequest() { 
    Log.i("TAG", "CreateLocationRequest"); 
    mLocationRequest = new LocationRequest(); 
    long UPDATE_INTERVAL = 10 * 1000; 
    mLocationRequest.setInterval(UPDATE_INTERVAL); 
    long FASTEST_INTERVAL = 10000; 
    mLocationRequest.setFastestInterval(FASTEST_INTERVAL); 
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
    builder = new LocationSettingsRequest.Builder() 
      .addLocationRequest(mLocationRequest); 
    //************************** 
    builder.setAlwaysShow(true); //this is the key ingredient 
    //************************** 

} 

private void startLocationUpdates() { 

    Log.i("TAG", "StartLocationUpdates"); 

    if (Build.VERSION.SDK_INT >= 23) { 
     if (ContextCompat.checkSelfPermission(getActivity().getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(getActivity().getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 
      // TODO: Consider calling 
      LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, 
        mLocationRequest, this); 

     } 
    } else { 
     LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, 
       mLocationRequest, this); 
    } 

} 

private void stopLocationUpdates() { 
    Log.i("TAG", "StopLocationUpdates"); 
    LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 

} 
@Override 
public void onConnectionSuspended(int i) { 
    Log.i("TAG", "onConnectionSuspended"); 
    if (i == CAUSE_SERVICE_DISCONNECTED) { 
     Toast.makeText(getActivity().getApplicationContext(), "Disconnected. Please re-connect.", Toast.LENGTH_SHORT).show(); 
    } else if (i == CAUSE_NETWORK_LOST) { 
     Toast.makeText(getActivity().getApplicationContext(), "Network lost. Please re-connect.", Toast.LENGTH_SHORT).show(); 
    } 
    mGoogleApiClient.connect(); 
} 

@Override 
public void onLocationChanged(Location location) { 
    Log.i("TAG", "OnLocationChanged"); 
    Log.i("TAG", "Current Location==>" + location); 
    currentlatitude = location.getLatitude(); 
    currentlongitude = location.getLongitude(); 
} 


    @Override 
public void onConnectionFailed(ConnectionResult connectionResult) { 
if (connectionResult.hasResolution()) { 
     try { 
      // Start an Activity that tries to resolve the error 
      connectionResult.startResolutionForResult(getActivity(), connectionResult.RESOLUTION_REQUIRED); 
     } catch (IntentSender.SendIntentException e) { 
      e.printStackTrace(); 
     } 
    } else { 
     Log.e("TAG", "Location services connection failed with code==>" + connectionResult.getErrorCode()); 
     Log.e("TAG", "Location services connection failed Because of==> " + connectionResult.getErrorMessage()); 
    } 

} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    if (mGoogleApiClient != null) 
     mGoogleApiClient.disconnect(); 
} 
@Override 
public void onConnected(Bundle bundle) { 
    Location mCurrentLocation; 

    Log.i("TAG", "OnConnected"); 
    if (Build.VERSION.SDK_INT >= 23) { 
     if (ContextCompat.checkSelfPermission(getActivity().getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(getActivity().getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 
      // TODO: Consider calling 
      mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 

      // Note that this can be NULL if last location isn't already known. 
      if (mCurrentLocation != null) { 
       // Print current location if not null 
       Log.d("DEBUG", "current location: " + mCurrentLocation.toString()); 
       currentlatitude = mCurrentLocation.getLatitude(); 
       currentlongitude = mCurrentLocation.getLongitude(); 
      } else { 
       startLocationUpdates(); 
      } 


     } 
    } else { 
     mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 

     // Note that this can be NULL if last location isn't already known. 
     if (mCurrentLocation != null) { 
      // Print current location if not null 
      Log.d("DEBUG", "current location: " + mCurrentLocation.toString()); 
      currentlatitude = mCurrentLocation.getLatitude(); 
      currentlongitude = mCurrentLocation.getLongitude(); 
     } 
     // Begin polling for new location updates. 
     startLocationUpdates(); 
    } 
} 

更多详细信息请点击此链接:https://developers.google.com/android/guides/setup

https://developer.android.com/training/location/retrieve-current.html

+0

所以,我不得不在这个文件的新方法,以获取那个地点。我对么。我只想创建这个类的实例并调用方法来获取位置。 – bibek

相关问题