-
Notifications
You must be signed in to change notification settings - Fork 982
/
SampleCustomMyLocation.java
117 lines (97 loc) · 3.48 KB
/
SampleCustomMyLocation.java
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package org.osmdroid.samplefragments.location;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import org.osmdroid.samplefragments.BaseSampleFragment;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.overlay.Marker;
import androidx.core.app.ActivityCompat;
/**
* See https://github.com/osmdroid/osmdroid/issues/815
* created on 12/21/2017.
*
* @author Alex O'Ree
*/
public class SampleCustomMyLocation extends BaseSampleFragment implements LocationListener {
@Override
public String getSampleTitle() {
return "Custom My Location Overlay";
}
LocationManager mgr;
Marker myLocation;
boolean added = false;
boolean followme = true;
@Override
public void addOverlays() {
super.addOverlays();
myLocation = new Marker(mMapView);
myLocation.setIcon(getResources().getDrawable(org.osmdroid.R.drawable.icon));
myLocation.setImage(getResources().getDrawable(org.osmdroid.R.drawable.icon));
}
public void onResume() {
super.onResume();
mgr = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), 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;
}
try {
mgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0L, 0f, this);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void onPause() {
super.onPause();
if (mgr != null)
try {
mgr.removeUpdates(this);
mgr = null;
} catch (Exception ex) {
ex.printStackTrace();
}
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy");
if (mgr != null)
try {
mgr.removeUpdates(this);
mgr = null;
} catch (Exception ex) {
ex.printStackTrace();
}
}
@Override
public void onLocationChanged(Location location) {
myLocation.setPosition(new GeoPoint(location.getLatitude(), location.getLongitude()));
if (!added) {
mMapView.getOverlayManager().add(myLocation);
added = true;
}
if (followme) {
mMapView.getController().animateTo(myLocation.getPosition());
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}