I am new to Android and I am trying to develop an Android app which shows current location of the user. I am using Genymotion. Now I am using
mLocation=Loca
LocationListener locationListener;
LocationManager locationManager;
double latitude;
double longitude;
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener()
{
@Override
public void onLocationChanged(Location location) {
longitude = location.getLongitude();
latitude = location.getLatitude();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000, 0, locationListener);
Log.e("location", longitude + " " + latitude);
For Map Service, It requires Google Play Services. Make Sure Your Genymotion Has PlayService installed if not Get a help from Here To Install Google Play Servies in Genymotion. Happy Coding..!!!
posting simple solution with code.
add permissions inside AndroidManifest.xml file
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
if you app is marshmallow compatible then check run time permission.
add dependency inside gradle file:
compile 'com.google.android.gms:play-services:9.2.0'
implements this two interface in you activity
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener
and
create googleapiclient object like this in oncreate:
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
and make this on start activity
mGoogleApiClient.connect();
here we go on result callback of location on this override method.
public void onConnected(@Nullable Bundle bundle) {
Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLastLocation != null) {
Log.e(TAG, "onConnected: " + String.valueOf(mLastLocation.getLatitude()) + ":" + String.valueOf(mLastLocation.getLongitude()));
} else {
Toast.makeText(getApplicationContext(), "Your Location Not Found", Toast.LENGTH_LONG).show();
}
}
full code of activity is something like that:
public class LocationActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private static final String TAG = LocationActivity.class.getSimpleName();
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
// Create an instance of GoogleAPIClient.
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
}
@Override
protected void onStart() {
// connect googleapiclient
mGoogleApiClient.connect();
super.onStart();
}
@Override
protected void onStop() {
// disconnect googleapiclient
mGoogleApiClient.disconnect();
super.onStop();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLastLocation != null) {
// here we go you can see current lat long.
Log.e(TAG, "onConnected: " + String.valueOf(mLastLocation.getLatitude()) + ":" + String.valueOf(mLastLocation.getLongitude()));
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}}
p.s. googleapiclient is not working in emulator without play services, so you have to test it on real devices. please make sure gps is enable on that device.
if you want to use traditional method for get location then try this tutorial. http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/
------UPDATE JUNE 15---------
for latest apis check android google post: https://android-developers.googleblog.com/2017/06/reduce-friction-with-new-location-apis.html