简体   繁体   English

只允许用户在谷歌地图上放置 1 个标记

[英]Allowing user to place 1 marker on google map only

I'm stuck on limiting the number of marker that user can tapped on the Google Map's map我坚持限制用户可以在谷歌地图的地图上点击的标记数量

The problem: Whenever the user tap on google map the marker keeps creating new ones and the old ones does not clear away.问题:每当用户点击谷歌地图时,标记就会不断创建新标记,而旧标记不会清除。

Update: I think that it does not fall into the else if statement.更新:我认为它不属于 else if 语句。 Because every time the function is called it restart the process.因为每次调用该函数时,它都会重新启动进程。 Thus allowing the marker to be create constantly.从而允许不断创建标记。

override fun onMapClick(point: LatLng){

val tappedLocation = LatLng(latitude,longitude)
        var mapMarkerCount: Int =0

        if(mapMarkerCount == 0) {
            map.addMarker(
                    MarkerOptions()
                            .position(tappedLocation)
                            .title("You have Clicked")

            )
            
            mapMarkerCount++

        }//check Marker Count
        else if(mapMarkerCount == 1){

            map.clear()
            mapMarkerCount--
        }
}

What I have tried is assigning a variable to sort of check the whether the marker has been created.我尝试过的是分配一个变量来检查标记是否已创建。 and if the marker exists, upon the next tap on the map it is supposed to clear the map and the new marker is at the new location that the user has tapped.如果标记存在,则在地图上的下一次点击时应该清除地图并且新标记位于用户点击的新位置。

I apologize for my bad English in advance我提前为我的英语不好道歉

i think that your problem comes from your variable declaration.我认为您的问题来自您的变量声明。

you should try to : delete the mapMarkerCount=0 and make it boolean i think that the program execute mapMarkerCount=0 everytime you add a marker so i my mind it explain why you can't delete the old one您应该尝试:删除mapMarkerCount=0并使其成为布尔值我认为程序每次添加标记时都会执行mapMarkerCount=0所以我认为它解释了为什么您不能删除旧的

if (markerExist == false)
addOne();
markerExist=true;
elsif (markerExist == true)
map.clear();
add one();

with this code, the first time you will only add a new marker.使用此代码,您第一次只会添加一个新标记。 and after you will clear the old marker and put the new one each time you touch the screen然后每次触摸屏幕时清除旧标记并放置新标记

Since override fun onMapClick(point: LatLng) ,由于override fun onMapClick(point: LatLng)

meaning to say that whenever the function is called it will always execute line by line of the fun onMapClick .意思是说,无论何时调用该函数,它都会一行一行地执行fun onMapClick

What you can do is, you can use map.clear() before placing maps.addMarker() So whenever the function is called it will clear the map first before adding.你可以做的是,你可以使用map.clear()将前maps.addMarker()所以,只要函数被调用它会增加前先清空地图。

override fun onMapClick(point: LatLng){

map.clear() //above the adding of marker

val tappedLocation = LatLng(latitude,longitude)

map.addMarker(
                    MarkerOptions()
                            .position(tappedLocation)
                            .title("You have Clicked")

            )
}

Essentially you do not need make a marker for check.基本上你不需要做一个标记来检查。 Not recommended if you have you want to keep multiple markers or other stuff as it will wipe out anything on the google map.如果您想保留多个标记或其他东西,不建议这样做,因为它会清除谷歌地图上的任何内容。 To the default map when user first see.用户第一次看到时的默认地图。

What your code is assign mapMarkerCount = 0 on every time the map is clicked, even though there might be a marker already on the map.每次点击地图时,您的代码都会分配mapMarkerCount = 0 ,即使地图上可能已经有一个标记。 And because of that it will always fall into the if statement block, because mapMarkerCount will never be equal to 1 when the map is clicked.正因为如此,它总是会落入 if 语句块中,因为单击地图时 mapMarkerCount 永远不会等于 1。 Doing this assignment at the very beginning of onCreate would be a better option.在 onCreate 一开始就做这个任务会是一个更好的选择。 Or as someone here has suggested, a boolean variable should be fine for your purpose considering you only wish to have one marker on the map.或者正如这里有人建议的那样,考虑到您只希望在地图上有一个标记,布尔变量应该可以满足您的目的。 One implementation:一种实现:

var mapMarkerExists: Boolean = false

override fun onMapClick(point: LatLng) {

    val tappedLocation = LatLng(latitude,longitude)

    if(mapMarkerExists) {
        map.clear()
        map.addMarker(
            MarkerOptions()
                .position(tappedLocation)
                .title("You have Clicked"))
    }//check Marker Count
    else {
        map.addMarker(
            MarkerOptions()
                .position(tappedLocation)
                .title("You have Clicked"))
        mapMarkerExists = true
    }
}

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

相关问题 从 Google 地图上的标记获取地名 - Getting the place name from a marker on Google Map 如何在谷歌地图上的屏幕中心放置标记 - How to place a Marker on center of the screen on google map 可以通过按一下按钮在Android的Google地图上放置高分的地图标记吗? - Is it possible to place a map marker with a highscore on a Google map in Android by pressing a button? 检测用户是否“取消选择”谷歌地图上的标记 - Detect if user “deselects” marker on google map 用户在Google Map V2标记上的输入 - User input on Google map V2 marker 如何在Android的Google Map Marker中添加搜索位置图像 - How to add search place image in google map marker in android 将字符串转换为双精度后无法在谷歌地图中放置标记 - Unable to place marker in google map after converting string to double 使用多个位置标记Android的Google Map v2在Zoomlevel中发布 - Issue in Zoomlevel in google map v2 with multiple place marker android 尝试在Google Map V2上放置标记时发生NullPointerException - NullPointerException When Trying to place Marker on Google Map V2 Android Google Maps活动,并在用户点击的位置创建了标记 - Android google maps activity with marker created in place where user clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM