1717
1818package th .or .nectec .marlo ;
1919
20- import android .Manifest ;
20+ import android .content . Context ;
2121import android .location .Location ;
2222import android .os .Bundle ;
2323import android .support .annotation .IdRes ;
3939import th .or .nectec .marlo .option .DefaultMarkerOptionFactory ;
4040import th .or .nectec .marlo .option .MarkerOptionFactory ;
4141
42+ import static android .Manifest .permission .ACCESS_COARSE_LOCATION ;
43+ import static android .Manifest .permission .ACCESS_FINE_LOCATION ;
44+ import static android .content .pm .PackageManager .PERMISSION_GRANTED ;
45+ import static android .support .v4 .content .ContextCompat .checkSelfPermission ;
46+
4247/**
4348 * Base fragment of Marlo Project, Contain common process such as Setup map, Add common ui component
4449 * and Enable my location feature
@@ -47,6 +52,12 @@ public abstract class MarloFragment extends SupportMapFragment implements OnMapR
4752
4853 private static final String TAG = "MarloFragment" ;
4954
55+ /**
56+ * Request code on for request location setting when can't get last knowLocation
57+ * after call moveToMyLocation. Activity can handle result of user action by onActivityResult()
58+ */
59+ public static final int REQUEST_LOCATION_SETTINGS = 10512 ;
60+
5061 protected MarkerOptionFactory markOptFactory = new DefaultMarkerOptionFactory ();
5162 protected GoogleMap googleMap ;
5263
@@ -96,19 +107,37 @@ public void onStop() {
96107 }
97108
98109 /**
99- * enable default My Location button
110+ * enable myLocation feature to use moveToMyLocation.
111+ * If need built-in myLocationButton use enableMyLocationButton() instead.
100112 *
101113 * @throws SecurityException if permission not granted
102114 */
103115 @ RequiresPermission (anyOf = {
104- Manifest . permission . ACCESS_COARSE_LOCATION ,
105- Manifest . permission . ACCESS_FINE_LOCATION })
106- public void enableMyLocationButton () throws SecurityException {
116+ ACCESS_COARSE_LOCATION ,
117+ ACCESS_FINE_LOCATION })
118+ public void enableMyLocation () throws SecurityException {
107119 myLocationEnable = true ;
108120 if (googleMap != null ) {
109121 googleMap .setMyLocationEnabled (true );
110122 }
111123 locationService .connect ();
124+ }
125+
126+ /**
127+ * enable default My Location button with myLocation feature
128+ *
129+ * @throws SecurityException if permission not granted
130+ */
131+ @ RequiresPermission (anyOf = {
132+ ACCESS_COARSE_LOCATION ,
133+ ACCESS_FINE_LOCATION })
134+ public void enableMyLocationButton () {
135+ Context context = getContext ();
136+ if (checkSelfPermission (context , ACCESS_FINE_LOCATION ) != PERMISSION_GRANTED
137+ && checkSelfPermission (context , ACCESS_COARSE_LOCATION ) != PERMISSION_GRANTED ) {
138+ throw new IllegalStateException ("Location permission must be granted" );
139+ }
140+ enableMyLocation ();
112141 updateMyLocationVisibility ();
113142 }
114143
@@ -168,11 +197,21 @@ public void onClick(View view) {
168197 */
169198 public abstract void mark (LatLng markPoint );
170199
171- private void moveToMyLocation () {
200+ /**
201+ * animate to current location. if can't get last know location it will automatically prompt
202+ * user to change setting
203+ *
204+ * @exception IllegalStateException if call before enableMyLocation() or enableMyLocationButton()
205+ */
206+ public void moveToMyLocation () {
207+ if (!myLocationEnable )
208+ throw new IllegalStateException ("Must enable myLocation feature before" );
172209 Location lastKnowLocation = locationService .getLastKnowLocation ();
173210 if (lastKnowLocation != null ) {
174211 LatLng latLng = new LatLng (lastKnowLocation .getLatitude (), lastKnowLocation .getLongitude ());
175212 googleMap .animateCamera (CameraUpdateFactory .newLatLngZoom (latLng , 16 ), 1000 , null );
213+ } else {
214+ locationService .requestLocationSetting (getActivity (), REQUEST_LOCATION_SETTINGS );
176215 }
177216 }
178217
0 commit comments