簡體   English   中英

如何使用 Mapbox iOS SDK 顯示來自 Mapbox 項目的地圖?

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

我在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

這里的根本問題是用編輯器創建的光柵地圖與GL 樣式不兼容。 新的 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 都不應該崩潰,我們將使其更加健壯。)

我創建了一個新的訪問令牌並使用info.plist 現在它工作正常。

順序在某種程度上很重要。 確保使用新生成的令牌將此參數放入 info.plist "MGLMapboxAccessToken" 並重建項目。 它應該工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM