firebasegoogle-mapskotlingoogle-cloud-firestoregoogle-polyline

Saving polylines to firebase in Kotlin?


override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    val mapFragment = childFragmentManager.findFragmentById(R.id.map) as SupportMapFragment?
    mapFragment?.getMapAsync(this)
}

@SuppressLint("MissingPermission", "PotentialBehaviorOverride")
override fun onMapReady(googleMap: GoogleMap?) {
    map = googleMap!!
    map.isMyLocationEnabled = true
    map.setOnMyLocationButtonClickListener(this)
    map.setOnMarkerClickListener(this)
    map.uiSettings.apply {
        isZoomControlsEnabled = false
        isZoomGesturesEnabled = false
        isRotateGesturesEnabled = false
        isTiltGesturesEnabled = false
        isCompassEnabled = false
        isScrollGesturesEnabled = false
    }
    observeTrackerService()
}

private fun observeTrackerService() {
    TrackerService.locationList.observe(viewLifecycleOwner, {
        if (it != null) {
            locationList = it
            if (locationList.size > 1) {
                binding.stopButton.enable()
            }
            drawPolyline()
            followPolyline()
        }
    })
    TrackerService.started.observe(viewLifecycleOwner, {
        started.value = it
    })
    TrackerService.startTime.observe(viewLifecycleOwner, {
        startTime = it
    })
    TrackerService.stopTime.observe(viewLifecycleOwner, {
        stopTime = it
        if (stopTime != 0L) {
            showBiggerPicture()
            displayResults()
        }
    })
}

private fun drawPolyline() {
    val polyline = map.addPolyline(
            PolylineOptions().apply {
                width(10f)
                color(Color.BLUE)
                jointType(JointType.ROUND)
                startCap(ButtCap())
                endCap(ButtCap())
                addAll(locationList)
            }
    )
    polylineList.add(polyline)
}

private fun followPolyline() {
    if (locationList.isNotEmpty()) {
        map.animateCamera(
                (CameraUpdateFactory.newCameraPosition(
                        setCameraPosition(
                                locationList.last()
                        )
                )), 1000, null)
    }
}






}

private fun showBiggerPicture() {
    val bounds = LatLngBounds.Builder()
    for (location in locationList) {
        bounds.include(location)
    }
    map.animateCamera(
            CameraUpdateFactory.newLatLngBounds(
                    bounds.build(), 100
            ), 2000, null
    )
    addMarker(locationList.first())
    addMarker(locationList.last())
}

private fun addMarker(position: LatLng){
    val marker = map.addMarker(MarkerOptions().position(position))
    markerList.add(marker)
}

private fun displayResults() {
    val result = Result(
            calculateTheDistance(locationList),
            calculateElapsedTime(startTime, stopTime)
    )



    lifecycleScope.launch {
        delay(2500)
        val directions = MapsFragmentDirections.actionMapsFragmentToResultFragment(result)
       findNavController().navigate(directions)
        binding.startButton.apply {
            hide()
            enable()
        }
        binding.stopButton.hide()
        binding.resetButton.show()
    

} `

I would like to send the polylines to Firestore. How to send the polylines in code to Firestore? Can anyone help? My code has a map fragment with buttons. This is a distance tracking app. The app plots the distance using polylines. How to convert polylines to arrays. Here I am doing a location tracking app. I want to convert the polylines to arrays so that I could save it cloud.


Solution

  • According to the official documentation, a Polyline is not a Firestore supported data-type. So there is no way you can add such an object to Firestore.

    What's a polyline?

    It's basically a list of points. So what you can do instead is to add all these points to Firestore. You can add them as simple as latitude and longitude or as GeoPoint objects. If you have additional details for the locations, you can add them as documents in a collection, otherwise, you can store them in an array within a document.

    To read them, simply create a reference to the document, loop through the array, create a new LatLng object of each location, and add all of them to the polyline.