然后再添加另一个检查现有标记 [英] Check for existing markers before adding another

查看:192
本文介绍了然后再添加另一个检查现有标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谷歌地图API V2

虽然我在等待另一个应用程序敲定我创建另一个出于兴趣,只会使1标记被添加到地图在同一时间(我爱贴图我这样做)。


目前,我有这个code这将清除地图,如果一个标记已经存在,然后添加一个又一个。但我想要的是它显示一条消息,告诉用户清除地图的时候,才允许添加另一个标记。

Google Maps API v2
Whilst I was waiting for another app to be finalised I was creating another out of interest that would only enable 1 marker to be added to a map at a time (I love maps I do).

I currently have this code which will clear the map if a marker already exists and then add another one. But what I wanted was for it to show a message telling to user to clear the map before it will allow another marker to be added.

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

我试过:

@Override
    public void onMapClick(LatLng position){
        if (position != null){
            Toast.makeText(this, "Clear map before adding another location", Toast.LENGTH_SHORT).show();

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

但一切确实是显示我想即使消息时不存在任何标记,所以你不能真正添加​​任何标记。我应该在这里使用ArrayList的?该应用程序将只允许1标志在设计的时候。我环顾四周,并没有发现具体到我需要什么要求了。

But all that does is show the message I want even when no markers exist so you can't actually add any markers. Should I be utilizing ArrayList here? The app will only allow 1 marker at a time by design. I have looked around and not found anything specific to my needs before asking.

感谢

这是我的编辑code,它仍然不能正常工作:

This is my edited code, which still doesn't work:

@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){
            Toast.makeText(this, "Clear first", Toast.LENGTH_SHORT).show();
            /*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)));           
        }
    }

这仍然没有让我添加标记后,我已经清除使用onMapLongClickListener地图

It is still not letting me add a marker after I have cleared the map using onMapLongClickListener

推荐答案

addMarker返回一个标记对象,你可以用它来更新或表明您创建的标记了。

addMarker returns a Marker object you can use to update or indicate that you created the Marker already.

您也可以使用标记对象从地图上,而不是清除(删除),去除所有标记。

You can also use the Marker object to remove it from the map instead of clear() that removes all markers.

在伪code:

Marker marker;

if(marker == null) {
   marker = map.addMarker(...)
} else {
   ....
}

这篇关于然后再添加另一个检查现有标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆