简体   繁体   English

如何使用 Mapbox iOS SDK 显示来自 Mapbox 项目的地图?

[英]How do I show a map from a Mapbox Project with the Mapbox iOS SDK?

I've created a Project (Map?) at https://www.mapbox.com/editor , which has given me a "Map ID".我在https://www.mapbox.com/editor创建了一个项目(地图?),它给了我一个“地图 ID”。 After a bunch of googling and searching, I finally worked out that (I think?) you're supposed to embed this Map ID within a URL and pass it in to the -[MGLMapView initWithFrame:styleURL:] initializer method.经过一番谷歌搜索和搜索,我终于确定(我认为?)您应该将此地图 ID 嵌入到 URL 中并将其传递给-[MGLMapView initWithFrame:styleURL:]初始值设定项方法。 I've tried the following:我尝试了以下方法:

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

But I get the following in the debugging console:但是我在调​​试控制台中得到以下信息:

[ERROR] {Map}[Setup]: loading style failed: HTTP status code 401 [ERROR] {Map}[Setup]:加载样式失败:HTTP 状态码 401

Am I setting it up wrong?我设置错了吗? Do I need to enable permissions or something in the Mapbox admin UI?我是否需要在 Mapbox 管理 UI 中启用权限或其他内容?

Update 1更新 1

I just discovered that the online docs at https://www.mapbox.com/ios-sdk/api/Classes/MGLMapView.html say that the URL should be in the form mapbox://styles/<user>/<style> .我刚刚发现https://www.mapbox.com/ios-sdk/api/Classes/MGLMapView.html上的在线文档说 URL 的格式应该是mapbox://styles/<user>/<style> . When I try that, I got the following crash on thread "Map":当我尝试这样做时,我在线程“Map”上遇到了以下崩溃:

malloc: * mach_vm_map(size=685248512) failed (error code=3) malloc: * mach_vm_map(size=685248512) 失败(错误代码=3)
* error: can't allocate region *错误:无法分配区域
*** set a breakpoint in malloc_error_break to debug *** 在 malloc_error_break 中设置断点进行调试
libc++abi.dylib: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc libc++abi.dylib:以未捕获的 std::bad_alloc 类型异常终止:std::bad_alloc

This is the relevant part of the stack trace:这是堆栈跟踪的相关部分:

#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

The fundamental issue here is that raster maps created with the editor are not compatible with GL styles .这里的根本问题是用编辑器创建的光栅地图与GL 样式不兼容。 The new Mapbox Studio (that's currently in private beta) is our tool for creating GL-compatible maps.新的 Mapbox Studio (目前处于私人测试阶段)是我们用于创建 GL 兼容地图的工具。

Loading a GL style with Mapbox iOS SDK is done like so:使用 Mapbox iOS SDK 加载 GL 样式是这样完成的:

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

Where mapbox://styles/mapbox/streets-v8 is a default style that follows the mapbox://styles/user/styleHash pattern.其中mapbox://styles/mapbox/streets-v8是遵循mapbox://styles/user/styleHash模式的默认样式。

(In any case, a malformed style URL should not crash and we'll make that more robust .) (在任何情况下,格式错误的样式 URL 都不应该崩溃,我们将使其更加健壮。)

I created a new Access Token and use info.plist .我创建了一个新的访问令牌并使用info.plist Now it's working fine.现在它工作正常。

The order somehow matters.顺序在某种程度上很重要。 Make sure put this parameters in info.plist "MGLMapboxAccessToken" with new generated token and rebuild the project.确保使用新生成的令牌将此参数放入 info.plist "MGLMapboxAccessToken" 并重建项目。 It should work它应该工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM