Last active
July 26, 2018 13:41
-
-
Save xsahil03x/64a288b40e1719c9ce03dcdd8d8f338f to your computer and use it in GitHub Desktop.
Location Api Gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.panchal.vivek.mapproject; | |
import android.Manifest; | |
import android.annotation.SuppressLint; | |
import android.content.Intent; | |
import android.content.pm.PackageManager; | |
import android.location.Geocoder; | |
import android.location.Location; | |
import android.os.Bundle; | |
import android.os.Handler; | |
import android.support.annotation.NonNull; | |
import android.support.v4.app.ActivityCompat; | |
import android.support.v4.content.ContextCompat; | |
import android.support.v4.os.ResultReceiver; | |
import android.support.v7.app.AppCompatActivity; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import com.google.android.gms.location.FusedLocationProviderClient; | |
import com.google.android.gms.location.LocationCallback; | |
import com.google.android.gms.location.LocationRequest; | |
import com.google.android.gms.location.LocationResult; | |
import com.google.android.gms.location.LocationServices; | |
import com.google.android.gms.tasks.OnSuccessListener; | |
public class MainActivity extends AppCompatActivity { | |
private static final int REQUEST_CODE = 100; | |
private TextView mLocationTextView; | |
private FusedLocationProviderClient mFusedLocationClient; | |
private static Location mCurrentLocation; | |
private LocationCallback mLocationCallback; | |
@Override | |
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { | |
super.onRequestPermissionsResult(requestCode, permissions, grantResults); | |
switch (requestCode) { | |
case REQUEST_CODE: | |
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { | |
Toast.makeText(this, "Hurray Permission Granted", | |
Toast.LENGTH_SHORT).show(); | |
} else if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_DENIED) { | |
Toast.makeText(this, "Location permission not granted, " + | |
"restart the app if you want the feature", | |
Toast.LENGTH_SHORT).show(); | |
} | |
} | |
} | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
permissionCheck(); | |
mLocationTextView = findViewById(R.id.location_text); | |
Button mBtnLocation = findViewById(R.id.btn_location); | |
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this); | |
buildLocationCallback(); | |
mBtnLocation.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
startLocationUpdates(); | |
getAddress(); | |
} | |
}); | |
} | |
private void permissionCheck() { | |
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { | |
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE); | |
} | |
} | |
private void buildLocationCallback() { | |
mLocationCallback = new LocationCallback() { | |
@Override | |
public void onLocationResult(LocationResult locationResult) { | |
if (locationResult == null) { | |
return; | |
} | |
for (Location location : locationResult.getLocations()) { | |
mCurrentLocation.set(location); | |
} | |
} | |
}; | |
} | |
private LocationRequest buildLocationRequest() { | |
LocationRequest mLocationRequest = new LocationRequest(); | |
mLocationRequest.setInterval(10000); | |
mLocationRequest.setFastestInterval(5000); | |
mLocationRequest.setSmallestDisplacement(10); | |
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); | |
return mLocationRequest; | |
} | |
private class LocationAddressResultReceiver extends ResultReceiver { | |
@SuppressLint("RestrictedApi") | |
LocationAddressResultReceiver(Handler handler) { | |
super(handler); | |
} | |
@Override | |
protected void onReceiveResult(int resultCode, Bundle resultData) { | |
if (resultCode == 0) { | |
//Last Location can be null for various reasons | |
//for example the api is called first time | |
//so retry till location is set | |
//since intent service runs on background thread, it doesn't block main thread | |
Log.d("Address", "Location null retrying"); | |
getAddress(); | |
} | |
if (resultCode == 1) { | |
Toast.makeText(MainActivity.this, | |
"Address not found, ", | |
Toast.LENGTH_SHORT).show(); | |
} | |
String currentAdd = resultData.getString("address_result"); | |
mLocationTextView.setText(currentAdd); | |
} | |
} | |
private void startLocationUpdates() { | |
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { | |
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE); | |
} else { | |
mFusedLocationClient.requestLocationUpdates(buildLocationRequest(), mLocationCallback, null); | |
} | |
} | |
private void getAddress() { | |
if (!Geocoder.isPresent()) { | |
Toast.makeText(MainActivity.this, | |
"Can't find current address, ", | |
Toast.LENGTH_SHORT).show(); | |
return; | |
} | |
LocationAddressResultReceiver addressResultReceiver = new LocationAddressResultReceiver(new Handler()); | |
Intent intent = new Intent(this, GetAddressIntentService.class); | |
intent.putExtra("add_receiver", addressResultReceiver); | |
intent.putExtra("add_location", mCurrentLocation); | |
startService(intent); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment