-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Upgrade to Mapbox GL–based Mapbox Static API #54
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
ccb1d89
Split Snapshot.swift by class
1ec5 3c87b52
Upgraded to GL-based Static API
1ec5 0cc8c09
Make GeoJSON initializer throwable
1ec5 2fd31c3
Re-typed apiEndpoint as URL
1ec5 dc8d6d7
JSON object values are Any, not AnyObject
1ec5 5a1975f
Rewrote classic snapshot tests
1ec5 892850e
Made SnapshotCamera initializer public
1ec5 a628377
Fixed example applications
1ec5 5a42efc
Updated readme and playgrounds
1ec5 cc91770
styles:tiles scope required for GL snapshots
1ec5 e8e4457
Simplified zoom test
1ec5 bc81dc3
Auto-fitting for GL API
1ec5 88cee66
Added SnapshotOptions tests
1ec5 346993d
Fixed pitch without heading
1ec5 a95cc20
Test auto, logo, attribution options
1ec5 ec4956d
Fixed auto description in macOS playground
1ec5 09a812a
Updated readme screenshots
1ec5 41ac689
Corrected image width
1ec5 53a1660
Added altitude to SnapshotCamera
1ec5 5ccc930
Clarified documentation
1ec5 49ec1d0
Updated example access token
1ec5 189229b
Merge branch 'master' into 1ec5-gl-9
1ec5 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
#if os(OSX) | ||
import Cocoa | ||
#elseif os(watchOS) | ||
import WatchKit | ||
#else | ||
import UIKit | ||
#endif | ||
|
||
/** | ||
A structure that determines what a snapshot depicts and how it is formatted. A classic snapshot is made by compositing one or more [tile sets](https://www.mapbox.com/help/define-tileset/) with optional overlays using the [classic Mapbox Static API](https://www.mapbox.com/api-documentation/?language=Swift#static-classic). | ||
|
||
Typically, you use a `ClassicSnapshotOptions` object to generate a snapshot of a [raster tile set](https://www.mapbox.com/help/define-tileset/#raster-tilesets). If you use `ClassicSnapshotOptions` to display a [vector tile set](https://www.mapbox.com/help/define-tileset/#vector-tilesets), the snapshot image will depict a wireframe representation of the tile set. To generate a static, styled image of a vector tile set, use a `SnapshotOptions` object. | ||
*/ | ||
@objc(MBClassicSnapshotOptions) | ||
open class ClassicSnapshotOptions: NSObject, SnapshotOptionsProtocol { | ||
/** | ||
An image format supported by the classic Static API. | ||
*/ | ||
@objc(MBSnapshotFormat) | ||
public enum Format: Int, CustomStringConvertible { | ||
/// True-color Portable Network Graphics format. | ||
case png | ||
/// 32-color color-indexed Portable Network Graphics format. | ||
case png32 | ||
/// 64-color color-indexed Portable Network Graphics format. | ||
case png64 | ||
/// 128-color color-indexed Portable Network Graphics format. | ||
case png128 | ||
/// 256-color color-indexed Portable Network Graphics format. | ||
case png256 | ||
/// JPEG format at default quality. | ||
case jpeg | ||
/// JPEG format at 70% quality. | ||
case jpeg70 | ||
/// JPEG format at 80% quality. | ||
case jpeg80 | ||
/// JPEG format at 90% quality. | ||
case jpeg90 | ||
|
||
public var description: String { | ||
switch self { | ||
case .png: | ||
return "png" | ||
case .png32: | ||
return "png32" | ||
case .png64: | ||
return "png64" | ||
case .png128: | ||
return "png128" | ||
case .png256: | ||
return "png256" | ||
case .jpeg: | ||
return "jpg" | ||
case .jpeg70: | ||
return "jpg70" | ||
case .jpeg80: | ||
return "jpg80" | ||
case .jpeg90: | ||
return "jpg90" | ||
} | ||
} | ||
} | ||
|
||
// MARK: Configuring the Map Data | ||
|
||
/** | ||
An array of [map identifiers](https://www.mapbox.com/help/define-map-id/) of the form `username.id`, identifying the [tile sets](https://www.mapbox.com/help/define-tileset/) to display in the snapshot. This array may not be empty. | ||
|
||
The order of the map identifiers in the array reflects their visible order in the snapshot, with the tile set identified at index 0 being the backmost tile set. | ||
*/ | ||
open var mapIdentifiers: [String] | ||
|
||
/** | ||
An array of overlays to draw atop the map. | ||
|
||
The order in which the overlays are drawn on the map is undefined. | ||
*/ | ||
open var overlays: [Overlay] = [] | ||
|
||
/** | ||
The geographic coordinate at the center of the snapshot. | ||
|
||
If the value of this property is `nil`, the `zoomLevel` property is ignored and a center coordinate and zoom level are automatically chosen to fit any overlays specified in the `overlays` property. If the `overlays` property is also empty, the behavior is undefined. | ||
|
||
The default value of this property is `nil`. | ||
*/ | ||
open var centerCoordinate: CLLocationCoordinate2D? | ||
|
||
/** | ||
The zoom level of the snapshot. | ||
|
||
In addition to affecting the visual size and detail of features on the map, the zoom level may affect style properties that depend on the zoom level. | ||
|
||
`ClassicSnapshotOptions` zoom levels differ from `SnapshotCamera` zoom levels. At zoom level 0, the entire world map is 256 points wide and 256 points tall; at zoom level 1, it is 512×512 points; at zoom level 2, it is 1,024×1,024 points; and so on. | ||
*/ | ||
open var zoomLevel: Int? | ||
|
||
// MARK: Configuring the Image Output | ||
|
||
/** | ||
The format of the image to output. | ||
|
||
The default value of this property is `SnapshotOptions.Format.png`, causing the image to be output in true-color Portable Network Graphics format. | ||
*/ | ||
open var format: Format = .png | ||
|
||
/** | ||
The logical size of the image to output, measured in points. | ||
*/ | ||
open var size: CGSize | ||
|
||
#if os(OSX) | ||
/** | ||
The scale factor of the image. | ||
|
||
If you multiply the logical size of the image (stored in the `size` property) by the value in this property, you get the dimensions of the image in pixels. | ||
|
||
The default value of this property matches the natural scale factor associated with the main screen. However, only images with a scale factor of 1.0 or 2.0 are ever returned by the classic Static API, so a scale factor of 1.0 of less results in a 1× (standard-resolution) image, while a scale factor greater than 1.0 results in a 2× (high-resolution or Retina) image. | ||
*/ | ||
open var scale: CGFloat = NSScreen.main()?.backingScaleFactor ?? 1 | ||
#elseif os(watchOS) | ||
/** | ||
The scale factor of the image. | ||
|
||
If you multiply the logical size of the image (stored in the `size` property) by the value in this property, you get the dimensions of the image in pixels. | ||
|
||
The default value of this property matches the natural scale factor associated with the screen. Images with a scale factor of 1.0 or 2.0 are ever returned by the classic Static API, so a scale factor of 1.0 of less results in a 1× (standard-resolution) image, while a scale factor greater than 1.0 results in a 2× (high-resolution or Retina) image. | ||
*/ | ||
open var scale: CGFloat = WKInterfaceDevice.current().screenScale | ||
#else | ||
/** | ||
The scale factor of the image. | ||
|
||
If you multiply the logical size of the image (stored in the `size` property) by the value in this property, you get the dimensions of the image in pixels. | ||
|
||
The default value of this property matches the natural scale factor associated with the main screen. However, only images with a scale factor of 1.0 or 2.0 are ever returned by the classic Static API, so a scale factor of 1.0 of less results in a 1× (standard-resolution) image, while a scale factor greater than 1.0 results in a 2× (high-resolution or Retina) image. | ||
*/ | ||
open var scale: CGFloat = UIScreen.main.scale | ||
#endif | ||
|
||
/** | ||
Initializes a snapshot options instance that causes a snapshotter object to automatically choose a center coordinate and zoom level that fits any overlays. | ||
|
||
After initializing a snapshot options instance with this initializer, set the `overlays` property to specify the overlays to fit the snapshot to. | ||
|
||
- parameter mapIdentifiers: An array of [map identifiers](https://www.mapbox.com/help/define-map-id/) of the form `username.id`, identifying the [tile sets](https://www.mapbox.com/help/define-tileset/) to display in the snapshot. This array may not be empty. | ||
- parameter size: The logical size of the image to output, measured in points. | ||
*/ | ||
public init(mapIdentifiers: [String], size: CGSize) { | ||
self.mapIdentifiers = mapIdentifiers | ||
self.size = size | ||
} | ||
|
||
/** | ||
Initializes a snapshot options instance that results in a snapshot centered at the given geographical coordinate and showing the given zoom level. | ||
|
||
- parameter mapIdentifiers: An array of [map identifiers](https://www.mapbox.com/help/define-map-id/) of the form `username.id`, identifying the [tile sets](https://www.mapbox.com/help/define-tileset/) to display in the snapshot. This array may not be empty. | ||
- parameter centerCoordinate: The geographic coordinate at the center of the snapshot. | ||
- parameter zoomLevel: The zoom level of the snapshot. | ||
- parameter size: The logical size of the image to output, measured in points. | ||
*/ | ||
public init(mapIdentifiers: [String], centerCoordinate: CLLocationCoordinate2D, zoomLevel: Int, size: CGSize) { | ||
self.mapIdentifiers = mapIdentifiers | ||
self.centerCoordinate = centerCoordinate | ||
self.zoomLevel = zoomLevel | ||
self.size = size | ||
} | ||
|
||
/** | ||
The path of the HTTP request URL corresponding to the options in this instance. | ||
|
||
- returns: An HTTP URL path. | ||
*/ | ||
open var path: String { | ||
assert(!mapIdentifiers.isEmpty, "At least one map identifier must be specified.") | ||
let tileSetComponent = mapIdentifiers.joined(separator: ",") | ||
|
||
let position: String | ||
if let centerCoordinate = centerCoordinate { | ||
position = "\(centerCoordinate.longitude),\(centerCoordinate.latitude),\(zoomLevel ?? 0)" | ||
} else { | ||
position = "auto" | ||
} | ||
|
||
if let zoomLevel = zoomLevel { | ||
assert(zoomLevel >= 0, "minimum zoom is 0") | ||
assert(zoomLevel <= 20, "maximum zoom is 20") | ||
} | ||
|
||
assert(size.width <= 1_280, "maximum width is 1,280 points") | ||
assert(size.height <= 1_280, "maximum height is 1,280 points") | ||
|
||
assert(overlays.count <= 100, "maximum number of overlays is 100") | ||
|
||
let overlaysComponent: String | ||
if overlays.isEmpty { | ||
overlaysComponent = "" | ||
} else { | ||
overlaysComponent = "/" + overlays.map { return "\($0)" }.joined(separator: ",") | ||
} | ||
|
||
return "/v4/\(tileSetComponent)\(overlaysComponent)/\(position)/\(Int(round(size.width)))x\(Int(round(size.height)))\(scale > 1 ? "@2x" : "").\(format)" | ||
} | ||
|
||
/** | ||
The query component of the HTTP request URL corresponding to the options in this instance. | ||
|
||
- returns: The query URL component as an array of name/value pairs. | ||
*/ | ||
open var params: [URLQueryItem] { | ||
return [] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
#if os(OSX) | ||
import Cocoa | ||
#elseif os(watchOS) | ||
import WatchKit | ||
#else | ||
import UIKit | ||
#endif | ||
|
||
/** | ||
A structure that configures a standalone marker image and how it is formatted. A standalone marker image is produced by the [classic Mapbox Static API](https://www.mapbox.com/api-documentation/?language=Swift#static-classic). | ||
*/ | ||
@objc(MBMarkerOptions) | ||
open class MarkerOptions: MarkerImage, SnapshotOptionsProtocol { | ||
#if os(OSX) | ||
/** | ||
The scale factor of the image. | ||
|
||
If you multiply the logical size of the image (stored in the `size` property) by the value in this property, you get the dimensions of the image in pixels. | ||
|
||
The default value of this property matches the natural scale factor associated with the main screen. However, only images with a scale factor of 1.0 or 2.0 are ever returned by the classic Static API, so a scale factor of 1.0 of less results in a 1× (standard-resolution) image, while a scale factor greater than 1.0 results in a 2× (high-resolution or Retina) image. | ||
*/ | ||
open var scale: CGFloat = NSScreen.main()?.backingScaleFactor ?? 1 | ||
#elseif os(watchOS) | ||
/** | ||
The scale factor of the image. | ||
|
||
If you multiply the logical size of the image (stored in the `size` property) by the value in this property, you get the dimensions of the image in pixels. | ||
|
||
The default value of this property matches the natural scale factor associated with the screen. Images with a scale factor of 1.0 or 2.0 are ever returned by the classic Static API, so a scale factor of 1.0 of less results in a 1× (standard-resolution) image, while a scale factor greater than 1.0 results in a 2× (high-resolution or Retina) image. | ||
*/ | ||
open var scale: CGFloat = WKInterfaceDevice.current().screenScale | ||
#else | ||
/** | ||
The scale factor of the image. | ||
|
||
If you multiply the logical size of the image (stored in the `size` property) by the value in this property, you get the dimensions of the image in pixels. | ||
|
||
The default value of this property matches the natural scale factor associated with the main screen. However, only images with a scale factor of 1.0 or 2.0 are ever returned by the classic Static API, so a scale factor of 1.0 of less results in a 1× (standard-resolution) image, while a scale factor greater than 1.0 results in a 2× (high-resolution or Retina) image. | ||
*/ | ||
open var scale: CGFloat = UIScreen.main.scale | ||
#endif | ||
|
||
/** | ||
Initializes a marker options instance. | ||
|
||
- parameter size: The size of the marker. | ||
- parameter label: A label or Maki icon to place atop the pin. | ||
*/ | ||
fileprivate override init(size: Size, label: Label?) { | ||
super.init(size: size, label: label) | ||
} | ||
|
||
/** | ||
Initializes a marker options instance that results in a red marker labeled with an English letter. | ||
|
||
- parameter size: The size of the marker. | ||
- parameter letter: An English letter from A through Z to place atop the pin. | ||
*/ | ||
public convenience init(size: Size = .small, letter: UniChar) { | ||
self.init(size: size, label: .letter(Character(UnicodeScalar(letter)!))) | ||
} | ||
|
||
/** | ||
Initializes a marker options instance that results in a red marker labeled with a one- or two-digit number. | ||
|
||
- parameter size: The size of the marker. | ||
- parameter number: A number from 0 through 99 to place atop the pin. | ||
*/ | ||
public convenience init(size: Size = .small, number: Int) { | ||
self.init(size: size, label: .number(number)) | ||
} | ||
|
||
/** | ||
Initializes a marker options instance that results in a red marker with a Maki icon. | ||
|
||
- parameter size: The size of the marker. | ||
- parameter iconName: The name of a [Maki](https://www.mapbox.com/maki-icons/) v0.5.0 icon to place atop the pin. | ||
*/ | ||
public convenience init(size: Size = .small, iconName: String) { | ||
self.init(size: size, label: .iconName(iconName)) | ||
} | ||
|
||
/** | ||
The path of the HTTP request URL corresponding to the options in this instance. | ||
|
||
- returns: An HTTP URL path. | ||
*/ | ||
open var path: String { | ||
let labelComponent: String | ||
if let label = label { | ||
labelComponent = "-\(label)" | ||
} else { | ||
labelComponent = "" | ||
} | ||
|
||
return "/v4/marker/pin-\(size)\(labelComponent)+\(color.toHexString())\(scale > 1 ? "@2x" : "").png" | ||
} | ||
|
||
/** | ||
The query component of the HTTP request URL corresponding to the options in this instance. | ||
|
||
- returns: The query URL component as an array of name/value pairs. | ||
*/ | ||
open var params: [URLQueryItem] { | ||
return [] | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it a historical oversight that these lines aren’t manually wrapped?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since Xcode automatically reformats the comment in the generated interface (whether you're coming from Swift or Objective-C code), I've been less concerned about documentation comment formatting here than in the gl-native codebase. But if you're concerned about readability for contributors to this codebase, we can address the line wrapping issue in a separate PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We’d have to tread carefully, because the component that translates Swift into Objective-C headers (and thus reStructuredText into Doxygen) is a bit fragile. Objective-C bridging has broken in the past due to something as innocuous as a blank line between
- parameter
and- note
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mostly the line length just made it slightly more painful to review.