如何为ClusterManager中的每个标记启动自定义URI意图?

时间:2019-03-25 22:43:57

标签: android kotlin maps markers

我当前正在尝试启动Uri意图,以便当用户单击标记的信息窗口时,它将启动google查询+我从Firebase获得并解析为ClusterManager的相应标记的名称。 / p>

我尝试创建一个字符串持有人以在其中存储公司名称,然后将其与Google查询字符串一起添加,并将其作为Uri Intent传递到我的setOnInfoWindowClickListener中。

注意:'TheNameOfThisMarker'实际上并未在此处定义,我只是想对我要完成的事情有一个想法”)

“我的信息”窗口中的单击侦听器:

        mMap.setOnInfoWindowClickListener(object : GoogleMap.OnInfoWindowClickListener {
            override fun onInfoWindowClick(marker: Marker) {
                val url = "http://www.google.com/#q=" + " " + (TheNameOfThisMarker)
                val i = Intent(Intent.ACTION_VIEW)
                i.setData(Uri.parse(url))
                startActivity(i)
            }
        })

从Firebase加载数据:

    private fun loadMarkersFromDB() {
        mCompanies.child("data/results").addListenerForSingleValueEvent(object : ValueEventListener {
            override fun onDataChange(dataSnapshot: DataSnapshot) {
                if (dataSnapshot.exists()) {
                    for (s in dataSnapshot.children) {
                        var company = s.getValue(Company::class.java)
                        val infoWindow =
                            MyItem(company!!.latitude, company.longitude, company.name)
                        mClusterManager.setAnimation(false)

                        if ((company.latitude < (circle.center.latitude + circle.radius) && company.latitude > (circle.center.latitude - circle.radius))
                            && ((company.longitude < (circle.center.longitude + circle.radius) && company.longitude > (circle.center.longitude - circle.radius)))
                        ) {
                            mClusterManager.addItem(infoWindow)
                        }

                        TheNameOfThisMarker = company!!.name
                        Log.d("onDataChange", company.toString())
                    }
                }
            }

            override fun onCancelled(databaseError: DatabaseError) {
                Log.w("MapsActivity", databaseError.toException())
            }
        })
    }

我的群集设置:

    private fun setUpCluster() {
        // Initialize the manager with the context and the map.
        // (Activity extends context, so we can pass 'this' in the constructor.)
        mClusterManager = ClusterManager(this, mMap)
        // Point the map's listeners at the listeners implemented by the cluster
        // manager.
        mMap.setOnCameraIdleListener(mClusterManager)
        mMap.setOnMarkerClickListener(mClusterManager)

        // Add cluster items (markers) to the cluster manager.
        loadMarkersFromDB()
    }

尽管我可以在“信息窗口”的“单击侦听器”中执行某些操作,但我不知道如何查明每个标记的名称并将其添加到查询Uri意图中。

1 个答案:

答案 0 :(得分:0)

经过大量研究和反复试验,我找到了解决自己问题的方法。

我创建了 LatLng,字符串

的HashMap

我将Firebase中的LatLng位置以及String类型的公司名称存储到HashMap中,然后在setOnInfoWindowClickListener上创建一个lat和long变量,其中包含marker.position.latitude和经度。然后,我创建另一个名为LatLng的变量addPositions,并将lat和long标记位置添加到其中。然后我就去companyMap.get(addPositions)和瞧!我得到了相应标记的公司字符串名称!

    private lateinit var companyName: String
    private lateinit var companyLocation: LatLng
    private val companyMap = hashMapOf<LatLng, String>()
        mMap.setOnInfoWindowClickListener(object : GoogleMap.OnInfoWindowClickListener {
            override fun onInfoWindowClick(marker: Marker) {
                var lat = marker.position.latitude
                var long = marker.position.longitude
                var addPositions: LatLng
                addPositions = LatLng(lat, long)

                var companyGet = companyMap.get(addPositions)

                val url = "http://www.google.com/#q=" + companyGet.toString()
                val i = Intent(Intent.ACTION_VIEW)
                i.setData(Uri.parse(url))
                startActivity(i)
            }
        })
  private fun loadMarkersFromDB() {
        mCompanies.child("data/results").addListenerForSingleValueEvent(object : ValueEventListener {
            override fun onDataChange(dataSnapshot: DataSnapshot) {
                if (dataSnapshot.exists()) {
                    for (s in dataSnapshot.children) {
                        var company = s.getValue(Company::class.java)
                        val infoWindow =
                            MyItem(company!!.latitude, company.longitude, company.name)
                        mClusterManager.setAnimation(false)

                        if ((company.latitude < (circle.center.latitude + circle.radius) && company.latitude > (circle.center.latitude - circle.radius))
                            && ((company.longitude < (circle.center.longitude + circle.radius) && company.longitude > (circle.center.longitude - circle.radius)))
                        ) {
                            mClusterManager.addItem(infoWindow)
                        }

                        companyName = company.name
                        companyLocation = LatLng(company.latitude, company.longitude)
                        companyMap.put(companyLocation, companyName)
                        Log.d("onDataChange", company.toString())
                    }
                }
            }

            override fun onCancelled(databaseError: DatabaseError) {
                Log.w("MapsActivity", databaseError.toException())
            }
        })
    }

呜呜! :D