RSS-Feed

David Hadizadeh

E-Mail

Xing-Profil

LinkedIn

Google+

Impressum

 




GPS Technology

10.05.2015

If you want to combine indoor positioning with GPS positioning (outdoor) you can use the indoor-positioning library as well.
Here you can see an implementation of GPS technology, which will notify if a persisted position is in a defined range to the current GPS position.
It also checks if the GPS signal is new enough.
You can add this technology to the positioning manager with a single line of code:

1
positionManager.addTechnology(new GpsTechnology(this, "GPS", 5, 3000, 1));

3. parameter: radius of each persisted position (in meters)
4. parameter: minimum time interval between location updates (in milliseconds)
5. parameter: minimum distance between location updates (in meters)

Add the following class to your technology package:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import de.hadizadeh.positioning.controller.Technology;
import de.hadizadeh.positioning.model.PositionInformation;
import de.hadizadeh.positioning.model.SignalInformation;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
public class GpsTechnology extends Technology implements LocationListener {
    private static final String LAT_TEXT = "lat";
    private static final String LNG_TEXT = "lng";
    private static final int GPS_TIME_DELTA = 10000;
    private LocationManager locationManager;
    private Location location;
    private float maxDistance;
 
    public GpsTechnology(Context context, String name, float maxDistance, int minUpdateInterval, float minDistanceChange) {
        super(name);
        this.maxDistance = maxDistance;
        locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minUpdateInterval, minDistanceChange, this);
    }
 
    @Override
    public Map<String, SignalInformation> getSignalData() {
        Map<String, SignalInformation> signalData = new HashMap<String, SignalInformation>();
        if (location != null) {
            signalData.put(LAT_TEXT, new SignalInformation(location.getLatitude()));
            signalData.put(LNG_TEXT, new SignalInformation(location.getLongitude()));
        }
        return signalData;
    }
 
    @Override
    public void onLocationChanged(Location location) {
        this.location = location;
    }
 
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        this.location = null;
    }
 
    @Override
    public void onProviderEnabled(String provider) {
 
    }
 
    @Override
    public void onProviderDisabled(String provider) {
        this.location = null;
    }
 
    private double calculateDistance(Location location, double lat, double lng) {
        Location compareLocation = new Location("");
        compareLocation.setLatitude(lat);
        compareLocation.setLongitude(lng);
        return location.distanceTo(compareLocation);
    }
 
    @Override
    public Map<PositionInformation, Double> match(List<PositionInformation> persistedPositions) {
        // Remove current location data, if signal is to old
        Location lastLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (lastLocation == null) {
            location = null;
        } else if (lastLocation.getTime() + GPS_TIME_DELTA < System.currentTimeMillis()) {
            location = null;
        }
 
        if (location != null) {
            Map<PositionInformation, Double> nearestPositions = new HashMap<PositionInformation, Double>();
            for (PositionInformation positionInformation : persistedPositions) {
                Map<String, SignalInformation> signalInformation = positionInformation.getSignalInformation();
                double distance = calculateDistance(location, signalInformation.get(LAT_TEXT).getStrength(), signalInformation.get(LNG_TEXT)
                        .getStrength());
                if (distance <= maxDistance) {
                    nearestPositions.put(positionInformation, distance);
                }
            }
            return nearestPositions;
        } else {
            return null;
        }
    }
}

Kategorie: Indoor-Positioning Addons |


Impressum