Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main2Activity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,
- GoogleApiClient.OnConnectionFailedListener, LocationListener {
- Location mLastLocation;
- private GoogleApiClient mGoogleApiClient;
- private LocationRequest mLocationRequest;
- String lat, lon;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main2);
- buildGoogleApiClient();
- }
- @Override
- public void onConnected(Bundle bundle) {
- mLocationRequest = LocationRequest.create();
- mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
- mLocationRequest.setInterval(100); // Update location every second
- LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
- mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
- mGoogleApiClient);
- if (mLastLocation != null) {
- lat = String.valueOf(mLastLocation.getLatitude());
- lon = String.valueOf(mLastLocation.getLongitude());
- }
- }
- @Override
- public void onConnectionSuspended(int i) {
- }
- @Override
- public void onLocationChanged(Location location) {
- lat = String.valueOf(location.getLatitude());
- lon = String.valueOf(location.getLongitude());
- Log.d("System out", "Latitude:"+lat+"::Longitude:"+lon);
- }
- @Override
- public void onConnectionFailed(ConnectionResult connectionResult) {
- buildGoogleApiClient();
- }
- synchronized void buildGoogleApiClient() {
- mGoogleApiClient = new GoogleApiClient.Builder(this)
- .addConnectionCallbacks(this)
- .addOnConnectionFailedListener(this)
- .addApi(LocationServices.API)
- .build();
- }
- @Override
- protected void onStart() {
- super.onStart();
- mGoogleApiClient.connect();
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- mGoogleApiClient.disconnect();
- }
- }
Add Comment
Please, Sign In to add comment