Primitive Shapes
MGLAnnotation
@protocol MGLAnnotation <NSObject>
@protocol MGLAnnotation <NSObject>
The MGLAnnotation
protocol is used to provide annotation-related information
to a map view. To use this protocol, you adopt it in any custom objects that
store or represent annotation data. Each object then serves as the source of
information about a single map annotation and provides critical information,
such as the annotation’s location on the map. Annotation objects do not provide
the visual representation of the annotation but typically coordinate (in
conjunction with the map view’s delegate) the creation of an appropriate
objects to handle the display.
An object that adopts this protocol must implement the coordinate
property.
The other methods of this protocol are optional.
Related examples
See the
Annotation models and
Annotation views examples to learn how to add objects that follow the
MGLAnnotation
protocol.
-
coordinate
The center point (specified as a map coordinate) of the annotation. (required) (read-only)
Declaration
Objective-C
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
Swift
var coordinate: CLLocationCoordinate2D { get }
-
title
The string containing the annotation’s title.
Although this property is optional, if you support the selection of annotations in your map view, you are expected to provide this property. This string is displayed in the callout for the associated annotation.
Declaration
Objective-C
@optional @property (nonatomic, copy, readonly, nullable) NSString *title;
Swift
optional var title: String? { get }
-
subtitle
The string containing the annotation’s subtitle.
This string is displayed in the callout for the associated annotation.
Declaration
Objective-C
@optional @property (nonatomic, copy, readonly, nullable) NSString *subtitle;
Swift
optional var subtitle: String? { get }
MGLOverlay
@protocol MGLOverlay <MGLAnnotation>
The MGLOverlay
protocol defines a specific type
of annotation that represents
both a point and an area on a map. Overlay objects are
essentially data objects
that contain the geographic data needed to represent the map
area. Overlays can
take the form of a polyline or polygon.
You use overlays to layer more sophisticated content on top of a map view. For example, you could use an overlay to show the boundaries of a national park or trace a bus route along city streets. This SDK defines several concrete classes that conform to this protocol and define standard shapes.
-
coordinate
A coordinate representing the overlay. (required) (read-only)
Declaration
Objective-C
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
Swift
var coordinate: CLLocationCoordinate2D { get }
-
overlayBounds
The cooordinate rectangle that encompasses the overlay. (required) (read-only)
This property contains the smallest rectangle that completely encompasses the overlay. Implementers of this protocol must set this area when implementing their overlay class, and after setting it, you must not change it.
If this overlay spans the antimeridian, its bounds may extend west of −180 degrees longitude or east of 180 degrees longitude. For example, an overlay covering the Pacific Ocean from Tokyo to San Francisco might have a bounds extending from (35.68476, −220.24257) to (37.78428, −122.41310).
Declaration
Objective-C
@property (nonatomic, readonly) MGLCoordinateBounds overlayBounds;
Swift
var overlayBounds: MGLCoordinateBounds { get }
-
-intersectsOverlayBounds:
Returns a Boolean indicating whether the specified rectangle intersects the receiver’s shape.
You can implement this method to provide more specific bounds checking for an overlay. If you do not implement it, the bounding rectangle is used to detect intersections.
Declaration
Objective-C
- (BOOL)intersectsOverlayBounds:(MGLCoordinateBounds)overlayBounds;
Swift
func intersects(_ overlayBounds: MGLCoordinateBounds) -> Bool
Parameters
overlayBounds
The rectangle to intersect with the receiver’s area.
Return Value
YES
if any part of the map rectangle intersects the receiver’s shape orNO
if it does not.
MGLShape
@interface MGLShape : NSObject <MGLAnnotation, NSSecureCoding>
MGLShape
is an abstract class that represents a
shape or annotation. Shapes
constitute the content of a map — not only the overlays atop
the map, but also
the content that forms the base map.
Create instances of
MGLPointAnnotation
,
MGLPointCollection
,
MGLPolyline
,
MGLMultiPolyline
,
MGLPolygon
,
MGLMultiPolygon
,
or
MGLShapeCollection
in
order to use MGLShape
‘s methods. Do not create
instances of MGLShape
directly, and do not create your own subclasses of this
class. The shape
classes correspond to the
Geometry
object
types in the GeoJSON standard, but some have nonstandard
names for backwards
compatibility.
Although you do not create instances of this class directly,
you can use its
+[MGLShape shapeWithData:encoding:error:]
factory method to create one of the
concrete subclasses of MGLShape
noted above
from GeoJSON data. To access a
shape’s attributes, use the corresponding
MGLFeature
class instead.
You can add shapes to the map by adding them to an
MGLShapeSource
object.
Configure the appearance of an
MGLShapeSource
’s
or
MGLVectorTileSource
’s
shapes collectively using a concrete instance of
MGLVectorStyleLayer
.
Alternatively, you can add some kinds of shapes directly to
a map view as
annotations or overlays.
You can filter the features in a
MGLVectorStyleLayer
or vary their layout or
paint attributes based on the features’ geographies. Pass an
MGLShape
into an
NSPredicate
with the format
SELF IN %@
or %@ CONTAINS SELF
and
set the
MGLVectorStyleLayer.predicate
property to that predicate, or set a layout or
paint attribute to a similarly formatted
NSExpression
.
-
+shapeWithData:encoding:error:
Returns an
MGLShape
object initialized with the given data interpreted as a string containing a GeoJSON object.If the GeoJSON object is a geometry, the returned value is a kind of
MGLShape
. If it is a feature object, the returned value is a kind ofMGLShape
that conforms to theMGLFeature
protocol. If it is a feature collection object, the returned value is an instance ofMGLShapeCollectionFeature
.Example
let url = mainBundle.url(forResource: "amsterdam", withExtension: "geojson")! let data = try! Data(contentsOf: url) let feature = try! MGLShape(data: data, encoding: String.Encoding.utf8.rawValue) as! MGLShapeCollectionFeature
Declaration
Objective-C
+ (nullable MGLShape *)shapeWithData:(nonnull NSData *)data encoding:(NSStringEncoding)encoding error:(NSError *_Nullable *_Nullable)outError;
Swift
/*not inherited*/ init(data: Data, encoding: UInt) throws
Parameters
data
String data containing GeoJSON source code.
encoding
The encoding used by
data
.outError
Upon return, if an error has occurred, a pointer to an
NSError
object describing the error. Pass inNULL
to ignore any error.Return Value
An
MGLShape
object representation ofdata
, ornil
ifdata
could not be parsed as valid GeoJSON source code. Ifnil
,outError
contains anNSError
object describing the problem.
-
title
The title of the shape annotation.
The default value of this property is
nil
.This property is ignored when the shape is used in an
MGLShapeSource
. To name a shape used in a shape source, create anMGLFeature
and add an attribute to theMGLFeature.attributes
property.Declaration
Objective-C
@property (nonatomic, copy, nullable) NSString *title;
Swift
var title: String? { get set }
-
subtitle
The subtitle of the shape annotation. The default value of this property is
nil
.This property is ignored when the shape is used in an
MGLShapeSource
. To provide additional information about a shape used in a shape source, create anMGLFeature
and add an attribute to theMGLFeature.attributes
property.Declaration
Objective-C
@property (nonatomic, copy, nullable) NSString *subtitle;
Swift
var subtitle: String? { get set }
-
-geoJSONDataUsingEncoding:
Returns the GeoJSON string representation of the shape encapsulated in a data object.
Declaration
Objective-C
- (nonnull NSData *)geoJSONDataUsingEncoding:(NSStringEncoding)encoding;
Swift
func geoJSONData(usingEncoding encoding: UInt) -> Data
Parameters
encoding
The string encoding to use.
Return Value
A data object containing the shape’s GeoJSON string representation.
MGLMultiPoint
@interface MGLMultiPoint : MGLShape
The MGLMultiPoint
class is an abstract
superclass used to define shapes
composed of multiple vertices.
Create instances of
MGLPolyline
or
MGLPolygon
in order to use
properties of MGLMultiPoint
. Do not create
instances of MGLMultiPoint
directly and do not create your own subclasses of this
class. You can use
the method and properties of this class to access
information about the
vertices of the line or polygon.
Do not confuse MGLMultiPoint
with
MGLPointCollection
,
which represents a
collection of related but disconnected points.
-
coordinates
The array of vertices associated with the shape.
This C array is a pointer to a structure inside the multipoint object, which may have a lifetime shorter than the multipoint object and will certainly not have a longer lifetime. Therefore, you should copy the C array if it needs to be stored outside of the memory context in which you use this property.
Declaration
Objective-C
@property (nonatomic, readonly) NS_RETURNS_INNER_POINTER CLLocationCoordinate2D *coordinates;
Swift
var coordinates: UnsafeMutablePointer<CLLocationCoordinate2D> { get }
-
pointCount
The number of vertices in the shape.
Declaration
Objective-C
@property (nonatomic, readonly) NSUInteger pointCount;
Swift
var pointCount: UInt { get }
-
-getCoordinates:range:
Retrieves the vertices of part of the shape.
Declaration
Objective-C
- (void)getCoordinates:(nonnull CLLocationCoordinate2D *)coords range:(NSRange)range;
Swift
func getCoordinates(_ coords: UnsafeMutablePointer<CLLocationCoordinate2D>, range: NSRange)
Parameters
coords
On input, you must provide a C array of
CLLocationCoordinate2D
structures large enough to hold the desired number of coordinates. On output, this structure contains the requested coordinate data.range
The range of vertices you want. The
location
field indicates the first vertex you are requesting, with0
being the first vertex,1
being the second vertex, and so on. Thelength
field indicates the number of vertices you want. The array incoords
must be large enough to accommodate the number of requested coordinates. -
-setCoordinates:count:
Sets the shape’s vertices to the given C array of vertices.
Declaration
Objective-C
- (void)setCoordinates:(nonnull CLLocationCoordinate2D *)coords count:(NSUInteger)count;
Swift
func setCoordinates(_ coords: UnsafeMutablePointer<CLLocationCoordinate2D>, count: UInt)
Parameters
coords
The array of coordinates defining the shape. The data in this array is copied to the shape’s
coordinates
property.count
The number of coordinates from the
coords
array. -
-insertCoordinates:count:atIndex:
Inserts the given vertices into the shape.
If the shape is currently visible on the map as an annotation, it is redrawn immediately. If the shape is part of an
MGLShapeSource
object, you must explicitly set theMGLShapeSource.shape
property in order for any style layers that use the source to be redrawn.Declaration
Objective-C
- (void)insertCoordinates:(nonnull const CLLocationCoordinate2D *)coords count:(NSUInteger)count atIndex:(NSUInteger)index;
Swift
func insertCoordinates(_ coords: UnsafePointer<CLLocationCoordinate2D>, count: UInt, at index: UInt)
Parameters
coords
The array of coordinates to insert into the shape. The data in this array is copied to the shape’s
coordinates
property.count
The number of items in the
coords
array.index
The zero-based index at which the first coordinate in
coords
will appear in thecoordinates
property. -
-appendCoordinates:count:
Appends the given vertices to the shape.
If the shape is currently visible on the map as an annotation, it is redrawn immediately. If the shape is part of an
MGLShapeSource
object, you must explicitly set theMGLShapeSource.shape
property in order for any style layers that use the source to be redrawn.Declaration
Objective-C
- (void)appendCoordinates:(nonnull const CLLocationCoordinate2D *)coords count:(NSUInteger)count;
Swift
func appendCoordinates(_ coords: UnsafePointer<CLLocationCoordinate2D>, count: UInt)
Parameters
coords
The array of coordinates to add to the shape. The data in this array is copied to the shape’s
coordinates
property.count
The number of items in the
coords
array. -
-replaceCoordinatesInRange:withCoordinates:
Replaces the vertices at the given range in the shape with the same number of vertices from a given C array.
If the shape is currently visible on the map as an annotation, it is redrawn immediately. If the shape is part of an
MGLShapeSource
object, you must explicitly set theMGLShapeSource.shape
property in order for any style layers that use the source to be redrawn.The number of coordinates in
coords
must be equal to the length ofrange
. If you want to insert or delete one or more vertices, use the-replaceCoordinatesInRange:withCoordinates:count:
method.If
range
extends beyond the shape’scoordinates
property, anNSRangeException
is raised. If you want to append new vertices to the shape, use the-appendCoordinates:count:
method.Declaration
Objective-C
- (void)replaceCoordinatesInRange:(NSRange)range withCoordinates: (nonnull const CLLocationCoordinate2D *)coords;
Swift
func replaceCoordinates(in range: NSRange, withCoordinates coords: UnsafePointer<CLLocationCoordinate2D>)
Parameters
range
The range of vertices to replace. The
location
field indicates the first vertex you are replacing, with0
being the first vertex,1
being the second vertex, and so on. Thelength
field indicates the number of vertices to replace.coords
The array of coordinates defining part of the shape. The data in this array is copied to the shape’s
coordinates
property. -
-replaceCoordinatesInRange:withCoordinates:count:
Replaces the vertices at the given range in the shape with the specified number of vertices from a given C array.
If the shape is currently visible on the map as an annotation, it is redrawn immediately. If the shape is part of an
MGLShapeSource
object, you must explicitly set theMGLShapeSource.shape
property in order for any style layers that use the source to be redrawn.If
count
is greater than thelength
field ofrange
, some vertices will effectively be inserted into the shape. On the other hand, ifcount
is less than thelength
field ofrange
, some vertices will effectively be removed.If
range
extends beyond the shape’scoordinates
property, anNSRangeException
is raised. If you want to append new vertices to the shape, use the-appendCoordinates:count:
method.Declaration
Objective-C
- (void)replaceCoordinatesInRange:(NSRange)range withCoordinates:(nonnull const CLLocationCoordinate2D *)coords count:(NSUInteger)count;
Swift
func replaceCoordinates(in range: NSRange, withCoordinates coords: UnsafePointer<CLLocationCoordinate2D>, count: UInt)
Parameters
range
The range of vertices to replace. The
location
field indicates the first vertex you are replacing, with0
being the first vertex,1
being the second vertex, and so on. Thelength
field indicates the number of vertices to replace.coords
The array of coordinates defining part of the shape. The data in this array is copied to the shape’s
coordinates
property.count
The number of coordinates from the
coords
array to insert in place of the coordinates inrange
. The sum ofrange
’s length and this count must not exceed the number of items currently in thecoordinates
property. -
-removeCoordinatesInRange:
Removes the vertices at the given range from the shape.
If the shape is currently visible on the map as an annotation, it is redrawn immediately. If the shape is part of an
MGLShapeSource
object, you must explicitly set theMGLShapeSource.shape
property in order for any style layers that use the source to be redrawn.If
range
extends beyond the shape’scoordinates
property, anNSRangeException
is raised.Declaration
Objective-C
- (void)removeCoordinatesInRange:(NSRange)range;
Swift
func removeCoordinates(in range: NSRange)
Parameters
range
The range of vertices to remove. The
location
field indicates the first vertex you are removing, with0
being the first vertex,1
being the second vertex, and so on. Thelength
field indicates the number of vertices to remove.
MGLPointAnnotation
@interface MGLPointAnnotation : MGLShape
An MGLPointAnnotation
object represents a
one-dimensional shape located at a
single geographical coordinate. Depending on how it is used,
an
MGLPointAnnotation
object is known as a point
annotation or point shape. For
example, you could use a point shape to represent a city at
low zoom levels, an
address at high zoom levels, or the location of a long press
gesture.
You can add point shapes to the map by adding them to an
MGLShapeSource
object. Configure the appearance of an
MGLShapeSource
’s
or
MGLVectorTileSource
’s
point shapes collectively using an
MGLCircleStyleLayer
or
MGLSymbolStyleLayer
object.
For more interactivity, add a selectable point annotation to
a map view using
the
-[MGLMapView addAnnotation:]
method. Alternatively, define your own model
class that conforms to the
MGLAnnotation
protocol. Configure a point
annotation’s appearance using
-[MGLMapViewDelegate mapView:imageForAnnotation:]
or
-[MGLMapViewDelegate mapView:viewForAnnotation:]
(iOS only). A point
annotation’s
MGLShape.title
and
MGLShape.subtitle
properties define the
default content of the annotation’s callout (on iOS) or
popover (on macOS).
To group multiple related points together in one shape, use
an
MGLPointCollection
or
MGLShapeCollection
object. To access
a point’s attributes, use an
MGLPointFeature
object.
A point shape is known as a Point geometry in GeoJSON.
Related examples
See the
Mark a place on the map with an annotation,
Mark a place on the map with an image, and
Default callout usage examples to learn how to add
MGLPointAnnotation
objects to your map.
-
coordinate
The coordinate point of the shape, specified as a latitude and longitude.
Declaration
Objective-C
@property (nonatomic) CLLocationCoordinate2D coordinate;
Swift
var coordinate: CLLocationCoordinate2D { get set }
MGLPointCollection
@interface MGLPointCollection : MGLShape <MGLOverlay>
An MGLPointCollection
object represents a shape
consisting of one or more
disconnected vertices, specified as
CLLocationCoordinate2D
instances. The
points in the collection may be related but are not
connected spatially. For
example, you could use a point collection to represent all
the trees in an
orchard.
You can add point collections to the map by adding them to an
MGLShapeSource
object. Configure the appearance of an
MGLShapeSource
’s
or
MGLVectorTileSource
’s
point collections collectively using an
MGLCircleStyleLayer
or
MGLSymbolStyleLayer
object. To access a point
collection’s attributes, use an
MGLPointCollectionFeature
object.
You cannot add an MGLPointCollection
object
directly to a map view as an
annotation. However, you can create individual
MGLPointAnnotation
objects
from the coordinates
array and add those
annotation objects to the map view
using the
-[MGLMapView addAnnotations:]
method.
A point collection is known as a
MultiPoint
geometry in GeoJSON. Do not confuse
MGLPointCollection
with
MGLMultiPoint
,
the abstract superclass of
MGLPolyline
and
MGLPolygon
.
-
+pointCollectionWithCoordinates:count:
Creates and returns a
MGLPointCollection
object from the specified set of coordinates.Declaration
Objective-C
+ (instancetype)pointCollectionWithCoordinates: (const CLLocationCoordinate2D *)coords count:(NSUInteger)count;
Swift
convenience init!(coordinates coords: UnsafePointer<CLLocationCoordinate2D>!, count: UInt)
Parameters
coords
The array of coordinates defining the shape. The data in this array is copied to the new object.
count
The number of items in the
coords
array.Return Value
A new point collection object.
-
coordinates
The array of coordinates associated with the shape.
Declaration
Objective-C
@property (nonatomic, readonly) CLLocationCoordinate2D *coordinates;
Swift
var coordinates: UnsafeMutablePointer<CLLocationCoordinate2D>! { get }
-
pointCount
The number of coordinates associated with the shape.
Declaration
Objective-C
@property (nonatomic, readonly) NSUInteger pointCount;
Swift
var pointCount: UInt { get }
-
-getCoordinates:range:
Retrieves one or more coordinates associated with the shape.
Declaration
Objective-C
- (void)getCoordinates:(CLLocationCoordinate2D *)coords range:(NSRange)range;
Swift
func getCoordinates(_ coords: UnsafeMutablePointer<CLLocationCoordinate2D>!, range: NSRange)
Parameters
coords
On input, you must provide a C array of structures large enough to hold the desired number of coordinates. On output, this structure contains the requested coordinate data.
range
The range of points you want. The
location
field indicates the first point you are requesting, with0
being the first point,1
being the second point, and so on. Thelength
field indicates the number of points you want. The array incoords
must be large enough to accommodate the number of requested coordinates.
MGLPolygon
@interface MGLPolygon : MGLMultiPoint <MGLOverlay>
An MGLPolygon
object represents a closed shape
consisting of four or more
vertices, specified as CLLocationCoordinate2D
instances, and the edges that
connect them. For example, you could use a polygon shape to
represent a
building, a lake, or an area you want to highlight.
You can add polygon shapes to the map by adding them to an
MGLShapeSource
object. Configure the appearance of an
MGLShapeSource
’s
or
MGLVectorTileSource
’s
polygons collectively using an
MGLFillStyleLayer
or
MGLSymbolStyleLayer
object. To access a polygon’s attributes, use an
MGLPolygonFeature
object.
Alternatively, you can add a polygon overlay directly to a
map view using the
-[MGLMapView addAnnotation:]
or
-[MGLMapView addOverlay:]
method. Configure
a polygon overlay’s appearance using
-[MGLMapViewDelegate mapView:strokeColorForShapeAnnotation:]
and
-[MGLMapViewDelegate mapView:fillColorForPolygonAnnotation:]
.
The vertices are automatically connected in the order in
which you provide
them. You should close the polygon by specifying the same
CLLocationCoordinate2D
as the first and last
vertices; otherwise, the
polygon’s fill may not cover the area you expect it to. To
avoid filling the
space within the shape, give the polygon a transparent fill
or use an
MGLPolyline
object.
A polygon may have one or more interior polygons, or holes,
that you specify as
MGLPolygon
objects with the
+polygonWithCoordinates:count:interiorPolygons:
method. For example, if a polygon represents a lake, it
could exclude an island
within the lake using an interior polygon. Interior polygons
may not themselves
have interior polygons. To represent a shape that includes a
polygon within a
hole or, more generally, to group multiple polygons together
in one shape, use
an
MGLMultiPolygon
or
MGLShapeCollection
object.
To make the polygon straddle the antimeridian, specify some longitudes less than −180 degrees or greater than 180 degrees.
Related examples
See the
Add a polygon annotation example to learn how to
initialize an
MGLPolygon
object from an array of coordinates.
-
interiorPolygons
The array of polygons nested inside the receiver.
The area occupied by any interior polygons is excluded from the overall shape. Interior polygons should not overlap. An interior polygon should not have interior polygons of its own.
If there are no interior polygons, the value of this property is
nil
.Declaration
Objective-C
@property (nonatomic, readonly, nullable) NSArray<MGLPolygon *> *interiorPolygons;
Swift
var interiorPolygons: [MGLPolygon]? { get }
-
+polygonWithCoordinates:count:
Creates and returns an
MGLPolygon
object from the specified set of coordinates.Declaration
Objective-C
+ (nonnull instancetype)polygonWithCoordinates: (nonnull const CLLocationCoordinate2D *)coords count:(NSUInteger)count;
Swift
convenience init(coordinates coords: UnsafePointer<CLLocationCoordinate2D>, count: UInt)
Parameters
coords
The array of coordinates defining the shape. The data in this array is copied to the new object.
count
The number of items in the
coords
array.Return Value
A new polygon object.
-
+polygonWithCoordinates:count:interiorPolygons:
Creates and returns an
MGLPolygon
object from the specified set of coordinates and interior polygons.Declaration
Objective-C
+ (nonnull instancetype) polygonWithCoordinates:(nonnull const CLLocationCoordinate2D *)coords count:(NSUInteger)count interiorPolygons:(nullable NSArray<MGLPolygon *> *)interiorPolygons;
Swift
convenience init(coordinates coords: UnsafePointer<CLLocationCoordinate2D>, count: UInt, interiorPolygons: [MGLPolygon]?)
Parameters
coords
The array of coordinates defining the shape. The data in this array is copied to the new object.
count
The number of items in the
coords
array.interiorPolygons
An array of
MGLPolygon
objects that define regions excluded from the overall shape. If this array isnil
or empty, the shape is considered to have no interior polygons.Return Value
A new polygon object.
MGLPolyline
@interface MGLPolyline : MGLMultiPoint <MGLOverlay>
An MGLPolyline
object represents a shape
consisting of two or more vertices,
specified as CLLocationCoordinate2D
instances,
and the line segments that
connect them. For example, you could use an polyline to
represent a road or the
path along which something moves.
You can add polyline shapes to the map by adding them to an
MGLShapeSource
object. Configure the appearance of an
MGLShapeSource
’s
or
MGLVectorTileSource
’s
polylines collectively using an
MGLLineStyleLayer
or
MGLSymbolStyleLayer
object. To access a polyline’s attributes, use an
MGLPolylineFeature
object.
Alternatively, you can add a polyline overlay directly to a
map view using the
-[MGLMapView addAnnotation:]
or
-[MGLMapView addOverlay:]
method. Configure
a polyline overlay’s appearance using
-[MGLMapViewDelegate mapView:strokeColorForShapeAnnotation:]
and
-[MGLMapViewDelegate mapView:lineWidthForPolylineAnnotation:]
.
The vertices are automatically connected in the order in
which you provide
them. The first and last vertices are not connected to each
other, but you can
specify the same CLLocationCoordinate2D
as the
first and last vertices in
order to close the polyline. To fill the space within the
shape, use an
MGLPolygon
object. To group multiple polylines together in one shape,
use an
MGLMultiPolyline
or
MGLShapeCollection
object.
To make the polyline go across the antimeridian or international date line, specify some longitudes less than −180 degrees or greater than 180 degrees. For example, a polyline that stretches from Tokyo to San Francisco would have coordinates of (35.68476, -220.24257) and (37.78428, -122.41310).
let coordinates = [
CLLocationCoordinate2D(latitude: 35.68476, longitude: -220.24257),
CLLocationCoordinate2D(latitude: 37.78428, longitude: -122.41310)
]
let polyline = MGLPolyline(coordinates: coordinates, count: UInt(coordinates.count))
A polyline is known as a LineString geometry in GeoJSON.
Related examples
See the
Annotation models example to learn how to add an
MGLPolyine
object to
your map.
-
+polylineWithCoordinates:count:
Creates and returns an
MGLPolyline
object from the specified set of coordinates.Declaration
Objective-C
+ (nonnull instancetype)polylineWithCoordinates: (nonnull const CLLocationCoordinate2D *)coords count:(NSUInteger)count;
Swift
convenience init(coordinates coords: UnsafePointer<CLLocationCoordinate2D>, count: UInt)
Parameters
coords
The array of coordinates defining the shape. The data in this array is copied to the new object.
count
The number of items in the
coords
array.Return Value
A new polyline object.
MGLMultiPolygon
@interface MGLMultiPolygon : MGLShape <MGLOverlay>
An MGLMultiPolygon
object represents a shape
consisting of one or more
polygons that do not overlap. For example, you could use a
multipolygon shape
to represent the body of land that consists of an island
surrounded by an
atoll: the inner island would be one
MGLPolygon
object, while the surrounding
atoll would be another. You could also use a multipolygon
shape to represent a
group of disconnected but related buildings.
You can add multipolygon shapes to the map by adding them to
an
MGLShapeSource
object. Configure the appearance of an
MGLShapeSource
’s
or
MGLVectorTileSource
’s
multipolygons collectively using an
MGLFillStyleLayer
or
MGLSymbolStyleLayer
object.
You cannot add an MGLMultiPolygon
object
directly to a map view using
-[MGLMapView addAnnotation:]
or
-[MGLMapView addOverlay:]
.
However, you can
add the polygons
array’s items as overlays
individually.
-
polygons
An array of polygons forming the multipolygon.
Declaration
Objective-C
@property (nonatomic, copy, readonly) NSArray<MGLPolygon *> *_Nonnull polygons;
Swift
var polygons: [MGLPolygon] { get }
-
+multiPolygonWithPolygons:
Creates and returns a multipolygon object consisting of the given polygons.
Declaration
Objective-C
+ (nonnull instancetype)multiPolygonWithPolygons: (nonnull NSArray<MGLPolygon *> *)polygons;
Swift
convenience init(polygons: [MGLPolygon])
Parameters
polygons
The array of polygons defining the shape.
Return Value
A new multipolygon object.
MGLMultiPolyline
@interface MGLMultiPolyline : MGLShape <MGLOverlay>
An MGLMultiPolyline
object represents a shape
consisting of one or more
polylines. For example, you could use a multipolyline shape
to represent both
sides of a divided highway (dual carriageway), excluding the
median (central
reservation): each carriageway would be a distinct
MGLPolyline
object.
You can add multipolyline shapes to the map by adding them to
an
MGLShapeSource
object. Configure the appearance of an
MGLShapeSource
’s
or
MGLVectorTileSource
’s
multipolylines collectively using an
MGLLineStyleLayer
or
MGLSymbolStyleLayer
object.
You cannot add an MGLMultiPolyline
object
directly to a map view using
-[MGLMapView addAnnotation:]
or
-[MGLMapView addOverlay:]
.
However, you can
add the polylines
array’s items as overlays
individually.
A multipolyline is known as a MultiLineString geometry in GeoJSON.
-
polylines
An array of polygons forming the multipolyline.
Declaration
Objective-C
@property (nonatomic, copy, readonly) NSArray<MGLPolyline *> *_Nonnull polylines;
Swift
var polylines: [MGLPolyline] { get }
-
+multiPolylineWithPolylines:
Creates and returns a multipolyline object consisting of the given polylines.
Declaration
Objective-C
+ (nonnull instancetype)multiPolylineWithPolylines: (nonnull NSArray<MGLPolyline *> *)polylines;
Swift
convenience init(polylines: [MGLPolyline])
Parameters
polylines
The array of polylines defining the shape.
Return Value
A new multipolyline object.
MGLShapeCollection
@interface MGLShapeCollection : MGLShape
An MGLShapeCollection
object represents a shape
consisting of zero or more
distinct but related shapes that are instances of
MGLShape
.
The constituent
shapes can be a mixture of different kinds of shapes.
MGLShapeCollection
is most commonly used to add
multiple shapes to a single
MGLShapeSource
.
Configure the appearance of an
MGLShapeSource
’s
or
MGLVectorTileSource
’s
shape collection collectively using an
MGLSymbolStyleLayer
object, or use multiple instances of
MGLCircleStyleLayer
,
MGLFillStyleLayer
,
and
MGLLineStyleLayer
to
configure the appearance of each kind of shape inside the
collection.
You cannot add an MGLShapeCollection
object
directly to a map view as an
annotation. However, you can create individual
MGLPointAnnotation
,
MGLPolyline
,
and
MGLPolygon
objects from the shapes
array and add those
annotation objects to the map view using the
-[MGLMapView addAnnotations:]
method.
To represent a collection of point, polyline, or polygon
shapes, it may be more
convenient to use an
MGLPointCollection
,
MGLMultiPolyline
,
or
MGLMultiPolygon
object, respectively. To access a shape collection’s
attributes, use the corresponding
MGLFeature
object.
A shape collection is known as a GeometryCollection geometry in GeoJSON.
-
shapes
-
+shapeCollectionWithShapes:
Creates and returns a shape collection consisting of the given shapes.
Declaration
Objective-C
+ (nonnull instancetype)shapeCollectionWithShapes: (nonnull NSArray<MGLShape *> *)shapes;
Swift
convenience init(shapes: [MGLShape])
Parameters
shapes
The array of shapes defining the shape collection. The data in this array is copied to the new object.
Return Value
A new shape collection object.