Skip to content

Commit

Permalink
Swift Playgrounds source code for #8
Browse files Browse the repository at this point in the history
  • Loading branch information
roblabs committed Sep 18, 2022
1 parent d761956 commit 0b46e98
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
35 changes: 26 additions & 9 deletions MapLibreTest.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
* The MapLibre Test Playground demonstrates basic rendering of a Mapbox style using the current MapLibre binary.
* Useful for testing the data in the`binaryTarget[]` in this Swift Package repo.
* Downloads the MapLibre `.xcframework` and creates a simple map, thereby testing the Swift Package Manager workflow.
* Add your style for basic rendering tests
* Add your style for basic rendering tests.
* Using the same style, demonstrate how to use `MGLMapSnapshotter`.
*/

import UIKit
import PlaygroundSupport
import Mapbox

// Create a map set its dimensions
let width: CGFloat = 600
let height: CGFloat = 400
let width: CGFloat = 640
let height: CGFloat = 480

let mapView = MGLMapView(frame: CGRect(x: 0, y: 0, width: width, height: height))
mapView.frame = CGRect(x: 0, y: 0, width: width, height: height)
Expand All @@ -22,16 +23,32 @@ mapView.attributionButton.isHidden = true

// enable debugging tile features
mapView.debugMask = MGLMapDebugMaskOptions(rawValue:
// Edges of tile boundaries
MGLMapDebugMaskOptions.tileBoundariesMask.rawValue +
// tile coordinate (x/y/z)
MGLMapDebugMaskOptions.tileInfoMask.rawValue
MGLMapDebugMaskOptions.collisionBoxesMask.rawValue + // Edges of glyphs and symbols
MGLMapDebugMaskOptions.timestampsMask.rawValue + // shows a timestamp indicating when it was loaded.
MGLMapDebugMaskOptions.tileBoundariesMask.rawValue + // Edges of tile boundaries
MGLMapDebugMaskOptions.tileInfoMask.rawValue // tile coordinate (x/y/z)
)

// Set Style
let styleJSON = "https://raw.githubusercontent.com/roblabs/openmaptiles-ios-demo/master/OSM2VectorTiles/styles/geography-class.GitHub.json"
var styleJSON = "https://demotiles.maplibre.org/style.json"

mapView.styleURL = URL(string: styleJSON)
mapView.setCenter(CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0), zoomLevel: 2, animated: false)
mapView.setCenter(CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0), zoomLevel: 3, animated: false)

// MGLMapSnapshotter example code
var image: UIImage?
let camera = MGLMapCamera(lookingAtCenter: CLLocationCoordinate2D(latitude: 0, longitude: 0), altitude: 100, pitch: 75, heading: 45)

let options = MGLMapSnapshotOptions(styleURL: URL(string: styleJSON), camera: camera, size: CGSize(width: width, height: height))
options.zoomLevel = mapView.zoomLevel

let snapshotter = MGLMapSnapshotter(options: options)
snapshotter.start { (snapshot, error) in
if let error = error {
fatalError(error.localizedDescription)
}

image = snapshot?.image
}

PlaygroundPage.current.liveView = mapView
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ When you download this repo there is a Swift Playground that allows you to chang

* Download this repo
* Navigate to the folder where you `clone`d, and open `Package.swift` in at least Xcode 12.
* Run Playground by choosing `Editor` > `Run Playground` or `⇧⌘⏎`
* Run Playground by choosing `Editor` > `Run Playground` or `⇧-⌘-⏎`
* See issue [maplibre-gl-native-distribution#8](https://github.com/maplibre/maplibre-gl-native-distribution/issues/8) for screenshots of the MapLibre for Swift Playgrounds in action.

0 comments on commit 0b46e98

Please sign in to comment.