wanted add in this method
private void getCurrentLocation() {
//Creating a location object
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.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.
return;
}
Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
if (location != null) {
//Getting longitude and latitude
longitude = location.getLongitude();
latitude = location.getLatitude();
//moving the map to location
moveMap();
}
}
Asked
Active
Viewed 244 times
-3

akhil mb
- 3
- 2
-
Welcome, you can take a look to [ask]. And try to be more specific, you want the GPS location using the google service. Google maps API provide the map activity only (except if I am wrong ? ) – AxelH Oct 25 '16 at 06:25
-
Also, to answer you from what we have : you simply use the LocationListener to ask automaticly an event every 30sec, one of the parameter of the registering methods take the time between to position. This will be more power efficient. This will not use the Google Services, this will use only the LocationListener you asked but this will be more than enought I believe – AxelH Oct 25 '16 at 06:32
2 Answers
0
If you want just to run this method in intervals, try java class Timer
int delay = 5000; // delay for 5 sec.
int interval = 30000; // iterate every 30 sec.
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
getCurrentLocation // Your method from above ..
}
}, delay, interval);

koperko
- 2,447
- 13
- 19
-
but if i start moving it is showing same longitude and latitudes values .....pls help @Koperko – akhil mb Oct 26 '16 at 05:51
0
LoactionListener mLocationListener;
double lat,lng;
inside your OnCreateMethod :
mLocationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
lat = location.getLatitude();
lng = location.getLongitude();
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
};
if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
try {
if (ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TOD
// 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.
// Not implementing Permission Request Measures as User Should not know he is being tracked
}
Criteria c = new Criteria();
c.setAccuracy(Criteria.ACCURACY_FINE);
c.setAltitudeRequired(false);
c.setBearingRequired(false);
c.setSpeedRequired(false);
// mLocationManager.requestSingleUpdate(c, mLocationListener, getMainLooper());
mLocationManager.requestLocationUpdates(REFRESH_TIME, REFRESH_DISTANCE, c, mLocationListener, getMainLooper()); //Check if this can give us location updates on change in locations
} catch (Exception e) {
Log.d(TAG, " Exception e" + e.toString());
}
}else{
Log.d(TAG,"Location permission are disabled in service");
}
now using the Koperko solution
int delay = 5000; // delay for 5 sec.
int interval = 30000; // iterate every 30 sec.
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
mFragment.getMapAsync(Context); // The object of your own map support fragment
}
}, delay, interval);
inYour onMapReady method
use
LatLng mLatLng=new LatLng(lat,lng);
Marker marker = mGoogleMap.addMarker(new MarkerOptions().position(mLatLng));

Ak9637
- 990
- 6
- 12