MGLMapViewDelegate
@protocol MGLMapViewDelegate <NSObject>
The MGLMapViewDelegate protocol defines a set of optional methods that you can use to receive map-related update messages. Because many map operations require the MGLMapView
class to load data asynchronously, the map view calls these methods to notify your application when specific operations complete. The map view also uses these methods to request annotation marker symbology and to manage interactions with those markers.
-
Tells the delegate that the region displayed by the map view is about to change.
This method is called whenever the currently displayed map region will start changing.
Declaration
Objective-C
- (void)mapView:(nonnull MGLMapView *)mapView regionWillChangeAnimated:(BOOL)animated;
Parameters
mapView
The map view whose visible region will change.
animated
Whether the change will cause an animated effect on the map.
-
Tells the delegate that the region displayed by the map view is changing.
This method is called whenever the currently displayed map region changes. During movement, this method may be called many times to report updates to the map position. Therefore, your implementation of this method should be as lightweight as possible to avoid affecting performance.
Declaration
Objective-C
- (void)mapViewRegionIsChanging:(nonnull MGLMapView *)mapView;
Parameters
mapView
The map view whose visible region is changing.
-
Tells the delegate that the region displayed by the map view just changed.
This method is called whenever the currently displayed map region has finished changing.
Declaration
Objective-C
- (void)mapView:(nonnull MGLMapView *)mapView regionDidChangeAnimated:(BOOL)animated;
Parameters
mapView
The map view whose visible region changed.
animated
Whether the change caused an animated effect on the map.
-
Tells the delegate that the map view will begin to load.
This method is called whenever the map view starts loading, including when a new style has been set and the map must reload.
Declaration
Objective-C
- (void)mapViewWillStartLoadingMap:(nonnull MGLMapView *)mapView;
Parameters
mapView
The map view that is starting to load.
-
Tells the delegate that the map view has finished loading.
This method is called whenever the map view finishes loading, either after the initial load or after a style change has forced a reload.
Declaration
Objective-C
- (void)mapViewDidFinishLoadingMap:(nonnull MGLMapView *)mapView;
Parameters
mapView
The map view that has finished loading.
-
Tells the delegate that the map view will begin tracking the user’s location.
This method is called when the value of the
showsUserLocation
property changes toYES
.Declaration
Objective-C
- (void)mapViewWillStartLocatingUser:(nonnull MGLMapView *)mapView;
Parameters
mapView
The map view that is tracking the user’s location.
-
Tells the delegate that the map view has stopped tracking the user’s location.
This method is called when the value of the
showsUserLocation
property changes toNO
.Declaration
Objective-C
- (void)mapViewDidStopLocatingUser:(nonnull MGLMapView *)mapView;
Parameters
mapView
The map view that is tracking the user’s location.
-
Tells the delegate that the location of the user was updated.
While the
showsUserLocation
property is set toYES
, this method is called whenever a new location update is received by the map view. This method is also called if the map view’s user tracking mode is set toMGLUserTrackingModeFollowWithHeading
and the heading changes, or if it is set toMGLUserTrackingModeFollowWithCourse
and the course changes.This method is not called if the application is currently running in the background. If you want to receive location updates while running in the background, you must use the Core Location framework.
Declaration
Objective-C
- (void)mapView:(nonnull MGLMapView *)mapView didUpdateUserLocation:(nullable MGLUserLocation *)userLocation;
Parameters
mapView
The map view that is tracking the user’s location.
userLocation
The location object representing the user’s latest location. This property may be
nil
. -
Tells the delegate that an attempt to locate the user’s position failed.
Declaration
Objective-C
- (void)mapView:(nonnull MGLMapView *)mapView didFailToLocateUserWithError:(nonnull NSError *)error;
Parameters
mapView
The map view that is tracking the user’s location.
error
An error object containing the reason why location tracking failed.
-
Tells the delegate that the map view’s user tracking mode has changed.
This method is called after the map view asynchronously changes to reflect the new user tracking mode, for example by beginning to zoom or rotate.
Declaration
Objective-C
- (void)mapView:(nonnull MGLMapView *)mapView didChangeUserTrackingMode:(MGLUserTrackingMode)mode animated:(BOOL)animated;
Parameters
mapView
The map view that changed its tracking mode.
mode
The new tracking mode.
animated
Whether the change caused an animated effect on the map.
-
Returns an image object to use for the marker for the specified point annotation object.
Declaration
Objective-C
- (nullable MGLAnnotationImage *)mapView:(nonnull MGLMapView *)mapView imageForAnnotation:(nonnull id<MGLAnnotation>)annotation;
Parameters
mapView
The map view that requested the annotation image.
annotation
The object representing the annotation that is about to be displayed.
Return Value
The image object to display for the specified annotation or
nil
if you want to display the default marker image. -
Returns the alpha value to use when rendering a shape annotation. Defaults to
1.0
.Declaration
Objective-C
- (CGFloat)mapView:(nonnull MGLMapView *)mapView alphaForShapeAnnotation:(nonnull MGLShape *)annotation;
Parameters
mapView
The map view rendering the shape annotation.
annotation
The annotation being rendered.
Return Value
An alpha value between
0
and1.0
. -
Returns the stroke color to use when rendering a shape annotation. Defaults to the map view’s tint color.
Declaration
Objective-C
- (nonnull UIColor *)mapView:(nonnull MGLMapView *)mapView strokeColorForShapeAnnotation:(nonnull MGLShape *)annotation;
Parameters
mapView
The map view rendering the shape annotation.
annotation
The annotation being rendered.
Return Value
A color to use for the shape outline.
-
Returns the fill color to use when rendering a polygon annotation. Defaults to the map view’s tint color.
Declaration
Objective-C
- (nonnull UIColor *)mapView:(nonnull MGLMapView *)mapView fillColorForPolygonAnnotation:(nonnull MGLPolygon *)annotation;
Parameters
mapView
The map view rendering the polygon annotation.
annotation
The annotation being rendered.
Return Value
A color to use for the polygon interior.
-
Returns the line width to use when rendering a polyline annotation. Defaults to
3.0
.Declaration
Objective-C
- (CGFloat)mapView:(nonnull MGLMapView *)mapView lineWidthForPolylineAnnotation:(nonnull MGLPolyline *)annotation;
Parameters
mapView
The map view rendering the polygon annotation.
annotation
The annotation being rendered.
Return Value
A line width for the polyline.
-
Returns a Boolean value indicating whether the annotation is able to display extra information in a callout bubble.
If the value returned is
YES
, a standard callout bubble is shown when the user taps a selected annotation. The callout uses the title and subtitle text from the associated annotation object. If there is no title text, though, the annotation will not show a callout. The callout also displays any custom callout views returned by the delegate for the left and right callout accessory views.If the value returned is
NO
, the value of the title and subtitle strings are ignored.Declaration
Objective-C
- (BOOL)mapView:(nonnull MGLMapView *)mapView annotationCanShowCallout:(nonnull id<MGLAnnotation>)annotation;
Parameters
mapView
The map view that requested the annotation callout ability.
annotation
The object representing the annotation.
Return Value
A Boolean indicating whether the annotation should show a callout.
-
Returns a callout view to display for the specified annotation.
If this method is present in the delegate, it must return a new instance of a view dedicated to display the callout bubble. It will be configured by the map view. If this method is not present, or if it returns
nil
, a standard, two-line, bubble-like callout view is displayed by default.Declaration
Objective-C
- (nullable UIView<MGLCalloutView> *)mapView:(nonnull MGLMapView *)mapView calloutViewForAnnotation: (nonnull id<MGLAnnotation>)annotation;
Parameters
mapView
The map view that requested the callout view.
annotation
The object representing the annotation.
Return Value
A view conforming to the
MGLCalloutView
protocol, ornil
to use the default callout view. -
Returns the view to display on the left side of the standard callout bubble.
The default value is treated as if
nil
. The left callout view is typically used to display information about the annotation or to link to custom information provided by your application.If the view you specify is also a descendant of the
UIControl
class, you can use the map view’s delegate to receive notifications when your control is tapped. If it does not descend fromUIControl
, your view is responsible for handling any touch events within its bounds.Declaration
Objective-C
- (nullable UIView *)mapView:(nonnull MGLMapView *)mapView leftCalloutAccessoryViewForAnnotation:(nonnull id<MGLAnnotation>)annotation;
Parameters
mapView
The map view presenting the annotation callout.
annotation
The object representing the annotation with the callout.
Return Value
The accessory view to display.
-
Returns the view to display on the right side of the standard callout bubble.
The default value is treated is if
nil
. The right callout view is typically used to link to more detailed information about the annotation. A common view to specify for this property isUIButton
object whose type is set toUIButtonTypeDetailDisclosure
.If the view you specify is also a descendant of the
UIControl
class, you can use the map view’s delegate to receive notifications when your control is tapped. If it does not descend fromUIControl
, your view is responsible for handling any touch events within its bounds.Declaration
Objective-C
- (nullable UIView *)mapView:(nonnull MGLMapView *)mapView rightCalloutAccessoryViewForAnnotation: (nonnull id<MGLAnnotation>)annotation;
Parameters
mapView
The map view presenting the annotation callout.
annotation
The object representing the annotation with the callout.
Return Value
The accessory view to display.
-
Tells the delegate that the user tapped one of the annotation’s accessory buttons.
Accessory views contain custom content and are positioned on either side of the annotation title text. If a view you specify is a descendant of the
UIControl
class, the map view calls this method as a convenience whenever the user taps your view. You can use this method to respond to taps and perform any actions associated with that control. For example, if your control displayed additional information about the annotation, you could use this method to present a modal panel with that information.If your custom accessory views are not descendants of the
UIControl
class, the map view does not call this method.Declaration
Objective-C
- (void)mapView:(nonnull MGLMapView *)mapView annotation:(nonnull id<MGLAnnotation>)annotation calloutAccessoryControlTapped:(nonnull UIControl *)control;
Parameters
mapView
The map view containing the specified annotation.
annotation
The annotation whose button was tapped.
control
The control that was tapped.
-
Tells the delegate that the user tapped on an annotation’s callout view.
Declaration
Objective-C
- (void)mapView:(nonnull MGLMapView *)mapView tapOnCalloutForAnnotation:(nonnull id<MGLAnnotation>)annotation;
Parameters
mapView
The map view containing the specified annotation.
annotation
The annotation whose callout was tapped.
-
Tells the delegate that one of its annotations was selected.
You can use this method to track changes in the selection state of annotations.
Declaration
Objective-C
- (void)mapView:(nonnull MGLMapView *)mapView didSelectAnnotation:(nonnull id<MGLAnnotation>)annotation;
Parameters
mapView
The map view containing the annotation.
annotation
The annotation that was selected.
-
Tells the delegate that one of its annotations was deselected.
You can use this method to track changes in the selection state of annotations.
Declaration
Objective-C
- (void)mapView:(nonnull MGLMapView *)mapView didDeselectAnnotation:(nonnull id<MGLAnnotation>)annotation;
Parameters
mapView
The map view containing the annotation.
annotation
The annotation that was deselected.