一段时间后,我的地图活动会刷新以指向初始位置,同时在地图上拖动

时间:2019-07-12 10:45:16

标签: android google-maps-markers

这是我的 AddressForCart 地图活动

public class AddressForCart extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    LocationManager locationManager;
    @SuppressLint("MissingPermission") //I have already asked for permission before opening this activity via intent. Dont worry about this

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_for_cart);

        // 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);


        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {
                @Override
                public void onLocationChanged(Location location) {
                    double latitude = location.getLatitude();
                    double longitude = location.getLongitude();
                    LatLng latLng = new LatLng(latitude,longitude);

                    displayOnMap(latLng,13.2f);
                }

                @Override
                public void onStatusChanged(String provider, int status, Bundle extras) {

                }

                @Override
                public void onProviderEnabled(String provider) {

                }

                @Override
                public void onProviderDisabled(String provider) {

                }
            });
        } else if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {
                @Override
                public void onLocationChanged(Location location) {
                    double latitude = location.getLatitude();
                    double longitude = location.getLongitude();
                    LatLng latLng = new LatLng(latitude,longitude);
                    displayOnMap(latLng,13.2f);
                }

                @Override
                public void onStatusChanged(String provider, int status, Bundle extras) {

                }

                @Override
                public void onProviderEnabled(String provider) {

                }

                @Override
                public void onProviderDisabled(String provider) {

                }
            });
        }

    }


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

        mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                displayOnMap(latLng,0f);
            }
        });


    }

    public void displayOnMap(LatLng latLng, Float zoom){
        Geocoder geocoder = new Geocoder(getApplicationContext());
        try {
            mMap.clear();
            List<Address> addressList = geocoder.getFromLocation(latLng.latitude,latLng.longitude,1);
            String str = addressList.get(0).getAddressLine(0) + ", ";
            str += addressList.get(0).getCountryName();
            mMap.addMarker(new MarkerOptions().position(latLng).title(str));
            if(zoom!=0)
            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoom));
            else
                mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

问题:假设打开地图时,它指向位置“ A”。现在,我想拖动标记图钉并通过单击地图上的不同位置(例如“ B”,“ C”,“ D”等)在地图上移动。发生的事情是,图钉正确地移动到了新位置地图,但有时在地图上标记不同的位置时,它会刷新并返回到初始位置。 也就是说,当我从A转到B到C时,有时,当我从B转到C时,该引脚会自动返回A,就好像它已经刷新一样。

编辑1:我注意到onCreate()中的onLocationChanged被调用,然后又返回。为什么会这样?

0 个答案:

没有答案