[英]how I can show current user location in Google Map only once, when I open the fragment?
当我在 Android Studio 中打开片段时,如何仅在 Google Map 中显示当前用户位置一次? 我使用此代码,但相机不断移动到用户当前位置。 当用户移动 map 时,它会自动移回当前位置。
public class MapsFragment extends Fragment implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener, OnMapReadyCallback {
private SharedPreferences sharedPref;
private GoogleMap gMap;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
private Location locationData;
private static final long TIME_INTERVAL_GET_LOCATION = 1000 * 5;
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 5000;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedPref = PreferenceManager.getDefaultSharedPreferences(getContext());
View rootView = inflater.inflate(R.layout.fragment_map, null);
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
TedPermission.with(getActivity())
.setPermissionListener(permissionlistener)
.setDeniedMessage("If you reject permission,you can not use this service\n\nPlease turn on permissions at [Setting] > [Permission]")
.setPermissions(Manifest.permission.ACCESS_FINE_LOCATION)
.check();
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
return rootView;
}
@Override
public void onResume() {
super.onResume();
}
PermissionListener permissionlistener = new PermissionListener() {
@Override
public void onPermissionGranted() {
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(TIME_INTERVAL_GET_LOCATION)
.setFastestInterval(TIME_INTERVAL_GET_LOCATION);
if (!mGoogleApiClient.isConnected()) {
mGoogleApiClient.connect();
locationChecker(mGoogleApiClient, getActivity());
}
}
@Override
public void onPermissionDenied(ArrayList<String> deniedPermissions) {
}
};
@Override
public void onConnected(@Nullable Bundle bundle) {
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
FusedLocationProviderClient mFusedLocationClient = LocationServices.getFusedLocationProviderClient(getActivity());
mFusedLocationClient.requestLocationUpdates(mLocationRequest, new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
locationData = locationResult.getLastLocation();
if (locationData != null) {
LatLng myLocation = new LatLng(locationData.getLatitude(), locationData.getLongitude());
gMap.clear();
gMap.addMarker(new MarkerOptions().position(myLocation).title("You are here!").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
CameraPosition cameraPosition = new CameraPosition.Builder().target(myLocation).zoom(14).build();
gMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
}
}, null);
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
//........
}
public void locationChecker(GoogleApiClient mGoogleApiClient, final Activity activity) {
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(30 * 1000);
locationRequest.setFastestInterval(5 * 1000);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
final LocationSettingsStates state = result.getLocationSettingsStates();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try {
status.startResolutionForResult(activity, 1000);
} catch (IntentSender.SendIntentException e) {
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
break;
}
}
});
}
@Override
public void onLocationChanged(Location location) {
{
}
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
@Override
public void onMapReady(GoogleMap googleMap) {
gMap = googleMap;
gMap.getUiSettings().setZoomControlsEnabled(false);
gMap.setBuildingsEnabled(false);
gMap.getUiSettings().setCompassEnabled(true);
gMap.getUiSettings().setCompassEnabled(true);
gMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
gMap.setIndoorEnabled(false);
gMap.getUiSettings().setRotateGesturesEnabled(true);
}
}
我应该怎么做才能解决这个问题? 如何将 map animation 关闭到当前位置?
尝试使用此 function 将您的相机设置为用户的第一个 position:
yourGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 8));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.