mapbox-developer-group / Mapbox-Tech-Q-A

在提问之前请自行查看文档哦,养成好习惯:https://docs.mapbox.com/
32 stars 1 forks source link

Android 端setStyle时,使用Style.Builder().fromUri(),报“loading style failed: Could not read asset”错 #77

Closed littleneil closed 3 years ago

littleneil commented 3 years ago

@canglaoshidaidui

我遇到的问题

我的项目需要用天地图作为地图,我看到Api中可以通过设置style实现,我在项目的assets目录下创建了一个名为“tdt_map.json”的json文件,通过调用style的setStyle的Style.Builder().fromUri()方法实现,运行后,地图没加载出来,报错“loading style failed: Could not read asset”

我遇到的问题截图

image image

该问题的类别

Android

canglaoshidaidui commented 3 years ago

mapboxMap.setStyle("asset://style.json") 这么给好使

canglaoshidaidui commented 3 years ago

assets目录 asset:// 别加s。可以看看 Style类里的 说明

littleneil commented 3 years ago

mapboxMap.setStyle("asset://style.json") 这么给好使

我按照上面的方法试了,不报错了,但是地图页面是一片漆黑;请问是不是我json文件写的有问题

canglaoshidaidui commented 3 years ago

{ "updatedAt": "2019-04-25T08:59:14.157Z", "createdAt": "2019-03-28T04:15:15.893Z", "owner": "zy", "zoom": 2.57217230129095, "sources": { "satellite": { "type": "raster", "tiles": [ "http://mt1.google.cn/vt/lyrs=s@113&hl=nl&x={x}&y={y}&z={z}&s=" ], "tileSize": 256 }, "dt_zzdk": { "type": "vector", "tiles": [ "http://127.0.0.1:8080/api/v1/tile/zy/dt_zzdk/{z}/{x}/{y}.pbf" ] } }, "name": "数据集底图", "sprite": "http://127.0.0.1:9021/api/v1/sprites/zy/r1vbPWb_7/sprite", "glyphs": "http://127.0.0.1:9021/api/v1/fonts/zy/{fontstack}/{range}.pbf", "layers": [ { "id": "背景", "type": "background", "paint": { "background-color": "rgb(4,7,14)" }, "layout": { "visibility": "visible" } }, { "id": "卫星影像", "type": "raster", "source": "satellite", "layout": { "visibility": "visible" } }, { "id": "测试1", "source": "dt_zzdk", "type": "line", "minzoom": 8, "maxzoom": 18, "layout": { "visibility": "visible" }, "paint": { "line-color": "#fc0000", "line-opacity": 1, "line-width": 1.5 }, "source-layer": "dt_zzdk", "metadata": { "mapbox:group": "3ad046ebc0f3b" } } ], "transition": {}, "pitch": 0, "bearing": 0, "center": [ 100.32695382043653, 38.849388042235574 ], "version": 8, "tags": [], "type": "normal", "scope": "public", "style_id": "rke2I9pF_E" }

canglaoshidaidui commented 3 years ago

应该是你这个地址有问题URL_VECTOR_2000,mapbox没法给你这个地址 添加 z,x,y 请求参数

canglaoshidaidui commented 3 years ago

把你的样式贴出来把。图片不好找原因

littleneil commented 3 years ago

把你的样式贴出来把。图片不好找原因

{ "version": 8, "sources": { "URL_VECTOR_2000": { "tiles": ["https://t0.tianditu.gov.cn/vec_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=vec&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=7eb1b8992d1314342be0ff80408b8395"], "type": "raster", "tileSize": 256, "maxzoom": 23 }, "URL_VECTOR_ANNOTATION_CHINESE_2000": { "tiles": ["https://t0.tianditu.gov.cn/cva_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cva&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=7eb1b8992d1314342be0ff80408b8395"], "type": "raster", "tileSize": 256, "maxzoom": 23 } }, "layers": [ { "id": "URL_VECTOR_2000", "type": "raster", "source": "URL_VECTOR_2000", "layout": { "visibility": "visible" } }, { "id": "URL_VECTOR_ANNOTATION_CHINESE_2000", "type": "raster", "source": "URL_VECTOR_ANNOTATION_CHINESE_2000", "layout": { "visibility": "visible" } } ] }

canglaoshidaidui commented 3 years ago

老哥 你把你的key 重新申请一个,你的这个key是网页的应该。移动端用不了。 你换成我的这个 tk=1d63495140638de0bfd2273f64b67371 就好使了

littleneil commented 3 years ago

老哥 你把你的key 重新申请一个,你的这个key是网页的应该。移动端用不了。 你换成我的这个 tk=1d63495140638de0bfd2273f64b67371 就好使了

谢谢,按照这个key已经可以显示了

littleneil commented 3 years ago

我还想问一点,如何实现跟mapbox自带的底图一样,放大在最大缩放级别后,再放大地图是跟图片缩放一样拉大,不会显示一片空白,我看上面您提供的json文件也可以实现这种效果