-Location Updates while mobile

This commit is contained in:
Armani
2017-12-08 23:12:34 +01:00
parent c0ab174962
commit 93bcb1ea30
2 changed files with 132 additions and 45 deletions
@@ -20,22 +20,28 @@ public class GPSTracker implements LocationListener{
boolean isNetworkEnabled=false;
boolean isGPSTrackingEnabled=false;
private Context context;
Location location;
public static double latitude;
public static double longitude;
private LocationListener locationListener;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
private static final long MIN_TIME_BW_UPDATES = 30000; // 1 minute
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES =5 ;
private static final long MIN_TIME_BW_UPDATES = 2000;
protected LocationManager locationManager;
private String provider_info;
public GPSTracker(Context context){
public GPSTracker(Context context,LocationListener locationListener){
this.context=context;
this.locationListener=locationListener;
getLocation();
}
@@ -43,19 +49,19 @@ public class GPSTracker implements LocationListener{
public void getLocation(){
try {
locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
//getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
//getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
//getting network status
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
//getting network status
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
// Try to get location if you GPS Service is enabled
if (isGPSEnabled) {
this.isGPSTrackingEnabled = true;
// Try to get location if you GPS Service is enabled
if (isGPSEnabled) {
this.isGPSTrackingEnabled = true;
Log.d(TAG, "Application use GPS Service");
Log.d(TAG, "Application use GPS Service");
/*
* This provider determines location using
@@ -63,39 +69,38 @@ public class GPSTracker implements LocationListener{
* a location fix.
*/
provider_info = LocationManager.GPS_PROVIDER;
provider_info = LocationManager.GPS_PROVIDER;
} else if (isNetworkEnabled) { // Try to get location if you Network Service is enabled
this.isGPSTrackingEnabled = true;
} else if (isNetworkEnabled) { // Try to get location if you Network Service is enabled
this.isGPSTrackingEnabled = true;
Log.d(TAG, "Application use Network State to get GPS coordinates");
Log.d(TAG, "Application use Network State to get GPS coordinates");
/*
* This provider determines location based on
* availability of cell tower and WiFi access points. Results are retrieved
* by means of a network lookup.
*/
provider_info = LocationManager.NETWORK_PROVIDER;
provider_info = LocationManager.NETWORK_PROVIDER;
}
}
// Application can use GPS or Network Provider
if (!provider_info.isEmpty()) {
locationManager.requestLocationUpdates(
provider_info,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES,
this
);
// Application can use GPS or Network Provider
if (!provider_info.isEmpty()) {
locationManager.requestLocationUpdates(
provider_info,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES,
locationListener
);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(provider_info);
updateGPSCoordinates();
}
}
}
catch (Exception e)
{
if (locationManager != null) {
location = locationManager.getLastKnownLocation(provider_info);
updateGPSCoordinates();
}
}
}
catch (Exception e){
//e.printStackTrace();
Log.e(TAG, "Impossible to connect to LocationManager", e);
}
@@ -139,6 +144,10 @@ public class GPSTracker implements LocationListener{
@Override
public void onLocationChanged(Location location) {
}
@Override
@@ -148,12 +157,12 @@ public class GPSTracker implements LocationListener{
@Override
public void onProviderEnabled(String provider) {
System.out.println("Provider has been Enabled");
}
@Override
public void onProviderDisabled(String provider) {
System.out.println("Provider has been Disabled");
}
}
@@ -2,6 +2,8 @@ package com.maptology.android.app;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
@@ -31,6 +33,10 @@ import com.locoslab.capi.android.leafless.vector.VectorLayerOptions;
import java.util.ArrayList;
import java.util.List;
import static android.content.ContentValues.TAG;
import static com.maptology.android.app.GPSTracker.latitude;
import static com.maptology.android.app.GPSTracker.longitude;
/**
* A fragment that displays a map.
@@ -85,12 +91,25 @@ public class MapFragment extends Fragment {
*/
private double angle;
private GPSTracker tracker;
private CircleOptions circleOptions;
private Circle circle;
private LocationListener listener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// make sure that the fragment is not recreated
setRetainInstance(true);
GPSTracker gpsTracker= new GPSTracker(getContext());
if(tracker==null){
makeLocationListener();
tracker = new GPSTracker(getContext(),listener);
}
}
/**
@@ -144,21 +163,80 @@ public class MapFragment extends Fragment {
});
if(center!=null){
map.setCenter(GPSTracker.longitude,GPSTracker.latitude);
map.setCenter(longitude, latitude);
}
CircleOptions op =new CircleOptions();
Circle c =new Circle(op);
Coordinate coordinate= new Coordinate(GPSTracker.longitude,GPSTracker.latitude);
System.out.println("#######Long "+ GPSTracker.longitude + " ########Lat "+ GPSTracker.latitude) ;
c.setRadius(10, CircleUnit.PIXEL);
c.setCenter(coordinate);
map.addShape(c);
circleOptions =new CircleOptions();
circle=new Circle(circleOptions);
//Coordinate coordinate= new Coordinate(longitude, latitude);
System.out.println("#######Long "+ longitude + " ########Lat "+ latitude) ;
center.setLatitude(GPSTracker.latitude);
center.setLongitude(GPSTracker.longitude);
circle.setRadius(10, CircleUnit.PIXEL);
circle.setCenter(center);
map.addShape(circle);
return view;
}
@Override
public void onResume() {
super.onResume();
if (tracker != null) {
tracker=this.tracker;
}else {
makeLocationListener();
tracker=new GPSTracker(getContext(),listener);
}
}
public void makeLocationListener(){
listener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Log.e(TAG, "Location is changed!");
Log.e(TAG, "Last known Location: " + " Lat: " + latitude + " Long: " + longitude);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
if (center != null) {
map.removeShape(circle);
center.setLatitude(latitude);
center.setLongitude(longitude);
circle.setRadius(10, CircleUnit.PIXEL);
circle.setCenter(center);
map.addShape(circle);
map.setCenter(longitude, latitude);
}
}
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
};
//tracker = new GPSTracker(getContext(),listener);
}
@Override