Java 如何阻止谷歌地图不断在我的位置重新定位

Java 如何阻止谷歌地图不断在我的位置重新定位,java,android,maps,Java,Android,Maps,我的问题与android中的谷歌地图有关。当在任何点移动相机时,它会立即返回到我的原始位置,我如何停止,你可以在下面找到我的代码 地图正常启动,然后由于某种原因,当我尝试导航地图时,它会将自己重新定位到原始位置 public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleMap.OnMapLongClickListener { private GoogleMap mMap

我的问题与android中的谷歌地图有关。当在任何点移动相机时,它会立即返回到我的原始位置,我如何停止,你可以在下面找到我的代码 地图正常启动,然后由于某种原因,当我尝试导航地图时,它会将自己重新定位到原始位置

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleMap.OnMapLongClickListener {

    private GoogleMap mMap;
    LocationManager locationmanager ;
    LocationListener locationlistener;


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        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);

               Location lastKnownLocation = locationmanager.getLastKnownLocation(locationmanager.GPS_PROVIDER);

                centerMpOnLocation(lastKnownLocation, "your Location ");


            }
        }
    }

    public void centerMpOnLocation (Location location, String title){
            if (location != null){
            LatLng userLocation = new LatLng(location.getLatitude(), location.getLongitude());

            mMap.clear();
            if (title != "your Location") {
                mMap.addMarker(new MarkerOptions().position(userLocation).title(title));
            }
               mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 15));

            }else {
                Toast.makeText(this, "Wait till we get your Location", Toast.LENGTH_SHORT).show();
            }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // 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);
    }


    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        mMap.setOnMapLongClickListener(this);

        Intent intent= getIntent();

        if (intent.getIntExtra("placeNumber", 0) == 0){

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

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

                    centerMpOnLocation(location, "your Location");



                }

                @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);
            }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);

                    Location lastKnownLocation = locationmanager.getLastKnownLocation(locationmanager.GPS_PROVIDER);

                    centerMpOnLocation(lastKnownLocation, "your Location");
                }

            }
        }else {

            Location placeLocation = new Location(locationmanager.GPS_PROVIDER);

            placeLocation.setLatitude(MainActivity.locations.get(intent.getIntExtra("placeNumber", 0)).latitude);
            placeLocation.setLongitude(MainActivity.locations.get(intent.getIntExtra("placeNumber", 0)).longitude);

            centerMpOnLocation(placeLocation, MainActivity.memorablePlaces.get(intent.getIntExtra("placeNumber", 0)));

        }


    }

    @Override
    public void onMapLongClick(LatLng latLng) {
        Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
       String address = "" ;
        try {
            List <Address> addresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1);

            if (addresses != null && addresses.size() > 0){
                if (addresses.get(0).getThoroughfare() != null){
                    if (addresses.get(0).getSubThoroughfare() != null){
                        address += addresses.get(0).getSubThoroughfare() + " ";
                    }
                    address += addresses.get(0).getThoroughfare();
                }
            }else if( address == "") { // عشان لو ماعرفتش اطلع عنوان هاحتاج اطلع تاريخ ووقت حاجه بديله يعنى

                SimpleDateFormat sdf = new SimpleDateFormat("HH:mm yyyy-MM-dd");

                address = sdf.format(new Date());

            }
        } catch (IOException e) {

            e.printStackTrace();
        }

        mMap.addMarker(new MarkerOptions().position(latLng).title(address));

        MainActivity.memorablePlaces.add(address);
        MainActivity.locations.add(latLng);
        MainActivity.arrayAdapter.notifyDataSetChanged();

        Toast.makeText(this, "Location Saved", Toast.LENGTH_SHORT).show();
    }
}`
公共类MapsActivity扩展了FragmentActivity在Google Map.OnMapLongClickListener上实现的FragmentActivity{
私有谷歌地图;
地点经理地点经理;
LocationListener LocationListener;
@凌驾
public void onRequestPermissionsResult(int-requestCode,@NonNull-String[]permissions,@NonNull-int[]grantResults){
super.onRequestPermissionsResult(请求代码、权限、授权结果);
如果(grantResults.length>0&&grantResults[0]==已授予PackageManager.PERMISSION){
if(ContextCompat.checkSelfPermission(此,Manifest.permission.ACCESS\u FINE\u位置)==PackageManager.permission\u已授予){
locationmanager.RequestLocationUpdate(locationmanager.GPS\提供程序,0,0,locationlistener);
Location lastKnownLocation=locationmanager.getLastKnownLocation(locationmanager.GPS\U提供程序);
centerMpOnLocation(lastKnownLocation,“您的位置”);
}
}
}
公共void中心位置(位置、字符串标题){
如果(位置!=null){
LatLng userLocation=新LatLng(location.getLatitude(),location.getLongitude());
mMap.clear();
如果(标题!=“您的位置”){
mMap.addMarker(新的MarkerOptions().position(userLocation).title(title));
}
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation,15));
}否则{
Toast.makeText(这是“等到我们找到你的位置”,Toast.LENGTH_SHORT.show();
}
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_映射);
//获取SupportMapFragment,并在地图准备好使用时收到通知。
SupportMapFragment mapFragment=(SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);
getMapAsync(这个);
}
/**
*一旦可用,就可以操纵贴图。
*当映射准备好使用时,将触发此回调。
*这是我们可以添加标记或线条、添加侦听器或移动摄影机的地方。在这种情况下,
*我们只是在澳大利亚悉尼附近加了一个标记。
*如果设备上未安装Google Play服务,系统将提示用户安装
*它位于SupportMapFragment内。此方法仅在用户
*已安装Google Play服务并返回应用程序。
*/
@凌驾
4月1日公开作废(谷歌地图谷歌地图){
mMap=谷歌地图;
mMap.setOnMapLongClickListener(这个);
Intent=getIntent();
if(intent.getIntExtra(“placeNumber”,0)==0){
locationmanager=(locationmanager)this.getSystemService(Context.LOCATION\u服务);
locationlistener=新locationlistener(){
@凌驾
已更改位置上的公共无效(位置){
centerMpOnLocation(位置,“您的位置”);
}
@凌驾
状态已更改的公共void(字符串s、int i、Bundle){
}
@凌驾
已提供已启用的公共void(字符串s){
}
@凌驾
公共无效onProviderDisabled(字符串s){
}
};
如果(Build.VERSION.SDK_INT<23){
locationmanager.RequestLocationUpdate(locationmanager.GPS\提供程序,0,0,locationlistener);
}否则{
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);
Location lastKnownLocation=locationmanager.getLastKnownLocation(locationmanager.GPS\U提供程序);
centerMpOnLocation(lastKnownLocation,“您的位置”);
}
}
}否则{
位置位置=新位置(locationmanager.GPS\U提供程序);
setLatitude(MainActivity.locations.get(intent.getIntExtra(“placeNumber”,0)).latitude);
setLongitude(MainActivity.locations.get(intent.getIntExtra(“placeNumber”,0)).longitude);
centerMpOnLocation(placeLocation,MainActivity.memorablePlaces.get(intent.getIntExtra(“placeNumber”,0));
}
}
@凌驾
在MaplongClick(LatLng LatLng)上的公共无效{
Geocoder Geocoder=新的Geocoder(getApplicationContext(),Locale.getDefault());
字符串地址=”;
试一试{
列表地址=geocoder.getFromLocation(latLng.latitude,latLng.longitude,1);
if(addresses!=null&&addresses.size()>0){
if(addresses.get(0.GetThroughure()!=null){
if(addresses.get(0.GetSubThroughure()!=null){
address+=addresses.get(0.GetSubThroway()+”;
}
address+=addresses.get(0.GetThroughway();
}
}此外,若若(地址==,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各第八条
SimpleDateFormat sdf=新SimpleDat
locationlistener = new LocationListener() {
                @Override
                public void onLocationChanged(Location location) {

                    centerMpOnLocation(location, "your Location");



                }
    ...
    }