繁体   English   中英

添加自定义控件作为标记 OSMDROID BONUS PACK

[英]Add Custom Control as Marker OSMDROID BONUS PACK

我可以将自定义控件作为标记添加到 OSMBONUSPACK 吗?

我在名为 MyMarkerItem.xml 的 Android xml 文件中创建了一些按钮和图像

我想要类似MyMarker.setDesign(R.layout.MyMarkerItem);东西MyMarker.setDesign(R.layout.MyMarkerItem);

谢谢

好的,我知道您希望标记图标本身不是一个简单的位图,而是一个布局。

您可以做的是使用标记信息窗口“代替”标记图标。

首先,创建一个新类 CustomInfoWindow,它继承自 MarkerInfoWindow - Marker 的默认 InfoWindow,并使用您自己的布局:

public class CustomInfoWindow extends MarkerInfoWindow {
    public CustomInfoWindow(MapView mapView) {
        super(my_own_layout, mapView);
    }
}

CustomInfoWindow myCustomInfoWindow = new CustomInfoWindow(mapView);

然后,在创建标记之后,执行以下操作:

  • 将标记图标设置为 1x1 像素大小的位图,完全透明:setIcon(mySmall_InvisibleIcon)
  • 将标记信息窗口设置为您自己的: setInfoWindow(myCustomInfoWindow)
  • 根据布局的“外观”,将信息窗口“锚点”设置为最合适和自然的位置: setInfoWindowAnchor(ANCHOR_CENTER, ANCHOR_CENTER) 也许?
  • 强制打开信息窗口:showInfoWindow()

所有这些步骤都相当简单。

但是,我猜您希望当用户单击您的布局按钮时会发生一些行为。 因此,在您的 CustomInfoWindow 代码中,您肯定需要做一些工作 => 遵循本教程

Osmdroid 中的标记实际上并不是 android 视图,因此不可能向其中添加其他组件。 它们基本上只是一个图像。

简单且建议的解决方案

不过,您可以将组件添加到单击后显示的MarkerInfoWindow

marker.setInfoWindow(new MarkerInfoWindow(R.layout.MyMarkerItem, mapView));

可能的“真实”解决方案

如果你真的需要这样的行为——意味着你真的需要在地图上显示多个按钮作为“标记”并让用户有机会点击它们——应该可以创建自己的类标记类实现。 换句话说,您必须实现自己的OverlayWithIWOverlay子类并覆盖(主要) draw方法(它应该将您的按钮绘制到画布上)和onSingleTapConfirmed方法,您需要在该方法中正确检测哪个按钮用户单击并调用相关操作。 尝试查看Marker 类的源代码,这是一个很好的例子。

但请记住:这是一项高级任务。 如果未正确完成,与在画布上绘图相关的所有内容都可能导致性能问题。 会有一些边缘情况,你必须涵盖。 您可能需要解决和调试其他问题。 我不会向初学者建议这样的解决方案。

现在是 2021 年 8 月,我想添加一个任意布局作为我的标记项。 正如 OP 在这里所要求的那样。

我尝试了https://stackoverflow.com/a/53003664/866333简单和建议的解决方案

marker.setInfoWindow(new MarkerInfoWindow(R.layout.MyMarkerItem, mapView));

单击它时出现运行时异常:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

为了参数,我将布局细化为单个 TextView 小部件,但错误仍然存​​在。


我放弃了那个答案并尝试接受一个答案: https : //stackoverflow.com/a/53216542/866333

public class CustomInfoWindow extends MarkerInfoWindow {
    public CustomInfoWindow(MapView mapView) {
        super(my_own_layout, mapView);
    }
}

CustomInfoWindow myCustomInfoWindow = new CustomInfoWindow(mapView);

使用与之前相同的my_own_layout ,我们称之为R.layout.my_info_window

我犯了同样的错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

我深入研究了 AS 逆向工程(感谢 AS)源代码,发现此错误的原因是传递给MarkerInfoWindow超类构造函数的布局具有特定的 id 要求。

我们的布局中必须有这些 id: bubble_titlebubble_descriptionbubble_subdescriptionbubble_image

这是一个最小的工作示例:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/bubble_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        tools:layout_editor_absoluteX="153dp"
        tools:layout_editor_absoluteY="20dp" />

    <TextView
        android:id="@+id/bubble_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        tools:layout_editor_absoluteX="153dp"
        tools:layout_editor_absoluteY="39dp" />

    <TextView
        android:id="@+id/bubble_subdescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        tools:layout_editor_absoluteX="153dp"
        tools:layout_editor_absoluteY="58dp" />

    <ImageView
        android:id="@+id/bubble_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/marker_cluster"
        tools:layout_editor_absoluteX="145dp"
        tools:layout_editor_absoluteY="76dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

效果很好!

我什至可以尝试通过附加另一个小部件来进行自定义:

    <TextView
        android:id="@+id/anothertexttestview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="look ma!"
        tools:layout_editor_absoluteX="153dp"
        tools:layout_editor_absoluteY="125dp" />

absoluteY被忽略并叠加在我以编程方式设置的唯一文本上,即标题。 IOW,小部件不是纯粹的声明性 xml。 我担心ConstraintLayout是在我作为前端工程师的时间之后,所以我将把它留给读者从这里开始实验。

暂无
暂无

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

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