如何更改 Android 的点击叠加标记?
我制作了一个应用程序,当用户单击它时,我需要更改覆盖项目的可绘制对象。我正在使用以下代码来实现此效果:
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
if(item.getTitle().equals("true")){
if(item.getMarker(OverlayItem.ITEM_STATE_FOCUSED_MASK).equals(greenMarker)){
item.setMarker(orangeMarker);
view1ComplainPoleList.add(item.getSnippet());
Log.i("adding",item.getSnippet());
map.invalidate();
}
else{
item.setMarker(greenMarker);
view1ComplainPoleList.remove(item.getSnippet());
Log.i("removing",item.getSnippet());
map.invalidate();
}
}
return true;
}
但这似乎不起作用。每当我点击一个覆盖项目时,它就会从视图中消失。出了什么问题?
更新:
您还能告诉我如何在地图滚动时加载新视图吗...?
先感谢您。
i have made an application in which i need to change the drawable of an overlayitem when a user clicks on it. i am using the following code to achieve this effect:
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
if(item.getTitle().equals("true")){
if(item.getMarker(OverlayItem.ITEM_STATE_FOCUSED_MASK).equals(greenMarker)){
item.setMarker(orangeMarker);
view1ComplainPoleList.add(item.getSnippet());
Log.i("adding",item.getSnippet());
map.invalidate();
}
else{
item.setMarker(greenMarker);
view1ComplainPoleList.remove(item.getSnippet());
Log.i("removing",item.getSnippet());
map.invalidate();
}
}
return true;
}
But this does not seem to be working. whenever i click on an overlayitem it disappears from view. what is going wrong?
UPDATE:
could you also tell me how to load new views when the map is scrolled...?
thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这非常简单:
This is very simple to do:
我在这里看到很多答案都以困难的方式做到这一点。如果您有 2 个图像并且想要根据焦点翻转它们,请采用简单的方法:
第 1 步:将两个图像复制到可绘制文件夹中:
示例:mycon_focused.png、mycon.png
第 2 步:在中创建选择器 xml 文件可绘制对象:示例“marker.xml”
步骤 3:当您创建 ItemOverlay 并添加 OverlayItems 可绘制对象时,请使用
而不是
,然后在点击方法中以编程方式更改它。在阅读了很多答案并且在任何地方都没有看到这个之后,我自己尝试了一下,效果非常好。
非常感谢以前的贡献者,没有你们的帮助我就不会有一个起点。
另一个注意事项:如果您使用 Sherif elKhatib 建议的代码并且您的标记位置已关闭,请尝试:
而不是
This 应该将其完美居中。
I see a lot of answers here doing this the hard way. If you have 2 images and you want to flip them based on focus, do it the easy way:
Step 1: Copy both images into a drawables folder:
Example: mycon_focused.png, mycon.png
Step 2: Create a selector xml file in drawables: Example "marker.xml"
Step 3: When you create your ItemOverlay and add the OverlayItems drawable, use
instead of
and then programically changing it in the on tap method. After reading through many answers and not seeing this anywhere I just tried it myself, and it worked perfectly.
Much thanks to previous contributors, without your help I wouldn't have had a starting point.
Another note: If you're using Sherif elKhatib suggested code and your marker position is off try:
instead of
This should center it perfectly.
我不知道 view1ComplainPoleList 是什么以及它是否会影响事情。我通过子类化 OverlayItem 并重写 getMarker() 以返回正确的图像来处理此问题。 这是我使用此技术的示例项目。
I have no idea what
view1ComplainPoleList
is and whether it is impacting matters. I handled this by subclassingOverlayItem
and overridinggetMarker()
to return the proper image. Here is the sample project in which I use this technique.使用 Setbounds api 设置边界,如下所示:编辑您的原始代码
Use Setbounds api to set the bounds as follows: Edited your original code