如何在 OSM 地图上创建信息窗口?

如何解决如何在 OSM 地图上创建信息窗口?

如何在 OSM 地图上创建信息窗口?

我是 android OSM 的新手,我想创建自定义标记,我没有找到任何相关的参考代码,请与我分享任何参考链接代码示例。

enter image description here

信息窗口在地图上方的弹出窗口中显示文本或图像。

解决方法

依赖:

   // open street map
    implementation 'org.osmdroid:osmdroid-android:6.1.10'

地图片段:OSM地图

class HomeFragment : Fragment() {
    private var binding: FragmentHomeBinding? = null
    //private lateinit var homeViewModel: HomeViewModel

    override fun onCreateView(
            inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?
    ): View {
        binding = FragmentHomeBinding.inflate(inflater)



        // OSM
        Configuration.getInstance().userAgentValue = BuildConfig.APPLICATION_ID

        binding!!.mapview.apply {
            // osm map
            setTileSource(TileSourceFactory.DEFAULT_TILE_SOURCE)
            setBuiltInZoomControls(false)  //https://stackoverflow.com/questions/67184020/what-do-i-use-now-that-osm-setbuiltinzoomcontrols-is-deprecated
            setMultiTouchControls(true)
            val mapController: IMapController = controller
            mapController.setZoom(10.0)
            val zoomLocation = GeoPoint(38.2352328,-85.7602859)
            mapController.setCenter(zoomLocation)

            // map marker
            val startMarker = Marker(this)
            startMarker.position = zoomLocation // marker location
            startMarker.icon =ContextCompat.getDrawable(requireContext(),R.drawable.test_marker)
            startMarker.setAnchor(Marker.ANCHOR_CENTER,1.0f)
            val infoWindow: InfoWindow = MyInfoWindow(R.layout.map_infobubble_black,binding!!.mapview)
            startMarker.infoWindow = infoWindow
            binding!!.mapview.overlays.add(startMarker)
            //startMarker.title = "Title of the marker"

            startMarker.setOnMarkerClickListener { marker,mapView ->
                marker.showInfoWindow()
                //mapView.controller.animateTo(marker.position)
                Toast.makeText(context,"on click to show info box",Toast.LENGTH_LONG).show()
                true
            }


            // polyline
            val gPt0 = GeoPoint(38.2352328,-85.7602859)
            val gPt1 = GeoPoint(38.2350183,-85.7603073)
            val gPt2 = GeoPoint(38.2348895,-85.7586551)
            val gPt3 = GeoPoint(38.2348037,-85.757850)
            val gPt4 = GeoPoint(38.2347286,-85.757035)
            val line = Polyline(this)
            line.addPoint(gPt0)
            line.addPoint(gPt1)
            line.addPoint(gPt2)
            line.addPoint(gPt3)
            line.addPoint(gPt4)

            overlays.add(line)

            line.setOnClickListener { polyline,mapView,eventPos ->
                Toast.makeText(context,"on click",Toast.LENGTH_LONG).show()
                true
            }

        }











        /*homeViewModel =
                ViewModelProvider(this).get(HomeViewModel::class.java)
        val root = inflater.inflate(R.layout.fragment_home,container,false)
        val textView: TextView = root.findViewById(R.id.text_home)
        homeViewModel.text.observe(viewLifecycleOwner,Observer {
            textView.text = it
        })*/
        return binding!!.root
    }


    override fun onResume() {
        super.onResume()
        binding!!.mapview.onResume()
    }

    override fun onPause() {
        super.onPause()
        binding!!.mapview.onPause()
    }
    override fun onDestroyView() {
        super.onDestroyView()
        binding=null
    } }

xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.home.HomeFragment">

    <org.osmdroid.views.MapView
        android:id="@+id/mapview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

自定义信息布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/bubble_layout"
    android:background="@color/white"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/dialog_info"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="Lorem ipsum,or lipsum as it is sometimes known,is dummy text used in laying out print,graphic or web designs. The passage is attributed to an unknown typesetter in the 15th century who is thought to have scrambled parts of Cicero's De Finibus Bonorum et Malorum for use in a type specimen book."/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_below="@id/dialog_info">

        <Button
            android:id="@+id/dialog_cancel"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.50"
            android:background="@color/purple_700"
            android:text="Cancel"/>

        <Button
            android:id="@+id/dialog_ok"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.50"
            android:background="@color/purple_500"
            android:text="Agree"/>
    </LinearLayout>
</RelativeLayout>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?