Anonymous
Сервер пользовательских плиток не отображается в MapBox android
Сообщение
Anonymous » 16 июл 2024, 13:37
Я впервые использую MapBox для чего-либо. Я пытаюсь повторить это в приложении для Android. Я следил за документацией MapBox, чтобы показать сервер векторных плиток от стороннего производителя. В настоящее время у меня есть собственный сервер плиток и файл json стилей, добавленный в папку ресурсов проекта, но, похоже, он у меня не работает, так как ни одна плитка не отображается в окне карты.
Вот мой деятельность:
Код: Выделить всё
class MapBoxActivity : AppCompatActivity() {
private lateinit var mapboxMap: MapboxMap
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val mapView = MapView(this)
setContentView(mapView)
mapboxMap = mapView.mapboxMap
mapboxMap.setCamera(
CameraOptions.Builder()
.zoom(12.0)
.center(Point.fromLngLat(-87.622088, 41.878781))
.build()
)
val styleJson = loadStyleFromAssets("mapbox_style.json")
if (styleJson != null) {
mapboxMap.loadStyleJson(styleJson) { style ->
updateTileSource(style)
}
}
}
private fun updateTileSource(style: Style) {
val url = "http://ts.carsprogram.org/public.rwrc_view/{z}/{x}/{y}.pbf"
// Remove existing source if it exists
if (style.styleSourceExists(SOURCE_ID)) {
style.removeStyleSource(SOURCE_ID)
}
// Add new source with updated URL
style.addSource(
vectorSource(SOURCE_ID) {
tiles(listOf(url))
minzoom(6)
maxzoom(14)
}
)
// Add or update the layer
if (!style.styleLayerExists(LAYER_ID)) {
style.addLayerBelow(
lineLayer(LAYER_ID, SOURCE_ID) {
sourceLayer(SOURCE_LAYER_ID)
lineCap(LineCap.ROUND)
lineJoin(LineJoin.ROUND)
lineOpacity(0.6)
lineColor(Expression.rgb(53.0, 175.0, 109.0))
lineWidth(2.0)
},
BELOW_LAYER_ID
)
}
}
private fun loadStyleFromAssets(fileName: String): String? {
return try {
val inputStream = assets.open(fileName)
val size = inputStream.available()
val buffer = ByteArray(size)
inputStream.read(buffer)
inputStream.close()
String(buffer, Charsets.UTF_8)
} catch (e: Exception) {
e.printStackTrace()
null
}
}
private companion object {
const val SOURCE_ID = "mapillary"
const val LAYER_ID = SOURCE_ID
const val TAG = SOURCE_ID
const val SOURCE_LAYER_ID = "sequence"
const val BELOW_LAYER_ID = "road-label-simple"
}
}
Вот мой json-файл стиля:
Код: Выделить всё
{
"id": "public.rwrc_view",
"schema": "public",
"name": "rwrc_view",
"properties": [
{
"name": "objectid",
"type": "int8",
"description": ""
},
{
"name": "road_condition",
"type": "float8",
"description": ""
},
{
"name": "color",
"type": "text",
"description": ""
},
{
"name": "segment_id",
"type": "text",
"description": ""
},
{
"name": "segment_name",
"type": "text",
"description": ""
},
{
"name": "headline",
"type": "text",
"description": ""
},
{
"name": "status",
"type": "text",
"description": ""
},
{
"name": "route_name",
"type": "text",
"description": ""
},
{
"name": "primarymp",
"type": "text",
"description": ""
},
{
"name": "secondarymp",
"type": "text",
"description": ""
},
{
"name": "primarylat",
"type": "text",
"description": ""
},
{
"name": "primarylong",
"type": "text",
"description": ""
},
{
"name": "secondarylat",
"type": "text",
"description": ""
},
{
"name": "secondarylong",
"type": "text",
"description": ""
},
{
"name": "description",
"type": "text",
"description": ""
},
{
"name": "source",
"type": "text",
"description": ""
},
{
"name": "source_link",
"type": "text",
"description": ""
},
{
"name": "report_updated",
"type": "float8",
"description": ""
},
{
"name": "report_created",
"type": "float8",
"description": ""
},
{
"name": "report_id",
"type": "text",
"description": ""
},
{
"name": "creationdate",
"type": "int8",
"description": ""
},
{
"name": "creator",
"type": "text",
"description": ""
},
{
"name": "editdate",
"type": "int8",
"description": ""
},
{
"name": "editor",
"type": "text",
"description": ""
},
{
"name": "route_rank",
"type": "float8",
"description": ""
},
{
"name": "pk",
"type": "int4",
"description": ""
}
],
"geometrytype": "Geometry",
"center": [
-95.51685333251953,
42.49988555908203
],
"bounds": [
-104.055908203125,
35.99919891357422,
-86.97779846191406,
49.000572204589844
],
"minzoom": 2,
"maxzoom": 22,
"tileurl": "http://ts.carsprogram.org/public.rwrc_view/{z}/{x}/{y}.pbf"
}
Нужно подсказать, что я здесь делаю не так. Спасибо!
Подробнее здесь:
https://stackoverflow.com/questions/787 ... ox-android
1721126271
Anonymous
Я впервые использую MapBox для чего-либо. Я пытаюсь повторить это в приложении для Android. Я следил за документацией MapBox, чтобы показать сервер векторных плиток от стороннего производителя. В настоящее время у меня есть собственный сервер плиток и файл json стилей, добавленный в папку ресурсов проекта, но, похоже, он у меня не работает, так как ни одна плитка не отображается в окне карты. Вот мой деятельность: [code]class MapBoxActivity : AppCompatActivity() { private lateinit var mapboxMap: MapboxMap override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val mapView = MapView(this) setContentView(mapView) mapboxMap = mapView.mapboxMap mapboxMap.setCamera( CameraOptions.Builder() .zoom(12.0) .center(Point.fromLngLat(-87.622088, 41.878781)) .build() ) val styleJson = loadStyleFromAssets("mapbox_style.json") if (styleJson != null) { mapboxMap.loadStyleJson(styleJson) { style -> updateTileSource(style) } } } private fun updateTileSource(style: Style) { val url = "http://ts.carsprogram.org/public.rwrc_view/{z}/{x}/{y}.pbf" // Remove existing source if it exists if (style.styleSourceExists(SOURCE_ID)) { style.removeStyleSource(SOURCE_ID) } // Add new source with updated URL style.addSource( vectorSource(SOURCE_ID) { tiles(listOf(url)) minzoom(6) maxzoom(14) } ) // Add or update the layer if (!style.styleLayerExists(LAYER_ID)) { style.addLayerBelow( lineLayer(LAYER_ID, SOURCE_ID) { sourceLayer(SOURCE_LAYER_ID) lineCap(LineCap.ROUND) lineJoin(LineJoin.ROUND) lineOpacity(0.6) lineColor(Expression.rgb(53.0, 175.0, 109.0)) lineWidth(2.0) }, BELOW_LAYER_ID ) } } private fun loadStyleFromAssets(fileName: String): String? { return try { val inputStream = assets.open(fileName) val size = inputStream.available() val buffer = ByteArray(size) inputStream.read(buffer) inputStream.close() String(buffer, Charsets.UTF_8) } catch (e: Exception) { e.printStackTrace() null } } private companion object { const val SOURCE_ID = "mapillary" const val LAYER_ID = SOURCE_ID const val TAG = SOURCE_ID const val SOURCE_LAYER_ID = "sequence" const val BELOW_LAYER_ID = "road-label-simple" } } [/code] Вот мой json-файл стиля: [code]{ "id": "public.rwrc_view", "schema": "public", "name": "rwrc_view", "properties": [ { "name": "objectid", "type": "int8", "description": "" }, { "name": "road_condition", "type": "float8", "description": "" }, { "name": "color", "type": "text", "description": "" }, { "name": "segment_id", "type": "text", "description": "" }, { "name": "segment_name", "type": "text", "description": "" }, { "name": "headline", "type": "text", "description": "" }, { "name": "status", "type": "text", "description": "" }, { "name": "route_name", "type": "text", "description": "" }, { "name": "primarymp", "type": "text", "description": "" }, { "name": "secondarymp", "type": "text", "description": "" }, { "name": "primarylat", "type": "text", "description": "" }, { "name": "primarylong", "type": "text", "description": "" }, { "name": "secondarylat", "type": "text", "description": "" }, { "name": "secondarylong", "type": "text", "description": "" }, { "name": "description", "type": "text", "description": "" }, { "name": "source", "type": "text", "description": "" }, { "name": "source_link", "type": "text", "description": "" }, { "name": "report_updated", "type": "float8", "description": "" }, { "name": "report_created", "type": "float8", "description": "" }, { "name": "report_id", "type": "text", "description": "" }, { "name": "creationdate", "type": "int8", "description": "" }, { "name": "creator", "type": "text", "description": "" }, { "name": "editdate", "type": "int8", "description": "" }, { "name": "editor", "type": "text", "description": "" }, { "name": "route_rank", "type": "float8", "description": "" }, { "name": "pk", "type": "int4", "description": "" } ], "geometrytype": "Geometry", "center": [ -95.51685333251953, 42.49988555908203 ], "bounds": [ -104.055908203125, 35.99919891357422, -86.97779846191406, 49.000572204589844 ], "minzoom": 2, "maxzoom": 22, "tileurl": "http://ts.carsprogram.org/public.rwrc_view/{z}/{x}/{y}.pbf" } [/code] Нужно подсказать, что я здесь делаю не так. Спасибо! Подробнее здесь: [url]https://stackoverflow.com/questions/78732586/custom-tiles-server-is-not-showing-in-mapbox-android[/url]