MGLImageSource

@interface MGLImageSource : MGLSource

MGLImageSource is a content source that is used for a georeferenced raster image to be shown on the map. The georeferenced image scales and rotates as the user zooms and rotates the map. Images may also be used as icons or patterns in a style layer. To register an image for use as an icon or pattern, use the -[MGLStyle setImage:forName:] method. To configure a point annotation’s image, use the MGLAnnotationImage class.

The geographic location of the raster image content, supplied with MGLCoordinateQuad, can be non-axis aligned. MGLImageSource supports raster content from NSURL, NSImage (macOS), or UIImage (iOS). An image source is added to an MGLStyle object along with one or more MGLRasterStyleLayer objects. Use a raster style layer to control the appearance of content supplied by the image source.

Each image source defined by the style JSON file is represented at runtime by an MGLImageSource 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:].

Example

let coordinates = MGLCoordinateQuad(
topLeft: CLLocationCoordinate2D(latitude: 46.437, longitude: -80.425),
bottomLeft: CLLocationCoordinate2D(latitude: 37.936, longitude: -80.425),
bottomRight: CLLocationCoordinate2D(latitude: 37.936, longitude: -71.516),
topRight: CLLocationCoordinate2D(latitude: 46.437, longitude: -71.516))
let source = MGLImageSource(identifier: "radar", coordinateQuad: coordinates, url: URL(string: "https://www.mapbox.com/mapbox-gl-js/assets/radar.gif")!)
mapView.style?.addSource(source)

Initializing a Source

-initWithIdentifier:coordinateQuad:URL:

Returns a georeferenced image source with an identifier, coordinates and a URL.

Declaration

Objective-C

- (nonnull instancetype)initWithIdentifier:(nonnull NSString *)identifier
                        coordinateQuad:(MGLCoordinateQuad)coordinateQuad
                                   URL:(nonnull NSURL *)url;

Swift

init(identifier: String, coordinateQuad: MGLCoordinateQuad, url: URL)

Parameters

identifier

A string that uniquely identifies the source.

coordinateQuad

the top left, top right, bottom right, and bottom left coordinates for the image.

url

An HTTP(S) URL, absolute file URL, or local file URL relative to the current application’s resource bundle.

Return Value

An initialized shape source.

-initWithIdentifier:coordinateQuad:image:

Returns a georeferenced image source with an identifier, coordinates and an image.

Declaration

Objective-C

- (nonnull instancetype)initWithIdentifier:(nonnull NSString *)identifier
                        coordinateQuad:(MGLCoordinateQuad)coordinateQuad
                                 image:(nonnull UIImage *)image;

Swift

init(identifier: String, coordinateQuad: MGLCoordinateQuad, image: UIImage)

Parameters

identifier

A string that uniquely identifies the source.

coordinateQuad

The top left, top right, bottom right, and bottom left coordinates for the image.

image

The image to display for the source.

Return Value

An initialized shape source.

Accessing a Source’s Content

URL

The URL to the source image.

If the receiver was initialized using -initWithIdentifier:coordinateQuad:image: or the image property is set, this property is set to nil.

Declaration

Objective-C

@property (nonatomic, copy, readwrite, nullable) NSURL *URL;

Swift

var url: URL? { get set }

image

The source image.

If the receiver was initialized using -initWithIdentifier:coordinateQuad:URL: or if the URL property is set, this property is set to nil.

Declaration

Objective-C

@property (nonatomic, retain, readwrite, nullable) UIImage *image;

Swift

var image: UIImage? { get set }

coordinates

The coordinates at which the corners of the source image will be placed.

Declaration

Objective-C

@property (nonatomic, assign, unsafe_unretained, readwrite)
MGLCoordinateQuad coordinates;

Swift

var coordinates: MGLCoordinateQuad { get set }
iOS SDK

SDK JS Reference

On this page