Skip to content
This repository was archived by the owner on Oct 26, 2025. It is now read-only.

Commit 29d91bf

Browse files
committed
moveToMyLocation() public and not bind with build-in MyLocationButton also prompt user to change setting if needed
1 parent 5bd1e20 commit 29d91bf

File tree

2 files changed

+92
-6
lines changed

2 files changed

+92
-6
lines changed

marlo/src/main/java/th/or/nectec/marlo/MarloFragment.java

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
package th.or.nectec.marlo;
1919

20-
import android.Manifest;
20+
import android.content.Context;
2121
import android.location.Location;
2222
import android.os.Bundle;
2323
import android.support.annotation.IdRes;
@@ -39,6 +39,11 @@
3939
import th.or.nectec.marlo.option.DefaultMarkerOptionFactory;
4040
import 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

marlo/src/main/java/th/or/nectec/marlo/PlayLocationService.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717

1818
package th.or.nectec.marlo;
1919

20+
import android.app.Activity;
2021
import android.content.Context;
22+
import android.content.IntentSender;
2123
import android.location.Location;
2224
import android.os.Bundle;
2325
import android.support.annotation.NonNull;
@@ -28,7 +30,15 @@
2830
import com.google.android.gms.common.api.GoogleApiClient;
2931
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
3032
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
33+
import com.google.android.gms.common.api.PendingResult;
34+
import com.google.android.gms.common.api.ResultCallback;
35+
import com.google.android.gms.common.api.Status;
36+
import com.google.android.gms.location.LocationRequest;
3137
import com.google.android.gms.location.LocationServices;
38+
import com.google.android.gms.location.LocationSettingsRequest;
39+
import com.google.android.gms.location.LocationSettingsResult;
40+
import com.google.android.gms.location.LocationSettingsStates;
41+
import com.google.android.gms.location.LocationSettingsStatusCodes;
3242

3343
final class PlayLocationService implements OnConnectionFailedListener, ConnectionCallbacks {
3444

@@ -80,4 +90,41 @@ public void onConnectionSuspended(int i) {
8090
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
8191
Toast.makeText(context, "ไม่สามารถเชื่อมต่อ Google Play Services ได้", Toast.LENGTH_LONG).show();
8292
}
93+
94+
public void requestLocationSetting(final Activity activity,
95+
final LocationRequest request,
96+
final int requestCode) {
97+
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
98+
.addLocationRequest(request);
99+
PendingResult<LocationSettingsResult> result =
100+
LocationServices.SettingsApi.checkLocationSettings(locationApiClient,
101+
builder.build());
102+
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
103+
@Override
104+
public void onResult(@NonNull LocationSettingsResult result) {
105+
final Status status = result.getStatus();
106+
final LocationSettingsStates state = result.getLocationSettingsStates();
107+
switch (status.getStatusCode()) {
108+
case LocationSettingsStatusCodes.SUCCESS:
109+
break;
110+
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
111+
try {
112+
status.startResolutionForResult(activity, requestCode);
113+
} catch (IntentSender.SendIntentException error) {
114+
// Ignore the error.
115+
}
116+
break;
117+
}
118+
}
119+
});
120+
}
121+
122+
public void requestLocationSetting(final Activity activity, int requestCode) {
123+
LocationRequest defaultRequest = new LocationRequest();
124+
defaultRequest.setInterval(10000);
125+
defaultRequest.setFastestInterval(5000);
126+
defaultRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
127+
128+
requestLocationSetting(activity, defaultRequest, requestCode);
129+
}
83130
}

0 commit comments

Comments
 (0)