add more optimalization and speed mareker

This commit is contained in:
2026-02-13 12:28:19 +01:00
parent 67fea51949
commit 0e2208d104
4 changed files with 109 additions and 48 deletions
@@ -13,6 +13,7 @@ import androidx.compose.ui.unit.dp
import kotlinx.serialization.Serializable
import okhttp3.OkHttpClient
import okhttp3.Request
import ovh.plrapps.mapcompose.api.addClusterer
import ovh.plrapps.mapcompose.api.addMarker
import ovh.plrapps.mapcompose.api.removeMarker
import ovh.plrapps.mapcompose.ui.state.markers.model.RenderingStrategy
@@ -81,12 +82,13 @@ val gtfshttpClient = OkHttpClient.Builder()
private val GTFSMainHandler = Handler(Looper.getMainLooper())
private var GTFSTrackingThread: Thread? = null
private var GTFSIsTracking = false
private var GTFSIsInitialized = false // Prevent duplicate initialization
private fun fetchGTFSPosition(gtfsUrl: URL,api_key: String, callee: Activity) {
try {
val request = Request.Builder()
.url("${gtfsUrl.protocol}://${gtfsUrl.host}${"/"}")
.url("${gtfsUrl.protocol}://${gtfsUrl.host}${"/"}.pd")
.header("User-Agent", "${BuildConfig.APPLICATION_ID}/${BuildConfig.VERSION_NAME} (Android)")
.build()
@@ -116,7 +118,7 @@ fun startGTFSTracking(url: String, api_key: String, callee: Activity) {
GTFSTrackingThread = Thread {
val gtfsUrl = URL(url);
try {
if(gtfsRoutes.isEmpty()) {
if(gtfsRoutes.isEmpty() && !GTFSIsInitialized) {
GTFSMainHandler.post {
Toast.makeText(callee, "Fetching routes...", Toast.LENGTH_SHORT).show()}
@@ -170,23 +172,49 @@ fun startGTFSTracking(url: String, api_key: String, callee: Activity) {
offset += messageParsed.features.size;
}while(messageParsed.features.isNotEmpty())
}
// put all stops on the map
// put all stops on the map in batches to prevent ANR
GTFSMainHandler.post {
Toast.makeText(callee, "Drawing stops...", Toast.LENGTH_SHORT).show()}
for (stop in gtfsStops!!.features) {
if (stop.properties.parent_station == null) {
mapState.addMarker(stop.properties.stop_id, x = longitudeToXNormalized(stop.geometry.coordinates[0]), y = latitudeToYNormalized(stop.geometry.coordinates[1]), renderingStrategy = RenderingStrategy.LazyLoading("default")) {
Icon(
painter = painterResource(id = R.drawable.outline_directions_bus_24),
contentDescription = null,
modifier = Modifier.size(12.dp),
tint = Color(0xFF0000FF)
)
}}
val stopsToAdd = gtfsStops!!.features.filter { it.properties.parent_station == null }
val batchSize = 50 // Process 50 markers at a time
val totalStops = stopsToAdd.size
var processed = 0
for (batch in stopsToAdd.chunked(batchSize)) {
if (!GTFSIsTracking) break // Stop if tracking was cancelled
GTFSMainHandler.post {
for (stop in batch) {
try {
mapState.addMarker(
stop.properties.stop_id,
x = longitudeToXNormalized(stop.geometry.coordinates[0]),
y = latitudeToYNormalized(stop.geometry.coordinates[1]),
renderingStrategy = RenderingStrategy.LazyLoading("default")
) {
Icon(
painter = painterResource(id = R.drawable.outline_directions_bus_24),
contentDescription = null,
modifier = Modifier.size(12.dp),
tint = Color(0xFF0000FF)
)
}
} catch (e: Exception) {
// Skip markers that fail to add
}
}
}
processed += batch.size
// Small delay between batches to let UI breathe
Thread.sleep(50)
}
GTFSMainHandler.post {
Toast.makeText(callee, "Done drawing ${gtfsStops.features.size} stops", Toast.LENGTH_SHORT).show()}
Toast.makeText(callee, "Done drawing ${totalStops} stops", Toast.LENGTH_SHORT).show()}
GTFSIsInitialized = true // Mark as initialized to prevent duplicate additions
} catch(e: Exception) {
GTFSMainHandler.post {
@@ -208,6 +236,7 @@ fun startGTFSTracking(url: String, api_key: String, callee: Activity) {
fun stopGTFSTracking() {
GTFSIsTracking = false
GTFSIsInitialized = false
for (stop in gtfsStops!!.features) {
if (stop.properties.parent_station == null) {
mapState.removeMarker(stop.properties.stop_id);