CartoDB / mobile-sdk

CARTO Mobile SDK core project
https://carto.com/docs/carto-engine/mobile-sdk/
BSD 3-Clause "New" or "Revised" License
184 stars 65 forks source link

OSM not loading in Android #527

Closed andrey-serdyuk closed 1 year ago

andrey-serdyuk commented 1 year ago

Hey. My native(Kotlin) Android app doesn't loads OSM tiles. I'm receiving Bad request logs when I'm trying to load it. All other tiles like Google and Bing are working fine but, OSM is not.

Here is my extention of HTTPTileDataSource

class MapTileDataSource(model: MapTile) : HTTPTileDataSource(1, 19, model.tile.replace("[lc]", AppSettings.locale)) {
    init {
        subdomains = model.vectors
    }
}

my map tile based on PersistentCacheTileDataSource class MapTileCache(model: MapTile) : PersistentCacheTileDataSource(MapTileDataSource(model), "cached_${model.value}_${AppSettings.locale}_tile.db")

TileModel

enum class MapTileDemo(val value: String) {
    Google("tile_google"),
    GoogleHyb("tile_google_hyb"),
    Osm("tile_osm"),
    Bing("tile_bing"),
    BingSat("tile_bing_sat"),
    BingHyb("tile_bing_hyb");

    private val subDomains
        get() = when (this) {
            Osm -> arrayOf("a", "b", "c")
            else -> arrayOf("0", "1", "2", "3")
        }

    val vectors
        get() = StringVector(subDomains.size.toLong()).apply {
            subDomains.forEach { add(it) }
        }

    val tile
        get() = when (this) {
            Google -> "https://mt{s}.google.com/vt/lyrs=m@167000000&hl=[lc]&x={x}&y={y}&z={zoom}"
            GoogleHyb -> "https://mt{s}.google.com/vt/lyrs=y@167000000&hl=[lc]&x={x}&y={y}&z={zoom}"
            Osm -> "https://{s}.tile.openstreetmap.org/{zoom}/{x}/{y}.png"
            Bing -> "https://ecn.t{s}.tiles.virtualearth.net/tiles/r{quadkey}.jpeg?g=733&mkt=en-US"
            BingSat -> "https://ecn.t{s}.tiles.virtualearth.net/tiles/a{quadkey}.jpeg?g=733&mkt=en-US"
            BingHyb -> "https://ecn.t{s}.tiles.virtualearth.net/tiles/h{quadkey}.jpeg?g=733&mkt=en-US"
        }
}

MapTileDataSource receives a MapTile instance and passes his tile property value as URL to HTTPTileDataSource.

Generated links are loading tiles when I open them in a browser but they don't loading up in the app. Here are some logs:

carto-mobile-sdk: HTTPClient::makeRequest: Bad status code: 403, URL: https://c.tile.openstreetmap.org/9/305/205.png carto-mobile-sdk: HTTPTileDataSource::loadTile: Failed to load https://c.tile.openstreetmap.org/9/305/205.png

Thanks for a help in advance.

andrey-serdyuk commented 1 year ago
class MapTileDataSource(model: MapTile) : HTTPTileDataSource(1, 19, model.tile.replace("[lc]", AppSettings.locale)) {
    init {
        subdomains = model.vectors
        httpHeaders = StringMap().apply {
            set("User-Agent", "PostmanRuntime/7.30.0")
            set("Accept", "*/*")
            set("Cache-Control", "no-cache")
            set("Accept-Encoding", "gzip, deflate, br")
            set("Connection", "keep-alive")
        }
    }
}

Http User-Agent has solved the issue.