当我在androidstudio中打开片段时,如何在googleMap中只显示一次当前用户的位置?我使用这个代码,但相机会不断移动到用户当前的位置。当用户移动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动画关闭到当前位置?
暂无答案!
目前还没有任何答案,快来回答吧!