Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to make an application with Kotlin and Google Maps in which the user can add markers on the Google Map and that these markers are sent to Firebase and other users can see those same markers on the map.

This is what I have done but it is not working well for me.

package com.racrapa.displayinglocationapp
        
import android.content.ContentValues
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.annotation.NonNull
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback
import com.google.android.gms.maps.SupportMapFragment
import com.google.android.gms.maps.model.BitmapDescriptorFactory
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.Marker
import com.google.android.gms.maps.model.MarkerOptions
import com.google.firebase.database.*
import com.google.firebase.database.ktx.database
import com.google.firebase.ktx.Firebase
import com.google.firebase.database.ValueEventListener
import com.google.firebase.database.DataSnapshot
import com.google.firebase.database.DatabaseError
import com.google.firebase.database.DatabaseReference

private const val TAG = "MapsActivity"

class MapsActivity : AppCompatActivity(), OnMapReadyCallback {

    private lateinit var mMap: GoogleMap
    public lateinit var databaseRef: DatabaseReference
    private lateinit var database: DatabaseReference
    private var markers: MutableList<Marker> = mutableListOf()
    private var postListener: ValueEventListener? = null
    var mDatabase: DatabaseReference? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_maps)
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        val mapFragment = supportFragmentManager
                .findFragmentById(R.id.map) as SupportMapFragment
        mapFragment.getMapAsync(this)
        databaseRef = Firebase.database.reference
        mDatabase = FirebaseDatabase.getInstance().reference.child("userlocation")
        database = Firebase.database.reference

    }

This is to get the data from Firebase

    public override fun onStart() {
        super.onStart()
        var postListener: ValueEventListener = object : ValueEventListener {
            override fun onDataChange(dataSnapshot: DataSnapshot) {
                if (dataSnapshot.exists()) {

                    val locationlogging = dataSnapshot.child("userlocation").getValue(LocationLogging::class.java)
                    var driverLat=locationlogging?.Latitude
                    var driverLong=locationlogging?.Longitude

                    if (driverLat != null && driverLong != null) {
                        val driverLoc = LatLng(driverLat, driverLong)

                        val markerOptions = MarkerOptions().position(driverLoc).title("Driver")
                        mMap.addMarker(markerOptions)

                        Toast.makeText(applicationContext, "Locations accessed from the database", Toast.LENGTH_LONG).show()
                    }
                }
            }

            override fun onCancelled(error: DatabaseError) {}
        }
        databaseRef.addValueEventListener(postListener)
    }


    override fun onMapReady(googleMap: GoogleMap) {
        mMap = googleMap

        mMap.setOnMapLongClickListener { latLng ->
            Log.i(TAG, "onMapLongClickListener")
            setMapLongClick(latLng)
        }


    }

This is the function of sending coordinates to Firebase

    private fun setMapLongClick(latLng: LatLng) {

        if (latLng != null) {
            Log.e("Latitude: ", latLng.latitude.toString() + "longitude: " + latLng.longitude)
            val latlang: MutableMap<String, Double> = HashMap()
            latlang["latitude"] = latLng.latitude
            latlang["longitude"] = latLng.longitude
            mDatabase!!.child("userlocation").push().setValue(latlang)
            onStart()


        }

    }

}

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
494 views
Welcome To Ask or Share your Answers For Others

1 Answer

等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...