【问题标题】:Marker disappearing and title not showing标记消失且标题未显示
【发布时间】:2020-08-25 08:15:02
【问题描述】:

我试图通过长按该位置并显示该位置的名称来在我的 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】:

    每次您在此处更改位置时,您都在清除地图:

    @Override
    public void onLocationChanged(Location location) {
    
         centerMapOnLocation(location, "Your Location");
    
    }
    

    这就是为什么您的标记在添加到地图后立即消失的原因。如果您在 centerMapOnLocation 方法中删除或注释掉这一行:

    mMap.clear();
    

    然后标记不再消失。

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多