Search code examples
androidkotlinmapboxmapbox-android

Mapbox did not draw Layer when user's moving


Im making a running app and using Sdk mapbox. The requirement is when user moves, I need to draw a layer from the beginning location to the latest one,

I tried this way but it didn’t work

 class LocationChangeListeningActivityLocationCallback internal constructor(activity: MapBoxActivity) : LocationEngineCallback<LocationEngineResult> {

        private val activityWeakReference: WeakReference<MapBoxActivity> = WeakReference(activity)
        override fun onSuccess(result: LocationEngineResult) {
            val activity = activityWeakReference.get()
            if (activity != null) {
                val location = result.lastLocation ?: return
                if (result.lastLocation != null) {
                    activity.mapBoxMap.locationComponent.forceLocationUpdate(result.lastLocation)
                    activity.listPoint.add(Point.fromLngLat(location.latitude, location.longitude))
                    activity.trackingLine()
                }
            }
        }

        override fun onFailure(exception: Exception) {
            val activity = activityWeakReference.get()
            if (activity != null) {
                Toast.makeText(activity, exception.localizedMessage, Toast.LENGTH_SHORT).show();
            }
        }
    }

var listPoint: ArrayList<Point> = ArrayList()
private var geoJson: GeoJsonSource = GeoJsonSource(LINE_SOURCE_ID)

fun trackingLine() {
    mapBoxMap.getStyle {
        geoJson.setGeoJson((Feature.fromGeometry(LineString.fromLngLats(listPoint))))
        LogCat.d("Set new location in geoJSON")
    }
}

private fun initDotLinePath(loadedMapStyle: Style) {
    loadedMapStyle.addSource(geoJson)
    loadedMapStyle.addLayerBelow(LineLayer(LAYER_ID_3, LINE_SOURCE_ID).withProperties(
        PropertyFactory.lineColor(Color.parseColor("#F13C6E")),
        PropertyFactory.lineCap(Property.LINE_CAP_ROUND),
        PropertyFactory.lineJoin(Property.LINE_JOIN_ROUND),
        PropertyFactory.lineWidth(4f)), "road-label")
}

Please show me my mistakes. Thanks a lot


Solution

  • activity.listPoint.add(Point.fromLngLat(location.latitude, location.longitude))

    Your coordinates are in the wrong order.