Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Include Mapbox SDK in existing OS X Application #3043

Closed
tkrajacic opened this issue Nov 16, 2015 · 17 comments
Closed

Include Mapbox SDK in existing OS X Application #3043

tkrajacic opened this issue Nov 16, 2015 · 17 comments
Assignees
Labels
feature macOS Mapbox Maps SDK for macOS
Milestone

Comments

@tkrajacic
Copy link

Is it possible to easily™ include mapbox-gl in an existing OS X application.
I could build the sample app and re-use its Xcode project but that really is unwieldy.
I am using Swift and Storyboards which might pose another problem?

An SDK like the one for iOS would be nice (cocoapods or carthage would be even better) but I understand that this is not the focus right now.

Can I just link the build-products of the osx Xcode project statically and be done with it?
Kind of like with the prebuilt binaries for iOS.

Any hints or pointers?
Any help is greatly appreciated.

@incanus incanus added the macOS Mapbox Maps SDK for macOS label Nov 16, 2015
@incanus incanus changed the title Include Mapox SDK in existing OS X Application Include Mapbox SDK in existing OS X Application Nov 16, 2015
@incanus
Copy link
Contributor

incanus commented Nov 16, 2015

The big lift here @tkrajacic is that aside from iOS, Android, and Node.js, we don't have bindings atop the C++ code at all. This includes the Mac. So the Mac project (e.g. make xproj) compiles the core GL library in C++, then stands it up similar to the Linux desktop version using GLFW to create a "dumb OpenGL window" to draw in, plus takes care of basic debug keybindings.

So, short version: you should be able to create an OS X app which creates its own e.g. NSOpenGLView for GL to draw into, but also takes responsibility for swapping buffers, calls mbgl::Map::renderSync(), and otherwise interfaces directly with the C++ API (stuff like the mbgl::Map object and mbgl::LatLng structures).

It's not really in the cards right now for anything more advanced for OS X or Linux or bindings for Cocoa manipulation.

I think that's the closest I can get you, but I'll let @kkaefer add any other details if he has them.

@tkrajacic
Copy link
Author

Thanks for the great walkthrough.

I just wonder - The iOS Bindings should be 99% reusable for OS X or not?
All the frameworks except MobileCoreServices.framework are on OS X as well.

@1ec5
Copy link
Contributor

1ec5 commented Nov 17, 2015

The iOS Bindings should be 99% reusable for OS X or not?

It may be possible, with a bit of trial and error, to get it to work on OS X, but code changes would definitely be required because AppKit differs from UIKit in many ways.

@tkrajacic
Copy link
Author

So if one would translate /platform/ios to AppKit, what else would be missing to just make the iOS bindings spit out an OSX version?
The /ios directory contains the podspec and example app?

@1ec5
Copy link
Contributor

1ec5 commented Nov 17, 2015

The ios/ directory contains the example application, while include/mbgl/ios/, platform/ios/, mbgl/platform/darwin/, and platform/darwin/ contain the actual library code. (The last two use only Foundation, so they should be compatible with OS X already.) The meat of the code is in MGLMapView.m. Note our use of SMCalloutView in platform/ios/vendor/ – on OS X, it’d be better to use NSPopover.

@1ec5
Copy link
Contributor

1ec5 commented Nov 17, 2015

There’s a lot of code that’s specific to UIKit in MGLMapView, MGLUserAnnotationView, etc. I haven’t tried, but #3043 (comment) sounds like a good summary of what would be required for the bare minimum to display a draggable/zoomable map.

@1ec5
Copy link
Contributor

1ec5 commented Nov 23, 2015

@tkrajacic, please take a look at the 1ec5-osx branch I just pushed. It adds a new MGLMapView class analogous to MGLMapView for iOS, plus a pure Cocoa osxapp. It’s exceptionally rough around the edges: in particular, it tends to spin up the fan even when idle, it crashes when you hit ⌘W, and annotations and the user dot aren’t yet implemented. But if you’re interested in hacking on it, run make xproj and build the osxapp target.

To integrate this library into a separate project:

  1. Run make xpackage.
  2. Pull build/osx/pkg/static/{Headers,libMapbox.a} into your project.
  3. Add libz.tbd, libsqlite3.tbd, and libc++.tbd to the Linked Frameworks and Libraries section of the project editor.
  4. In your main XIB or storyboard, drag in a Custom View and set its custom class to MGLMapView.

Please note that OS X is not officially supported at the same level as iOS (yet?), but I hope this branch in a good enough state that you can run with it. Good luck!

@tkrajacic
Copy link
Author

Wow, you guys are absolutely awesome! Setting aside even a few minutes to work on something that is clearly not the focus at all is ... well incredible. I'll buy you all a beer should I be in the area ;)

I'll take it for a spin, but I'm swamped with other work at the moment.
Thanks again!

@tkrajacic
Copy link
Author

Had to include SystemConfiguration.framework as well and create a bridging-header since I am using Swift.
I am getting a lot of duplicate symbols error on linking though like:

duplicate symbol _uv__udp_send in:
.../libMapbox.a(udp.o)
.../libMapbox.a(libuv_la-udp.o)

Seems that make xpackage should not include both object files (there is always a libuv_la variant for the duplicates) but I'll investigate further.

@1ec5
Copy link
Contributor

1ec5 commented Nov 23, 2015

I forgot to mention that Bitcode is enabled, in case you're still using Xcode 6.x. Not sure if that's related at all to this error, though.

@tkrajacic
Copy link
Author

I am using latest Xcode 7.1.1

I thought Bitcode wasn't available for OS X targets. No setting in Build Settings

@1ec5
Copy link
Contributor

1ec5 commented Nov 23, 2015

Ah, you’re right. 😄

@1ec5
Copy link
Contributor

1ec5 commented Nov 23, 2015

I’m not getting those warnings after a full make clean && make distclean && rm -rf gyp/build/. I did forget to mention that -ObjC is required in OTHER_LD_FLAGS, however.

@tkrajacic
Copy link
Author

The cleaning before building did the trick and got rid of the duplicate symbols.
Now the view stays black though and the console displays loading style failed: HTTP status code 401.
ATS is disabled.
I tried a local URL, and a style URL from my mapbox account (error: Error parsing style JSON at 0: Invalid value), but neither worked so far.
Where does the MGLMapView look for a style?

@1ec5
Copy link
Contributor

1ec5 commented Nov 23, 2015

MGLMapView supports style from various locations. If you designed a style in Mapbox Studio, the “style ID” that begins with mapbox:// should work. If you want to load a local style, a relative URL or one beginning with asset:// would be relative to your application’s Resources folder. (Drag the JSON file into your project’s Supporting Files group.)

If you’re using a Mapbox-hosted style, make sure your access token is set, either in the MGLMapboxAccessToken key in the Info.plist file or by setting -[MGLAccountManager setAccessToken:] before setting the style.

@tkrajacic
Copy link
Author

Yeah I thought the access token still had to be set via environment variable. Setting it via the manager works now.

@1ec5 1ec5 self-assigned this Dec 5, 2015
@1ec5
Copy link
Contributor

1ec5 commented Dec 14, 2015

#3135 landed.

@1ec5 1ec5 closed this as completed Dec 14, 2015
@1ec5 1ec5 removed the in progress label Dec 14, 2015
@1ec5 1ec5 modified the milestone: osx-v0.1.0 May 10, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature macOS Mapbox Maps SDK for macOS
Projects
None yet
Development

No branches or pull requests

3 participants