[英]I cannot move/scroll google maps up/down/left or right as it move back tomy current location
I have this problem that I can't move from my current location marker point to see the rest of the map. 我有这个问题, 我无法从当前位置标记点移动到地图的其余部分。
For an example, let's say I am at Silicon Valley. 举个例子,假设我在硅谷。 And I can see the marker too. 我也可以看到标记。 Everything looks perfect so far. 到目前为止一切看起来都很完美 But when I try to navigate (up, down, left, right) the map, it again brings me back to my current location. 但是当我尝试导航(向上,向下,向左,向右)地图时,它再次将我带回到我当前的位置。
It is automatically and forcefully brings me back to my current location not allowing me to see other places around 它自动而有力地将我带回我当前的位置, 不允许我看到周围的其他地方
here is my code for the MapActivity. 这是我的MapActivity代码。
package com.example.jananathbanuka.sliitshuttle;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.firebase.geofire.GeoFire;
import com.firebase.geofire.GeoLocation;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class DriversMap extends FragmentActivity implements
OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {
private GoogleMap mMap;
GoogleApiClient googleApiClient;
Location lastLocation;
LocationRequest locationRequest;
Marker currentUserLocationMarker;
private static final int REQUEST_USER_LOCATION_CODE = 99;
private Button back;
private FirebaseAuth mAuth;
private FirebaseUser currentUser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
mAuth = FirebaseAuth.getInstance();
currentUser = mAuth.getCurrentUser();
back = (Button)findViewById(R.id.back);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
checkUserLocationPermission();
}
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), StudentProfile.class);
startActivity(intent);
finish();
}
});
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
}
}
public boolean checkUserLocationPermission(){
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if(ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)){
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_USER_LOCATION_CODE);
}else{
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_USER_LOCATION_CODE);
}
return false;
}else {
return true;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode){
case REQUEST_USER_LOCATION_CODE:
if(grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
if(googleApiClient == null){
buildGoogleApiClient();
}
mMap.setMyLocationEnabled(true);
}
}else{ //permission denied
Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
return;
}
}
@Override
public void onConnected(@Nullable Bundle bundle) {
locationRequest = new LocationRequest();
locationRequest.setInterval(1000);
locationRequest.setFastestInterval(1000);
locationRequest.setPriority(locationRequest.PRIORITY_HIGH_ACCURACY);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
@Override
public void onPointerCaptureChanged(boolean hasCapture) {
}
@Override
public void onLocationChanged(Location location) {
lastLocation = location;
if(currentUserLocationMarker != null){ //set to some other location
currentUserLocationMarker.remove();
}
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title("Shutter is Here");
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));
currentUserLocationMarker = mMap.addMarker(markerOptions);
float zoomLevel = 16.0f;
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoomLevel));
// mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// mMap.animateCamera(CameraUpdateFactory.zoomTo(15));
String driverID = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference driverDatBaseReference = FirebaseDatabase.getInstance()
.getReference().child("On the Move");
GeoFire geoFire = new GeoFire(driverDatBaseReference);
geoFire.setLocation(driverID, new GeoLocation(location.getLatitude(), location.getLongitude()), new GeoFire.CompletionListener() {
@Override
public void onComplete(String key, DatabaseError error) {
System.out.println("================ UPDATED!!!");
}
});
}
protected synchronized void buildGoogleApiClient() {
googleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
googleApiClient.connect();
}
@Override
protected void onStop() {
super.onStop();
disconnectDriver();
}
private void disconnectDriver() {
String driverID = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference driverDatBaseReference = FirebaseDatabase.getInstance()
.getReference().child("On the Move");
GeoFire geoFire = new GeoFire(driverDatBaseReference);
geoFire.removeLocation(driverID);
}
}
Can someone tell me by looking at my code, why is it happening? 有人可以通过查看我的代码告诉我,为什么会这样?
By adding the line locationRequest.setInterval(1000);
通过添加行locationRequest.setInterval(1000);
you are asking to be updated of the users' location every 1 second. 您要求每1秒更新一次用户的位置。
This results in your onLocationChanged(Location location)
method -where you call mMap.moveCamera
- being called every second. 这会导致您的onLocationChanged(Location location)
方法 - 您调用mMap.moveCamera
- 每秒调用一次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.