【问题标题】:How do I show a map from a Mapbox Project with the Mapbox iOS SDK?如何使用 Mapbox iOS SDK 显示来自 Mapbox 项目的地图?
【发布时间】:2016-01-18 14:17:47
【问题描述】:

我在https://www.mapbox.com/editor 创建了一个项目(地图?),它给了我一个“地图 ID”。经过一番谷歌搜索和搜索,我终于知道(我认为?)您应该将此地图 ID 嵌入到 URL 中,并将其传递给 -[MGLMapView initWithFrame:styleURL:] 初始化方法。我尝试了以下方法:

NSURL *styleURL = [NSURL URLWithString:@"mapbox://my_username.abcdef123455"];
self.mapView = [[MGLMapView alloc] initWithFrame:self.view.bounds styleURL:styleURL];

但我在调试控制台中得到以下信息:

[ERROR] {Map}[Setup]:加载样式失败:HTTP 状态码 401

我设置错了吗?我是否需要在 Mapbox 管理 UI 中启用权限或其他功能?

更新 1

我刚刚发现 https://www.mapbox.com/ios-sdk/api/Classes/MGLMapView.html 的在线文档说 URL 应该采用 mapbox://styles/<user>/<style> 的形式。当我尝试这样做时,我在线程“Map”上遇到了以下崩溃:

malloc: * mach_vm_map(size=685248512) 失败(错误代码=3)
*
错误:无法分配区域
*** 在 malloc_error_break 中设置断点进行调试
libc++abi.dylib:以 std::bad_alloc 类型的未捕获异常终止:std::bad_alloc

这是堆栈跟踪的相关部分:

#8  0x32ea286a in operator new(unsigned long) ()
#9  0x32ea6546 in std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__init(char const*, unsigned long) ()
#10 0x32ea658e in std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::basic_string(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) ()
#11 0x00265390 in mbgl::util::mapbox::normalizeStyleURL(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) ()
#12 0x0022242e in mbgl::DefaultFileSource::request(mbgl::Resource const&, uv_loop_s*, std::__1::function<void (mbgl::Response const&)>) ()
#13 0x001ecf34 in mbgl::MapContext::setStyleURL(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) ()
#14 0x001ea85a in mbgl::util::RunLoop::Invoker<auto mbgl::util::Thread<mbgl::MapContext>::bind<void (mbgl::MapContext::*)(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)>(void (mbgl::MapContext::*)(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&))::'lambda'(void (mbgl::MapContext::*&&)(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)), std::__1::tuple<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >::operator()() ()
#15 0x00268662 in mbgl::util::RunLoop::process() ()
#16 0x002ad15a in uv__async_event at /Users/kkaefer/Code/mason/libuv-0.10.28/mason_packages/.build/libuv-0.10.28/src/unix/async.c:80

【问题讨论】:

    标签: ios mapbox


    【解决方案1】:

    这里的根本问题是使用the editor 创建的栅格地图与GL styles 不兼容。 new Mapbox Studio(目前处于私人测试阶段)是我们创建 GL 兼容地图的工具。

    使用 Mapbox iOS SDK 加载 GL 样式如下:

    NSURL *styleURL = [NSURL URLWithString:@"mapbox://styles/mapbox/streets-v8"];    
    self.mapView = [[MGLMapView alloc] initWithFrame:self.view.bounds styleURL:styleURL];
    

    其中mapbox://styles/mapbox/streets-v8 是遵循mapbox://styles/user/styleHash 模式的默认样式。

    (无论如何,格式错误的 URL 不应崩溃和we'll make that more robust。)

    【讨论】:

    • 要加载栅格地图,首先将其包装成 GL 样式:mapbox.com/ios-sdk/examples/raster-styles
    • 您是否必须将文件上传到您自己的服务器才能在 Mapbox iOS SDK 中查看 Mapbox Editor 地图,您不能将 JSON 嵌入您的应用程序包中?
    • 没错,目前无法加载本地样式。
    【解决方案2】:

    顺序很重要。确保将此参数与新生成的令牌一起放入 info.plist "MGLMapboxAccessToken" 并重建项目。它应该工作

    【讨论】:

      【解决方案3】:

      我创建了一个新的访问令牌并使用info.plist。现在它工作正常。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多