Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法在谷歌地图活动中移动。它总是返回到我当前的位置_Java_Android_Google Maps_Android Activity - Fatal编程技术网

Java 无法在谷歌地图活动中移动。它总是返回到我当前的位置

Java 无法在谷歌地图活动中移动。它总是返回到我当前的位置,java,android,google-maps,android-activity,Java,Android,Google Maps,Android Activity,我正在开发包含谷歌地图活动的应用程序 我的问题是: 当我试图在地图上移动或放大缩小时,它会立即返回到我当前的位置。此外,当我搜索位置时,新标记会添加到搜索位置,但会立即返回到当前位置 我寻找解决办法,但一无所获。。 我希望有人能弄明白 这是我的谷歌地图活动: public class MapActivity extends FragmentActivity implements OnMapReadyCallback { private GoogleMap mMap; publi

我正在开发包含谷歌地图活动的应用程序

我的问题是: 当我试图在地图上移动或放大缩小时,它会立即返回到我当前的位置。此外,当我搜索位置时,新标记会添加到搜索位置,但会立即返回到当前位置

我寻找解决办法,但一无所获。。 我希望有人能弄明白

这是我的谷歌地图活动:

public class MapActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    public LocationManager locationManager;
    public LocationListener locationListener;
    public DatabaseReference mDatabase;
    private FirebaseAuth mAuth;
    private ArrayList<User> userArrayList = new ArrayList<>();
    User useri;

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == 1) {

            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                    {
                        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
                    }
                }
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(map);
        mapFragment.getMapAsync(this);
        mDatabase = FirebaseDatabase.getInstance().getReference();
        mAuth = FirebaseAuth.getInstance();

    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {

                LatLng userLocation = new LatLng(location.getLatitude(), location.getLongitude());

                mMap.addMarker(new MarkerOptions().position(userLocation).title("המיקום שלי")
                        .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_action_marker1)));
                mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 11));

            }

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {

            }

            @Override
            public void onProviderEnabled(String s) {

            }

            @Override
            public void onProviderDisabled(String s) {

            }
        };

        if (Build.VERSION.SDK_INT < 23) {

            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
            mMap.setMyLocationEnabled(true);

        } else {

            if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);

            }
            else {

                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
                //mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                //MapStyleOptions style = MapStyleOptions.loadRawResourceStyle(this, R.raw.style);
                //mMap.setMapStyle(style);
                mMap.setMyLocationEnabled(true);
                final Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
                final LatLng userLocation = new LatLng(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude());
                mDatabase.child("Users").child(mAuth.getCurrentUser().getUid()).child("lat").setValue(lastKnownLocation.getLatitude());
                mDatabase.child("Users").child(mAuth.getCurrentUser().getUid()).child("lng").setValue(lastKnownLocation.getLongitude());
                mMap.clear();
                mMap.addMarker(new MarkerOptions().position(userLocation).title("המיקום שלי")
                        .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_action_marker1)));
                mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 11));

                showUsersOnMap();

            }
        }
    }


    // Search for location and show it on the map
    public void onClick(View view) {

        if(view.getId() == R.id.searchLocationBtn){
            EditText searchBoxLocation = (EditText) findViewById(R.id.searchBoxLocation);
            String location = searchBoxLocation.getText().toString();
            List<Address> addressList = null;
            MarkerOptions markerOptions = new MarkerOptions();
            if( ! location.equals("")){
                Geocoder geocoder = new Geocoder(this);
                try {
                    addressList = geocoder.getFromLocationName(location, 1);
                } catch (IOException e) {
                    e.printStackTrace();
                }

                for (int i = 0 ; i < addressList.size(); i++){
                    Address myAddress = addressList.get(i);
                    LatLng latLng = new LatLng(myAddress.getLatitude(), myAddress.getLongitude());
                    markerOptions.position(latLng);
                    mMap.clear();
                    mMap.addMarker(markerOptions);
                    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,11));
                }
            }
        }
    }

    // Function to show all the users on the map
    public void showUsersOnMap(){
        mDatabase.child("Users").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot ds: dataSnapshot.getChildren()){
                    User user = ds.getValue(User.class);
                    userArrayList.add(user);
                }

                for (int i = 0; i < userArrayList.size(); i++) {
                    useri = userArrayList.get(i);
                    if (useri.getLat() != 0  && useri.getLng() != 0) {
                        MarkerOptions markerOptions = new MarkerOptions();
                        LatLng userlatLng = new LatLng(useri.getLat(), useri.getLng());
                        markerOptions.position(userlatLng);
                        mMap.addMarker(new MarkerOptions().position(userlatLng).title(useri.getName()).snippet(useri.getPhone())
                                .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_action_marker2)));
                        //mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng2,10));

                    }
                    else Toast.makeText(getApplicationContext(),"ישנה בעיה. אנא נסה להתחבר למפה שוב",Toast.LENGTH_LONG).show();
                }

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }
}
公共类MapActivity扩展了FragmentActivity在MapReadyCallback上的实现{
私有谷歌地图;
公共场所经理;
公共场所监听器场所监听器;
公共数据库参考数据库;
私人消防队;
private ArrayList userArrayList=new ArrayList();
用户useri;
@凌驾
public void onRequestPermissionsResult(int-requestCode,@NonNull-String[]permissions,@NonNull-int[]grantResults){
super.onRequestPermissionsResult(请求代码、权限、授权结果);
if(requestCode==1){
if(grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION\u已授予){
if(ContextCompat.checkSelfPermission(此,Manifest.permission.ACCESS\u FINE\u位置)==PackageManager.permission\u已授予){
{
locationManager.RequestLocationUpdate(locationManager.GPS\提供程序,0,0,locationListener);
}
}
}
}
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
//获取SupportMapFragment,并在地图准备好使用时收到通知。
SupportMapFragment mapFragment=(SupportMapFragment)getSupportFragmentManager()
.findFragmentById(地图);
getMapAsync(这个);
mDatabase=FirebaseDatabase.getInstance().getReference();
mAuth=FirebaseAuth.getInstance();
}
@凌驾
4月1日公开作废(谷歌地图谷歌地图){
mMap=谷歌地图;
locationManager=(locationManager)this.getSystemService(Context.LOCATION\u服务);
locationListener=新locationListener(){
@凌驾
已更改位置上的公共无效(位置){
LatLng userLocation=新LatLng(location.getLatitude(),location.getLongitude());
mMap.addMarker(新的MarkerOptions().position(userLocation).title
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_action_marker1));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation,11));
}
@凌驾
状态已更改的公共void(字符串s、int i、Bundle){
}
@凌驾
已提供已启用的公共void(字符串s){
}
@凌驾
公共无效onProviderDisabled(字符串s){
}
};
如果(Build.VERSION.SDK_INT<23){
locationManager.RequestLocationUpdate(locationManager.GPS\提供程序,0,0,locationListener);
mMap.setMyLocationEnabled(真);
}否则{
if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u FINE\u LOCATION)!=PackageManager.permission\u已授予){
ActivityCompat.requestPermissions(这个新字符串[]{Manifest.permission.ACCESS\u FINE\u LOCATION},1);
}
否则{
locationManager.RequestLocationUpdate(locationManager.GPS\提供程序,0,0,locationListener);
//mMap.setMapType(GoogleMap.MAP\u TYPE\u NORMAL);
//MapStyleOptions style=MapStyleOptions.loadRawResourceStyle(this,R.raw.style);
//mMap.setMapStyle(样式);
mMap.setMyLocationEnabled(真);
最终位置lastKnownLocation=locationManager.getLastKnownLocation(locationManager.PASSIVE\u提供程序);
最终LatLng userLocation=新LatLng(lastKnownLocation.getLatitude(),lastKnownLocation.getLongitude());
mDatabase.child(“用户”).child(mAuth.getCurrentUser().getUid()).child(“lat”).setValue(lastKnownLocation.getLatitude());
mDatabase.child(“用户”).child(mAuth.getCurrentUser().getUid()).child(“lng”).setValue(lastKnownLocation.getLength());
mMap.clear();
mMap.addMarker(新的MarkerOptions().position(userLocation).title
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_action_marker1));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation,11));
showUsersOnMap();
}
}
}
//搜索位置并在地图上显示
公共void onClick(视图){
if(view.getId()==R.id.searchLocationBtn){
EditText searchBoxLocation=(EditText)findViewById(R.id.searchBoxLocation);
字符串位置=searchBoxLocation.getText().toString();
List addressList=null;
MarkerOptions MarkerOptions=新MarkerOptions();
如果(!location.equals(“”){
Geocoder Geocoder=新的Geocoder(本);
试一试{
addressList=geocoder.getFromLocationName(位置,1);
}捕获(IOE异常){
e、 printStackTrace();
}
对于(int i=0;i for (int i = 0 ; i < addressList.size(); i++){
                    Address myAddress = addressList.get(i);
                    LatLng latLng = new LatLng(myAddress.getLatitude(), myAddress.getLongitude());
                    markerOptions.position(latLng);
                    mMap.clear();
                    mMap.addMarker(markerOptions);
                    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,11));
                }