Android Studio 3.3 и Google Maps API, onMapLongClick не работает - PullRequest
0 голосов
/ 21 января 2019

onMapLongClick () неправильно добавляет маркер. Он добавляет его, а затем удаляет.

Я следую руководству по включению карт Google в Android Studio. В целях отладки я поместил Toast в метод onMapLongClick для отображения «Местоположение сохранено», которое работает, но не сохраняет маркер. Также я использую эмулятор Nexus 4 API 23.

Вот код из MapsActivity.java:

public class MapsActivity extends FragmentActivity implements 
OnMapReadyCallback, 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);
            centerMapOnLocation(lastKnownLocation, "Your location");
        }
    }

}
public void centerMapOnLocation(Location location, String title){
    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, 10));
}


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

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

    mMap.setOnMapLongClickListener(this);

    Intent intent = getIntent();
    if(intent.getIntExtra("placeNumber", 0) == 0){
        //zoom in on user's location
        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                centerMapOnLocation(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 device is running SDK < 23
        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) {
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
                Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                centerMapOnLocation(lastKnownLocation, "Your location");
            }else{
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
            }
        }
    }

}

@Override
public void onMapLongClick(LatLng latLng) {

    mMap.addMarker(new MarkerOptions().position(latLng).title("Your new memorable place"));

    Toast.makeText(this, "Location saved", Toast.LENGTH_LONG).show();
}

}

Общая цель состоит в том, чтобы при длительном щелчке он добавлял маркер и сохранял это местоположение в ArrayList, который отображается в MainActivity.

...