在 Kotlin 中将折线保存到 firebase?

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

} `

我想将折线发送到 Firestore。如何将代码中的折线发送到 Firestore?谁能帮忙?我的代码有一个带按钮的地图片段。这是一个距离跟踪应用程序。该应用程序使用折线绘制距离。如何将折线转换为数组。我在这里做一个位置跟踪应用程序。我想将多段线转换为数组,以便将其保存在云中。

根据官方文档,一个Polyline is not a Firestore supported data-type。所以你无法将这样的对象添加到 Firestore。

什么是折线?

它基本上是一个点列表。所以你可以做的是将所有这些点添加到 Firestore。您可以像纬度和经度一样简单地添加它们,也可以添加 GeoPoint 个对象。如果您有位置的其他详细信息,可以将它们作为文档添加到集合中,否则,您可以将它们存储在文档中的数组中。

要阅读它们,只需创建对文档的引用,遍历数组,为每个位置创建一个新的 LatLng 对象,然后将它们全部添加到折线中。