简体   繁体   English

如何删除最近的地图标记Google Map API

[英]How To Remove Nearest Map Marker Google Map API

I'm new to stackoverflow; 我是stackoverflow的新手。 noticed it was helpful for a lot of folks so I figured I'd try it out myself. 注意到它对很多人都有帮助,所以我想自己可以尝试一下。

I'm working on a project now for Android. 我正在为Android开发一个项目。

The gist of it goes like this: the user spawns a map with markers generated at random from a set radius with the current location being the origin. 要点是这样的:用户生成一个地图,该地图具有从设置的半径随机生成的标记,当前位置为原点。 The user then has to "collect" each of the virtual objects, at which point the game will end and go back to the main menu. 然后,用户必须“收集”每个虚拟对象,此时游戏将结束并返回主菜单。 Pretty simple concept. 很简单的概念。

My question: how do I remove markers based on the user's current location? 我的问题:如何根据用户的当前位置删除标记? In other words, how can I have the markers react to the user's location so that they're removed from the map respectively if the user is within a certain distance from them? 换句话说,如果用户与标记相距一定距离,我如何让标记对用户的位置做出反应,以便分别从地图上将其删除? I've looked online and found a few things on this site about calculating distance but nothing about removing markers based on distance. 我在网上看了一下,发现该站点上有一些有关计算距离的内容,但与基于距离删除标记无关。

Any help is appreciated. 任何帮助表示赞赏。 Also, critiques on my etiquette are accepted too; 同样,对我的礼节的批评也被接受; I'm unfamiliar with how one is to ask questions here. 我不熟悉在这里问问题的方式。

Thanks. 谢谢。

You should provide what you have tried to ask for help. 您应提供您试图寻求帮助的内容。 Have you achieved displaying markers on the map? 您是否已在地图上显示标记?

You need to register for location updates to make your app aware of the location changes. 您需要注册位置更新,以使您的应用知道位置更改。 Details: http://developer.android.com/training/location/receive-location-updates.html 详细信息: http : //developer.android.com/training/location/receive-location-updates.html

Each time a location update is provided, a method called onLocationChanged(Location userLocation) will be triggered. 每次提供位置更新时,都会触发一个名为onLocationChanged(Location userLocation)的方法。 You should use this information for distance calculation. 您应该使用此信息进行距离计算。

Traverse your list of markers and check the distance for each of them. 遍历您的标记列表,并检查每个标记的距离。

Here is a method calculating the distance between two locations from a bird's eye: 这是一种从鸟瞰角度计算两个位置之间的距离的方法:

public float getDistance(double startLat, double startLong, double goalLat, double goalLong)
{
    float[] resultArray = new float[1];
    Location.distanceBetween(startLat, startLong, goalLat, goalLong, resultArray);
    return resultArray[0];
}

Your marker object has a remove() method. 您的标记对象具有remove()方法。 Remove those that are closer to user's current location. 删除那些更接近用户当前位置的位置。

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

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