标记消失,标题不显示

问题描述 投票:2回答:1

试图通过长按该位置并显示该位置的名称来在我的Google地图上添加一个标记,但是,每当我长按该标记时,它就会出现一秒钟然后消失(消失)。

我理解长按后应该保留标记,然后显示标题,但不起作用

[如果有人遇到任何此类问题,请告诉我,并可以帮助我。

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

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


/**
 * 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) {
        //zoom in on the 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 (Build.VERSION.SDK_INT < 23) {

            if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    Activity#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for Activity#requestPermissions for more details.
                return;
            }
            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) {

    Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());

    String address ="";

    try {
        List <Address> listAddresses= geocoder.getFromLocation(latLng.latitude,latLng.longitude,1);

        if (listAddresses!=null&& listAddresses.size()>0){

            if (listAddresses.get(0).getThoroughfare()!=null){

                if (listAddresses.get(0).getSubThoroughfare()!=null){

                    address+=listAddresses.get(0).getSubThoroughfare()+" ";
                }

                address+= listAddresses.get(0).getThoroughfare();

            }

        }

    } catch (IOException e) {
        e.printStackTrace();
    }
    if (address == ""){

        SimpleDateFormat sdf = new SimpleDateFormat("mm:HH yyyyMMdd", Locale.getDefault());
        address = sdf.format(new Date());

    }

    mMap.addMarker(new MarkerOptions().position(latLng).title(address).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));

}

}

java android android-studio google-maps google-maps-markers
1个回答
0
投票

您每次在这里更改位置都会清除您的地图:

© www.soinside.com 2019 - 2024. All rights reserved.