iOS SDK

API Reference

On This Page

MGLVectorTileSource

@interface MGLVectorTileSource : MGLTileSource

MGLVectorTileSource is a map content source that supplies tiled vector data in Mapbox Vector Tile format to be shown on the map. The location of and metadata about the tiles are defined either by an option dictionary or by an external file that conforms to the TileJSON specification. A vector tile source is added to an MGLStyle object along with one or more MGLVectorStyleLayer objects. A vector style layer defines the appearance of any content supplied by the vector tile source.

MGLVectorTileSource is optimized for data sets that are too large to fit completely in memory, such as vector tile sets or data sets managed in Mapbox Studio. For GeoJSON data, use the MGLShapeSource class. For tiled data that changes dynamically, the MGLComputedShapeSource class may be a suitable alternative.

Each vector source defined by the style JSON file is represented at runtime by an MGLVectorTileSource object that you can use to initialize new style layers. You can also add and remove sources dynamically using methods such as -[MGLStyle addSource:] and -[MGLStyle sourceWithIdentifier:].

Within each vector tile, each geometric coordinate must lie between −1 × extent and (extent × 2) − 1, inclusive. Any vector style layer initialized with a vector tile source must have a non-nil value in its sourceLayerIdentifier property.

Commonly used vector tile sources include Mapbox Streets, Mapbox Terrain, and Mapbox Traffic.

Example

let source = MGLVectorTileSource(identifier: "pois", tileURLTemplates: ["https://example.com/vector-tiles/{z}/{x}/{y}.mvt"], options: [
.minimumZoomLevel: 9,
.maximumZoomLevel: 16,
.attributionInfos: [
    MGLAttributionInfo(title: NSAttributedString(string: "© Mapbox"), url: URL(string: "https://mapbox.com"))
]
])
mapView.style?.addSource(source)

Initializing a Source

-initWithIdentifier:configurationURL:

Returns a vector tile source initialized with an identifier and configuration URL.

After initializing and configuring the source, add it to a map view’s style using the -[MGLStyle addSource:] method.

The URL may be a full HTTP or HTTPS URL or, for tilesets hosted by Mapbox, a Mapbox URL indicating a tileset ID (mapbox://<tilesetid>). The URL should point to a JSON file that conforms to the TileJSON specification.

Declaration

Objective-C

- (nonnull instancetype)initWithIdentifier:(nonnull NSString *)identifier
                      configurationURL:(nonnull NSURL *)configurationURL;

Swift

init(identifier: String, configurationURL: URL)

Parameters

identifier

A string that uniquely identifies the source in the style to which it is added.

configurationURL

A URL to a TileJSON configuration file describing the source’s contents and other metadata.

Return Value

An initialized vector tile source.

-initWithIdentifier:tileURLTemplates:options:

Returns a vector tile source initialized an identifier, tile URL templates, and options.

Tile URL templates are strings that specify the URLs of the vector tiles to load. See the “Tile URL Templates” guide for information about the format of a tile URL template.

After initializing and configuring the source, add it to a map view’s style using the -[MGLStyle addSource:] method.

Declaration

Objective-C

- (nonnull instancetype)
initWithIdentifier:(nonnull NSString *)identifier
  tileURLTemplates:(nonnull NSArray<NSString *> *)tileURLTemplates
           options:
               (nullable NSDictionary<MGLTileSourceOption, id> *)options;

Swift

init(identifier: String, tileURLTemplates: [String], options: [MGLTileSourceOption : Any]? = nil)

Parameters

identifier

A string that uniquely identifies the source in the style to which it is added.

tileURLTemplates

An array of tile URL template strings. Only the first string is used; any additional strings are ignored.

options

A dictionary containing configuration options. See MGLTileSourceOption for available keys and values. Pass in nil to use the default values.

Return Value

An initialized tile source.

Accessing a Source’s Content

-featuresInSourceLayersWithIdentifiers:predicate:

Returns an array of map features loaded by this source, restricted to the given source layers and filtered by the given predicate.

Each object in the returned array represents a feature loaded by the source and provides access to attributes specified as part of the loaded feature. The source loads a feature if the source is added to an MGLMapView’s style; that style has a layer that uses the source; and the map view has recently scrolled to the region containing the feature.

Features come from tiled vector data that is converted to tiles internally, so feature geometries are clipped at tile boundaries and features may appear duplicated across tiles. For example, suppose part of a lengthy polyline representing a road has recently scrolled into view. The resulting array includes those parts of the road that lie within the map tiles that the source has loaded, even if the road extends into other tiles. The portion of the road within each map tile is included individually.

Returned features may not necessarily be visible to the user at the time they are loaded: the style may contain a layer that forces the source’s tiles to load but filters out the features in question, preventing them from being drawn. To obtain only visible features, use the -[MGLMapView visibleFeaturesAtPoint:inStyleLayersWithIdentifiers:predicate:] or -[MGLMapView visibleFeaturesInRect:inStyleLayersWithIdentifiers:predicate:] method.

Declaration

Objective-C

- (nonnull NSArray<id<MGLFeature>> *)
featuresInSourceLayersWithIdentifiers:
    (nonnull NSSet<NSString *> *)sourceLayerIdentifiers
                            predicate:(nullable NSPredicate *)predicate;

Swift

func features(sourceLayerIdentifiers: Set<String>, predicate: NSPredicate?) -> [MGLFeature]

Parameters

sourceLayerIdentifiers

The source layers to include in the query. Only the features contained in these source layers are included in the returned array. This array may not be empty.

predicate

A predicate to filter the returned features. Use nil to include all loaded features.

Return Value

An array of objects conforming to the MGLFeature protocol that represent features loaded by the source that match the predicate.