简体   繁体   English

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

[英]Add Custom Control as Marker OSMDROID BONUS PACK

Can I add Custom Control as Marker to OSMBONUSPACK?我可以将自定义控件作为标记添加到 OSMBONUSPACK 吗?

I create some Buttons and image in Android xml file Named MyMarkerItem.xml我在名为 MyMarkerItem.xml 的 Android xml 文件中创建了一些按钮和图像

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

Thanks谢谢

OK, I understand that you want the marker icon itself to be not a simple bitmap, but a layout.好的,我知道您希望标记图标本身不是一个简单的位图,而是一个布局。

What you can do is to use the marker infowindow "instead" of the marker icon.您可以做的是使用标记信息窗口“代替”标记图标。

First of all, create a new class CustomInfoWindow which inherits from MarkerInfoWindow - the default InfoWindow for Markers, and uses your own layout:首先,创建一个新类 CustomInfoWindow,它继承自 MarkerInfoWindow - Marker 的默认 InfoWindow,并使用您自己的布局:

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

CustomInfoWindow myCustomInfoWindow = new CustomInfoWindow(mapView);

Then, just after creating your Marker, do that:然后,在创建标记之后,执行以下操作:

  • Set a marker icon as a 1x1 pixel size bitmap, fully transparent: setIcon(mySmall_InvisibleIcon)将标记图标设置为 1x1 像素大小的位图,完全透明:setIcon(mySmall_InvisibleIcon)
  • Set the marker infowindow to your own: setInfoWindow(myCustomInfoWindow)将标记信息窗口设置为您自己的: setInfoWindow(myCustomInfoWindow)
  • Set the infowindow "anchor" to the most appropriate and natural position, depending on the "look" of your layout: setInfoWindowAnchor(ANCHOR_CENTER, ANCHOR_CENTER) maybe?根据布局的“外观”,将信息窗口“锚点”设置为最合适和自然的位置: setInfoWindowAnchor(ANCHOR_CENTER, ANCHOR_CENTER) 也许?
  • Force the opening of the infowindow: showInfoWindow()强制打开信息窗口:showInfoWindow()

All these steps are fairly simple.所有这些步骤都相当简单。

But then, I guess you expect some behaviour to happen when the user will click on your layout buttons.但是,我猜您希望当用户单击您的布局按钮时会发生一些行为。 So, inside your CustomInfoWindow code, you will certainly have to do some work => follow this tutorial .因此,在您的 CustomInfoWindow 代码中,您肯定需要做一些工作 => 遵循本教程

Markers in Osmdroid arent actually android views and therefore it's not possible to add other components into them. Osmdroid 中的标记实际上并不是 android 视图,因此不可能向其中添加其他组件。 They are basically just an image.它们基本上只是一个图像。

Simple and suggested solution简单且建议的解决方案

You can add your components to a MarkerInfoWindow which is displayed after a click, though.不过,您可以将组件添加到单击后显示的MarkerInfoWindow

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

Possible "real" soluiton可能的“真实”解决方案

If you really need such behaviour - meaning you really need multiple buttons displayed on a map as a "marker" and give the user the opportunity to click on them - it should be possible to create your own implementation of a Marker-like class.如果你真的需要这样的行为——意味着你真的需要在地图上显示多个按钮作为“标记”并让用户有机会点击它们——应该可以创建自己的类标记类实现。 In other words, you would have to implement your own subclass of OverlayWithIW or Overlay and override (mainly) the draw method (which should draw your buttons to a canvas) and the onSingleTapConfirmed method, where you would need to detect properly on which button user clicked and call the related action.换句话说,您必须实现自己的OverlayWithIWOverlay子类并覆盖(主要) draw方法(它应该将您的按钮绘制到画布上)和onSingleTapConfirmed方法,您需要在该方法中正确检测哪个按钮用户单击并调用相关操作。 Try to go through source of the Marker class , it's a good example.尝试查看Marker 类的源代码,这是一个很好的例子。

But keep in mind: this is an advanced task.但请记住:这是一项高级任务。 Everything related to drawing on canvas can lead to performance issues if it's not done properly.如果未正确完成,与在画布上绘图相关的所有内容都可能导致性能问题。 There will be edge cases you'll have to cover.会有一些边缘情况,你必须涵盖。 There may be other problems you'll need to solve and debug.您可能需要解决和调试其他问题。 I would not suggest such a solution to a beginner.我不会向初学者建议这样的解决方案。

It's August 2021 and I wanted to add an arbitrary layout as my marker item.现在是 2021 年 8 月,我想添加一个任意布局作为我的标记项。 Much as the OP is asking for here.正如 OP 在这里所要求的那样。

I tried the Simple and suggested solution of https://stackoverflow.com/a/53003664/866333 :我尝试了https://stackoverflow.com/a/53003664/866333简单和建议的解决方案

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

I get a runtime exception when I click it:单击它时出现运行时异常:

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

I refine my layout down to a single TextView widget for arguments sake and the error persists.为了参数,我将布局细化为单个 TextView 小部件,但错误仍然存​​在。


I give up on that answer and attempt the accepted one: https://stackoverflow.com/a/53216542/866333我放弃了那个答案并尝试接受一个答案: 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);

Using the same my_own_layout as before, let's call it R.layout.my_info_window使用与之前相同的my_own_layout ,我们称之为R.layout.my_info_window

I get the same error:我犯了同样的错误:

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

I dug into the AS-reverse engineered (thanks AS) source code to discover the reason for this error is that the layout being passed to the MarkerInfoWindow super class constructor has particular id requirements.我深入研究了 AS 逆向工程(感谢 AS)源代码,发现此错误的原因是传递给MarkerInfoWindow超类构造函数的布局具有特定的 id 要求。

We must have these ids in our layout: bubble_title , bubble_description , bubble_subdescription , bubble_image我们的布局中必须有这些 id: bubble_titlebubble_descriptionbubble_subdescriptionbubble_image

Here is a minimal working example:这是一个最小的工作示例:

<?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>

That works great!效果很好!

I can even attempt to customise by appending another widget:我什至可以尝试通过附加另一个小部件来进行自定义:

    <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" />

The absoluteY is ignored and superimposed on the only text I set programmatically, the title. absoluteY被忽略并叠加在我以编程方式设置的唯一文本上,即标题。 IOW, the widget isn't purely declarative xml. IOW,小部件不是纯粹的声明性 xml。 I'm afraid ConstraintLayout is after my time as a front end engineer so I'll leave it to the reader to experiment from here.我担心ConstraintLayout是在我作为前端工程师的时间之后,所以我将把它留给读者从这里开始实验。

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

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