0

I am currently trying to launch a Uri intent so that when the user clicks the Info Window of a marker, it will launch a google query + the name of that corresponding marker which I have obtained from Firebase and parsed into ClusterManager.

I have tried creating a String holder to store the company name inside it, and then adding it with a google query string and passing it as a Uri Intent all inside my setOnInfoWindowClickListener.

(NOTE: 'TheNameOfThisMarker' is not actually defined here, I just wanted to give an idea of what I am trying to accomplish')

My Info Window Click Listener:

        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)
            }
        })

Loading Data from 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())
            }
        })
    }

My Cluster setup:

    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()
    }

Although I am able to do something inside my Info Window Click Listener, I have no idea how to pinpoint each individual marker's name and add it into my query Uri intent.

1 Answer 1

1

After a lot of research and trial and error, I found a solution to my own problem.

I created HashMap of LatLng, String

I store the LatLng locations from my Firebase into the HashMap as well as the Company names which is of type String, and then on my setOnInfoWindowClickListener, I create a lat and long variable that holds marker.position.latitude, and longitude. I then create another variable called addPositions of type LatLng, and I add the lat and long marker positions into it. Then I just go companyMap.get(addPositions) and Voila!! I get the company String name for the corresponding markers!

    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())
            }
        })
    }

WhooHoo!! :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.