I am trying to figure out what is the best practice to use Mapbox on Android with tiles stored in a server.
I know the tiles are available because I can make them appear on the web.
I can use the default styles no problem mapBox.setStyle(Style.LIGHT)
but when I try to change the map style using the server tiles.
I'm creating a JsonObject
:
val mapJSONObject = JsonObject()
which is the equivalent of this:
{
"version": 8,
"sources": {
"tiles-name": {
"type": "raster",
"url": "https://myserver.com/map/c16c1u38213f0db8001b1c6e8cee5f7a/{z}/{x}/{y}?token=jfsdajfdsjkfjksh34232fsd",
"tileSize": 256
}
},
"layers": [{
"id": "ee-raster",
"type": "raster",
"source": "tiles-name",
"minzoom": 0,
"maxzoom": 22
}]
}
I've tried to use this JsonObject
by either calling it by:
map.setStyle(Style.Builder().fromJson(mapJSONObject.toString()))
or by creating a .json file via this function:
@Throws(IOException::class)
fun save(context: Context, jsonString: String) {
val rootFolder = context.getExternalFilesDir(null)
val jsonFile = File(rootFolder, "file.json")
val writer = FileWriter(jsonFile)
writer.write(jsonString)
writer.close()
}
then using that .json file via:
map.setStyle(Style.Builder().fromJson("file.json"))
When triggering the change map using:
map.setStyle(Style.Builder().fromJson(mapJSONObject.toString()))
I get the error message:
D/Mbgl-HttpRequest: [HTTP] Request with response = 404: No additional information [Style]: Failed to load source tiles-name: HTTP status code 404
When triggering the change map using:
map.setStyle(Style.Builder().fromJson("file.json"))
I get the error message:
[ParseStyle]: Failed to parse style: Invalid value. at offset 1
I have trouble debugging this, even though the error messages seem clear enough.
I have been reading through the MapBox documentation, but can't seem to find a clear example using tiles from tiles on a server.
Can anyone identify what might be the issue or point me to an example using this method?
Doesn't have to be in Kotlin, Java would be equally useful.