简体   繁体   English

为什么我的标记未重置为null?

[英]Why is my marker not resetting to null?

I have a map that will only allow 1 marker at a time. 我有一张地图,一次只允许1个标记。 But in order to stop that marker being accidentally replaced with another, it needs to be set so that the marker is cleared before another marker can be added. 但是为了阻止该标记被另一个标记意外替换,需要对其进行设置,以便在添加另一个标记之前清除该标记。
I am setting the marker via onMapClick and clearing them via onMapLongClick. 我通过onMapClick设置标记,并通过onMapLongClick清除它们。 As it stands at the moment, the clearing and adding of markers works fine, but I cannot seem to set up the map so that you need to clear the map first. 目前,清除和添加标记的效果很好,但是我似乎无法设置地图,因此您需要先清除地图。
I have tried the solution from check for existing markers but it hasn't worked. 我已经尝试通过检查现有标记来解决该问题,但没有成功。
Here is my code for the setup, which currently works by clearing existing marker and adding another without the need to clear the original marker first: 这是我的设置代码,当前可以通过清除现有标记并添加另一个而无需先清除原始标记的方式来工作:

@Override
    public void onMapLongClick(LatLng position) {
        mMap.clear();
        Toast.makeText(this, "Position Cleared", Toast.LENGTH_SHORT).show();
        position = null;
    }

    @Override
    public void onMapClick(LatLng position){
        if (position != null){
            mMap.clear();
            mMap.addMarker(new MarkerOptions()
            .position(position)
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher_new)));
        }
        else {

            mMap.addMarker(new MarkerOptions()
            .position(position)
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher_new)));           
        }
    }

But I thought it should be something like: 但我认为应该是这样的:

@Override
    public void onMapLongClick(LatLng position) {
        mMap.clear();
        Toast.makeText(this, "Position Cleared", Toast.LENGTH_SHORT).show();
        position = null;
    }

    @Override
    public void onMapClick(LatLng position){
        if (position == null){
            //mMap.clear();
            mMap.addMarker(new MarkerOptions()
            .position(position)
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher_new)));
        }
        else {
            Toast.makeText(this, "Clear first", Toast.LENGTH_SHORT).show();

        }
    }

But all that does is give me the Toast message and not be able to add a marker at all even on first load of the map. 但是所有要做的只是给我Toast消息,甚至在第一次加载地图时也根本无法添加标记。

Any help would be great 任何帮助都会很棒

Create a reference to the added marker, and remove it before adding it again on a new LatLng position. 创建对添加的标记的引用,并删除它,然后再将其添加到新的LatLng位置。

private Marker marker;

/**
**
some code here
**
**/

@Override
public void onMapClick(LatLng position) {
    if(marker != null) 
        marker.remove();
    marker = mMap.addMarker(new MarkerOptions().position(position).icon(
            BitmapDescriptorFactory
                    .fromResource(R.drawable.ic_launcher_new)));
}

I realised I was using code incorrectly as such I have fixed the issue as thus: 我意识到自己使用的代码不正确,因此我已经解决了以下问题:

public void onMapClick(LatLng position){
        if(marker == null) {
               marker = mMap.addMarker(new MarkerOptions()
               .position(position)
               .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher_new)));
            } else {
                Toast.makeText(this, "Please clear previous position before adding another", Toast.LENGTH_SHORT).show();
            }

The app now does as intended. 该应用现在可以按预期运行。 Thank you for your help 谢谢您的帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM