/
SampleFollowMe.java
181 lines (149 loc) · 5.95 KB
/
SampleFollowMe.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package org.osmdroid.samplefragments.location;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import org.osmdroid.R;
import org.osmdroid.samplefragments.BaseSampleFragment;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.overlay.ScaleBarOverlay;
import org.osmdroid.views.overlay.compass.CompassOverlay;
import org.osmdroid.views.overlay.compass.InternalCompassOrientationProvider;
import org.osmdroid.views.overlay.gestures.RotationGestureOverlay;
import org.osmdroid.views.overlay.mylocation.GpsMyLocationProvider;
import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay;
/**
* Based off the submission from https://github.com/2ndGAB
*
* Source: https://github.com/2ndGAB/OSMCenterToMyPosition
* Created by alex on 6/6/16.
*/
public class SampleFollowMe extends BaseSampleFragment implements LocationListener {
private MyLocationNewOverlay mLocationOverlay;
private CompassOverlay mCompassOverlay;
private ScaleBarOverlay mScaleBarOverlay;
private RotationGestureOverlay mRotationGestureOverlay;
protected ImageButton btCenterMap;
protected ImageButton btFollowMe;
private LocationManager lm;
private Location currentLocation = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.sample_followme, null);
mMapView = v.findViewById(R.id.mapview);
return v;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
//super.onViewCreated(view, savedInstanceState);
final Context context = this.getActivity();
final DisplayMetrics dm = context.getResources().getDisplayMetrics();
this.mCompassOverlay = new CompassOverlay(context, new InternalCompassOrientationProvider(context),
mMapView);
this.mLocationOverlay = new MyLocationNewOverlay(new GpsMyLocationProvider(context),
mMapView);
mScaleBarOverlay = new ScaleBarOverlay(mMapView);
mScaleBarOverlay.setCentred(true);
mScaleBarOverlay.setScaleBarOffset(dm.widthPixels / 2, 10);
mRotationGestureOverlay = new RotationGestureOverlay(mMapView);
mRotationGestureOverlay.setEnabled(true);
mMapView.getController().setZoom(15);
mMapView.setTilesScaledToDpi(true);
mMapView.setMultiTouchControls(true);
mMapView.setFlingEnabled(true);
mMapView.getOverlays().add(this.mLocationOverlay);
mMapView.getOverlays().add(this.mCompassOverlay);
mMapView.getOverlays().add(this.mScaleBarOverlay);
mLocationOverlay.enableMyLocation();
mLocationOverlay.enableFollowLocation();
mLocationOverlay.setOptionsMenuEnabled(true);
mCompassOverlay.enableCompass();
btCenterMap = view.findViewById(R.id.ic_center_map);
btCenterMap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i(TAG, "centerMap clicked ");
if (currentLocation != null) {
GeoPoint myPosition = new GeoPoint(currentLocation.getLatitude(), currentLocation.getLongitude());
mMapView.getController().animateTo(myPosition);
}
}
});
btFollowMe = view.findViewById(R.id.ic_follow_me);
btFollowMe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i(TAG, "btFollowMe clicked ");
if (!mLocationOverlay.isFollowLocationEnabled()) {
mLocationOverlay.enableFollowLocation();
btFollowMe.setImageResource(R.drawable.ic_follow_me_on);
} else {
mLocationOverlay.disableFollowLocation();
btFollowMe.setImageResource(R.drawable.ic_follow_me);
}
}
});
}
@Override
public void onPause() {
super.onPause();
try{
lm.removeUpdates(this);
}catch (Exception ex){}
mCompassOverlay.disableCompass();
mLocationOverlay.disableFollowLocation();
mLocationOverlay.disableMyLocation();
mScaleBarOverlay.enableScaleBar();
}
@Override
public void onResume(){
super.onResume();
lm = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
try{
//this fails on AVD 19s, even with the appcompat check, says no provided named gps is available
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0l,0f,this);
}catch (Exception ex){}
try{
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0l,0f,this);
}catch (Exception ex){}
mLocationOverlay.enableFollowLocation();
mLocationOverlay.enableMyLocation();
mScaleBarOverlay.disableScaleBar();
}
@Override
public String getSampleTitle() {
return "Follow Me";
}
@Override
public void onLocationChanged(Location location) {
currentLocation=location;
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onDestroyView(){
super.onDestroyView();
lm=null;
currentLocation=null;
mLocationOverlay=null;
mCompassOverlay=null;
mScaleBarOverlay=null;
mRotationGestureOverlay=null;
btCenterMap=null;
btFollowMe=null;
}
}