-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from mapbox/1ec5-gl-9
Upgrade to Mapbox GL–based Mapbox Static API
- Loading branch information
Showing
50 changed files
with
1,789 additions
and
903 deletions.
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.