简体   繁体   中英

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". 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. 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

Am I setting it up wrong? Do I need to enable permissions or something in the Mapbox admin UI?

Update 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> . When I try that, I got the following crash on thread "Map":

malloc: * mach_vm_map(size=685248512) failed (error code=3)
* error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
libc++abi.dylib: terminating with uncaught exception of type 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 . The new Mapbox Studio (that's currently in private beta) is our tool for creating GL-compatible maps.

Loading a GL style with Mapbox iOS SDK is done like so:

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.

(In any case, a malformed style URL should not crash and we'll make that more robust .)

I created a new Access Token and use 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. It should work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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