TGMapView
@interface TGMapView : UIView <UIGestureRecognizerDelegate>
TGMapView is a flexible and customizable map view managing the lifecycle of an OpenGL ES map. This view provides
gesture handlers for tap, double-tap, long press, pan, pinch, rotate, and shove gestures.
The public interface provides dynamic map marker placement, change of camera view settings, and map description changes through scene updates.
This view uses scene files described by the Tangram scene format allowing you to fully customize your map using your own data. Some pre-made basemap styles can be found here using Nextzen data sources.
To use basemap styles you can sign up for an API key and load it through your application:
let sceneURL = URL("https://www.nextzen.org/carto/bubble-wrap-style/9/bubble-wrap-style.zip");
let sceneUpdates = [ TGSceneUpdate(path: "global.sdk_api_key", value: "YOUR_API_KEY") ];
view.loadScene(from: sceneURL, with: sceneUpdates);
Note
All the screen positions used in this inteface are in logical pixel or drawing coordinate system (based on aUIKit coordinate system), which is independent of the phone pixel density. Refer to the
Apple documentation
regarding Coordinate Systems and Drawing in iOS for more information.
-
Initializes and returns a new map view with no scene loaded.
To load a map scene use one of:
-[TGMapView loadSceneFromURL:withUpdates:]-[TGMapView loadSceneAsyncFromURL:withUpdates:]-[TGMapView loadSceneFromYAML:relativeToURL:withUpdates:]-[TGMapView loadSceneAsyncFromYAML:relativeToUrl:withUpdates:]Declaration
Objective-C
- (nonnull instancetype)initWithFrame:(CGRect)frame;Swift
init(frame: CGRect)Parameters
frameThe view frame.
Return Value
An initialized map view.
-
Initializes and returns a new map view with no scene loaded.
To load a map scene use one of:
-[TGMapView loadSceneFromURL:withUpdates:]-[TGMapView loadSceneAsyncFromURL:withUpdates:]-[TGMapView loadSceneFromYAML:relativeToURL:withUpdates:]-[TGMapView loadSceneAsyncFromYAML:relativeToUrl:withUpdates:]Declaration
Objective-C
- (nonnull instancetype)initWithFrame:(CGRect)frame urlHandler:(nonnull id<TGURLHandler>)urlHandler;Parameters
frameThe view frame.
urlHandlerA
TGURLHandlerfor customizing URL request behavior.Return Value
An initialized map view.
-
Load a scene file synchronously from a URL with a list of updates.
If an error occurs while applying updates the new scene will not be applied. See
TGSceneUpdatefor details.Declaration
Objective-C
- (int)loadSceneFromURL:(nonnull NSURL *)url withUpdates:(nullable NSArray<TGSceneUpdate *> *)updates;Swift
func loadScene(from url: URL, with updates: [TGSceneUpdate]?) -> Int32Parameters
urlAn http(s) or file URL for the scene file.
updatesA list of
TGSceneUpdateto apply to the scene, ornil.Return Value
The integer ID for the new scene or -1 if the scene cannot be loaded.
-
Load a scene file asynchronously from a URL with a list of updates.
Calls
-[TGMapViewDelegate mapView:didLoadScene:withError:]on themapViewDelegatewhen it completes.If an error occurs while applying updates the new scene will not be applied. See
TGSceneUpdatefor details.Declaration
Objective-C
- (int)loadSceneAsyncFromURL:(nonnull NSURL *)url withUpdates:(nullable NSArray<TGSceneUpdate *> *)updates;Swift
func loadSceneAsync(from url: URL, with updates: [TGSceneUpdate]?) -> Int32Parameters
urlAn http(s) or file URL for the scene file.
updatesA list of
TGSceneUpdateto apply to the scene, ornil.Return Value
The integer ID for the new scene or -1 if the scene cannot be loaded.
-
Load a scene synchronously from string with a list of updates.
Calls
-[TGMapViewDelegate mapView:didLoadScene:withError:]on themapViewDelegatewhen it completes.If an error occurs while applying updates the new scene will not be applied. See
TGSceneUpdatefor details.Declaration
Objective-C
- (int)loadSceneFromYAML:(nonnull NSString *)yaml relativeToURL:(nonnull NSURL *)url withUpdates:(nullable NSArray<TGSceneUpdate *> *)updates;Swift
func loadScene(fromYAML yaml: String, relativeTo url: URL, with updates: [TGSceneUpdate]?) -> Int32Parameters
yamlYAML scene string.
urlThe base URL used to resolve relative URLs in the scene.
updatesA list of
TGSceneUpdateto apply to the scene, ornil.Return Value
The integer ID for the new scene or -1 if the scene cannot be loaded.
-
Load a scene asynchronously from string with a list of updates.
Calls
-[TGMapViewDelegate mapView:didLoadScene:withError:]on themapViewDelegatewhen it completes.If an error occurs while applying updates the new scene will not be applied. See
TGSceneUpdatefor details.Declaration
Objective-C
- (int)loadSceneAsyncFromYAML:(nonnull NSString *)yaml relativeToURL:(nonnull NSURL *)url withUpdates:(nullable NSArray<TGSceneUpdate *> *)updates;Swift
func loadSceneAsync(fromYAML yaml: String, relativeTo url: URL, with updates: [TGSceneUpdate]?) -> Int32Parameters
yamlYAML scene string.
urlThe base URL used to resolve relative URLs in the scene.
updatesA list of
TGSceneUpdateto apply to the scene, ornil.Return Value
The integer ID for the new scene or -1 if the scene cannot be loaded.
-
The gesture recognizer delegate May be
nil. SeeTGRecognizerDelegatefor more details.Declaration
Objective-C
@property (readwrite, nonatomic, nullable) id<TGRecognizerDelegate> gestureDelegate; -
The map view delegate May be
nil. SeeTGMapViewDelegatefor more details.Declaration
Objective-C
@property (readwrite, nonatomic, nullable) id<TGMapViewDelegate> mapViewDelegate;
-
The current position and orientation of the map camera.
Setting this property will move the map immediately. To animate the movement use
-[TGMapView setCameraPosition:withDuration:easeType:callback:]or-[TGMapView flyToCameraPosition:withDuration:callback:]Declaration
Objective-C
@property (readwrite, copy, nonatomic) TGCameraPosition *_Nonnull cameraPosition;Swift
@NSCopying var cameraPosition: TGCameraPosition { get set } -
The minimum zoom level for the map view.
The default minimum zoom is 0. Values less than the default will be clamped. Assigning a value greater than the current maximum zoom will set the maximum zoom to this value.
Declaration
Objective-C
@property (assign, readwrite, nonatomic) CGFloat minimumZoomLevel;Swift
var minimumZoomLevel: CGFloat { get set } -
The maximum zoom level for the map view.
The default maximum zoom is 20.5. Values greater than the default will be clamped. Assigning a value less than the current minimum zoom will set the minimum zoom to this value.
Declaration
Objective-C
@property (assign, readwrite, nonatomic) CGFloat maximumZoomLevel;Swift
var maximumZoomLevel: CGFloat { get set } -
Assign a
TGCameraTypeto the view cameraDeclaration
Objective-C
@property (assign, readwrite, nonatomic) TGCameraType cameraType;Swift
var cameraType: TGCameraType { get set } -
Move the map camera to a new position with an easing animation.
Declaration
Objective-C
- (void)setCameraPosition:(nonnull TGCameraPosition *)cameraPosition withDuration:(NSTimeInterval)duration easeType:(TGEaseType)easeType callback:(nullable void (^)(BOOL))callback;Swift
func setCameraPosition(_ cameraPosition: TGCameraPosition, withDuration duration: TimeInterval, easeType: TGEaseType, callback: ((Bool) -> Void)? = nil)Parameters
cameraPositionThe new camera position
durationThe animation duration in seconds
easeTypeThe type of easing animation
callbackA callback to execute when the animation completes
-
Move the map camera to a new position with an animation that pans and zooms in a smooth arc.
The animation duration is calculated based on the distance to the new camera position assuming a speed scaling factor of 1.0
Declaration
Objective-C
- (void)flyToCameraPosition:(nonnull TGCameraPosition *)cameraPosition callback:(nullable void (^)(BOOL))callback;Swift
func fly(to cameraPosition: TGCameraPosition, callback: ((Bool) -> Void)? = nil)Parameters
cameraPositionThe new camera position
callbackA callback to execute when the animation completes
-
Move the map camera to a new position with an animation that pans and zooms in a smooth arc.
Declaration
Objective-C
- (void)flyToCameraPosition:(nonnull TGCameraPosition *)cameraPosition withDuration:(NSTimeInterval)duration callback:(nullable void (^)(BOOL))callback;Swift
func fly(to cameraPosition: TGCameraPosition, withDuration duration: TimeInterval, callback: ((Bool) -> Void)? = nil)Parameters
cameraPositionThe new camera position
durationDuration of the animation in seconds
callbackA callback to execute when the animation completes
-
Move the map camera to a new position with an animation that pans and zooms in a smooth arc.
The animation duration is calculated based on the distance to the new camera position and the specified speed
Declaration
Objective-C
- (void)flyToCameraPosition:(nonnull TGCameraPosition *)cameraPosition withSpeed:(CGFloat)speed callback:(nullable void (^)(BOOL))callback;Swift
func fly(to cameraPosition: TGCameraPosition, withSpeed speed: CGFloat, callback: ((Bool) -> Void)? = nil)Parameters
cameraPositionThe new camera position
speedSpecified speed scaling factor
callbackA callback to execute when the animation completes
-
Get a camera position that encloses the given bounds with at least the given amount of padding on each side.
Declaration
Objective-C
- (nonnull TGCameraPosition *)cameraThatFitsBounds:(TGCoordinateBounds)bounds withPadding:(UIEdgeInsets)padding;Swift
func cameraThatFitsBounds(_ bounds: TGCoordinateBounds, withPadding padding: UIEdgeInsets) -> TGCameraPositionParameters
boundsThe map bounds to enclose
paddingThe minimum distance to keep between the bounds and the edges of the view
-
The longitude and latitude of the center of the map view
Declaration
Objective-C
@property (assign, readwrite, nonatomic) CLLocationCoordinate2D position;Swift
var position: CLLocationCoordinate2D { get set } -
The zoom level of the map view
Declaration
Objective-C
@property (assign, readwrite, nonatomic) CGFloat zoom;Swift
var zoom: CGFloat { get set } -
The orientation of to the map view clockwise from true North
Declaration
Objective-C
@property (assign, readwrite, nonatomic) CLLocationDirection bearing;Swift
var bearing: CLLocationDirection { get set } -
The tilt angle of the map view in degrees away from straight down
Declaration
Objective-C
@property (assign, readwrite, nonatomic) CGFloat pitch;Swift
var pitch: CGFloat { get set }
-
Convert a longitude and latitude to a view position.
Declaration
Objective-C
- (CGPoint)viewPositionFromCoordinate:(CLLocationCoordinate2D)coordinate;Swift
func viewPosition(from coordinate: CLLocationCoordinate2D) -> CGPointParameters
coordinateThe geographic coordinate to convert
Return Value
The view position of the input coordinate
-
Convert a position in view coordinates into the longitude and latitude of the corresponding geographic location.
Declaration
Objective-C
- (CLLocationCoordinate2D)coordinateFromViewPosition:(CGPoint)viewPosition;Swift
func coordinate(fromViewPosition viewPosition: CGPoint) -> CLLocationCoordinate2DParameters
viewPositionthe position in view coordinates to convert
Return Value
the longitude and latitude
-
Remove all the Markers from the map.
Declaration
Objective-C
- (void)markerRemoveAll;Swift
func markerRemoveAll()
-
Assign the resource root for this map view.
Scene file URLs will be resolved relative to this URL.
Note
By default the resource root is the main bundle resource URL. Using the default resource root:scene.yamlis resolved tofile://<main bundle path>/Resources/scene.yaml,/path/scene.yamlis resolved tofile:///path/scene.yaml, andhttps://my.host/scene.yamlis resolved to itself.Declaration
Objective-C
@property (readwrite, strong, nonatomic) NSURL *_Nonnull resourceRoot;Swift
var resourceRoot: URL { get set }
-
Request the view to draw another frame.
Typically there is no need to call this. The map view re-draws automatically when needed.
Declaration
Objective-C
- (void)requestRender;Swift
func requestRender() -
The rate you want the map view to re-draw its contents.
The default value is 60 frames per second.
Declaration
Objective-C
@property (assign, readwrite, nonatomic) NSInteger preferredFramesPerSecond;Swift
var preferredFramesPerSecond: Int { get set } -
The CADisplayLink used by the renderer. Exposed mostly for the ability to pause/unpause rendering without having to initiate a scene update, as animations defined within the stylesheet force a continuous rendering regardless of the
continuoussetting defined in code.Note
This property is manipulated by the rendering subsystem depending on lifecycle events coming from the OS as well as certain events within Tangram itself. As such, do not expect your manipulation of individual settings (such as
paused) to be long lived. An example of this is setting displayLink.paused to YES, then backgrounding and foregrounding the app. Tangram will respond to the OS lifecycle events and pause/unpause the map rendering, respectively, thereby overriding your setting. This is intended functionality and will not be changed.Note
This property, even though it is marked NS_ASSUME_NONNULL at the top of the header, will be nil until the view is added to some kind of view hierarchy (window or subview). In a future PR we should clean up this assumption, but until then, you have been warned :)
Declaration
Objective-C
@property (readonly, strong, nonatomic) CADisplayLink *_Nonnull displayLink;Swift
var displayLink: CADisplayLink { get } -
If
continuousisYES, the map view will re-draw continuously. Otherwise, the map will re-draw only when an event changes the map view.Scenes can be configured as
animated(see the animated property documentation for details). When a scene is loaded this property is set to match the animated value from the scene.Note
Changing this property will override the inferred value from the scene. Enabling continuous rendering can significantly increase the energy usage of an application.Declaration
Objective-C
@property (assign, readwrite, nonatomic) BOOL continuous;Swift
var continuous: Bool { get set }
-
Replaces the tap gesture recognizer used by the map view and adds it to the UIView.
Declaration
Objective-C
@property (readwrite, strong, nonatomic) UITapGestureRecognizer *_Nonnull tapGestureRecognizer;Swift
var tapGestureRecognizer: UITapGestureRecognizer { get set } -
Replaces the double tap gesture recognizer used by the map view and adds it to the UIView.
Declaration
Objective-C
@property (readwrite, strong, nonatomic) UITapGestureRecognizer *_Nonnull doubleTapGestureRecognizer;Swift
var doubleTapGestureRecognizer: UITapGestureRecognizer { get set } -
Replaces the pan gesture recognizer used by the map view and adds it to the UIView.
Declaration
Objective-C
@property (readwrite, strong, nonatomic) UIPanGestureRecognizer *_Nonnull panGestureRecognizer;Swift
var panGestureRecognizer: UIPanGestureRecognizer { get set } -
Replaces the pinch gesture recognizer used by the map view and adds it to the UIView.
Declaration
Objective-C
@property (readwrite, strong, nonatomic) UIPinchGestureRecognizer *_Nonnull pinchGestureRecognizer;Swift
var pinchGestureRecognizer: UIPinchGestureRecognizer { get set } -
Replaces the rotation gesture recognizer used by the map view and adds it to the UIView.
Declaration
Objective-C
@property (readwrite, strong, nonatomic) UIRotationGestureRecognizer *_Nonnull rotationGestureRecognizer;Swift
var rotationGestureRecognizer: UIRotationGestureRecognizer { get set } -
Replaces the shove gesture recognizer used by the map view and adds it to the UIView.
Declaration
Objective-C
@property (readwrite, strong, nonatomic) UIPanGestureRecognizer *_Nonnull shoveGestureRecognizer;Swift
var shoveGestureRecognizer: UIPanGestureRecognizer { get set } -
Replaces the long press gesture recognizer used by the map view and adds it to the UIView.
Declaration
Objective-C
@property (readwrite, strong, nonatomic) UILongPressGestureRecognizer *_Nonnull longPressGestureRecognizer;Swift
var longPressGestureRecognizer: UILongPressGestureRecognizer { get set }
-
Client can use these to trigger explicit
TGMapRegionChangeStatechanges Notifies that client is going to begin map region animationDeclaration
Objective-C
- (void)notifyGestureDidBegin;Swift
func notifyGestureDidBegin() -
Client can use these to trigger explicit
TGMapRegionChangeStatechanges Notifies that client is animating map regionDeclaration
Objective-C
- (void)notifyGestureIsChanging;Swift
func notifyGestureIsChanging() -
Client can use these to trigger explicit
TGMapRegionChangeStatechanges Notifies that client is stopping animating map regionDeclaration
Objective-C
- (void)notifyGestureDidEnd;Swift
func notifyGestureDidEnd()
-
Adds a named data layer to the map.
Note
You cannot create more than one data source with the same name. If you call this with a name that is already in use, the previously returned object will be returned again.
Declaration
Objective-C
- (nullable TGMapData *)addDataLayer:(nonnull NSString *)name generateCentroid:(BOOL)generateCentroid;Swift
func addDataLayer(_ name: String, generateCentroid: Bool) -> TGMapData?Parameters
nameThe name of the data layer.
generateCentroidIf YES, a point feature will be added at the centroid of every polygon feature. This can be useful for labeling.
Return Value
The map data, or
nilif the data source can’t be initialized.
-
Capture a screenshot of the map view.
The captured screenshot will be delivered to the
mapViewDelegateby themapView:didCaptureScreenshot:method. The delegate must implement this method to receive the screenshot.Declaration
Objective-C
- (void)captureScreenshot:(BOOL)waitForViewComplete;Swift
func captureScreenshot(_ waitForViewComplete: Bool)Parameters
waitForViewCompleteIf YES, the view will wait for all parts of the map in the current view to finish loading before taking the screenshot.
-
Set the radius in logical pixels to use when picking features on the map (default is
0.5).The
-pick*methods will retrieve allinteractivemap objects from a circular area with this radius around the pick location. Setting a larger radius can help ensure that desired features are retrieved from an imprecise touch input.Declaration
Objective-C
- (void)setPickRadius:(CGFloat)pixels;Swift
func setPickRadius(_ pixels: CGFloat)Parameters
pixelsThe pick radius in logical pixels.
-
Select a visible feature marked as
interactivefrom the map view.The pick result will be delivered to the
mapViewDelegatebymapView:didSelectFeature:atScreenPosition:.The scene file determines which features can be picked. See the interactive property documentation for details.
Declaration
Objective-C
- (void)pickFeatureAt:(CGPoint)viewPosition;Swift
func pickFeature(at viewPosition: CGPoint)Parameters
viewPositionThe position in the view to pick from, in logical pixels.
-
Select a label marked as
interactivefrom the map view.The pick result will be delivered to the
mapViewDelegatebymapView:didSelectFeature:atScreenPosition:.The scene file determines which features can be picked. See the interactive property documentation for details.
Declaration
Objective-C
- (void)pickLabelAt:(CGPoint)viewPosition;Swift
func pickLabel(at viewPosition: CGPoint)Parameters
viewPositionThe position in the view to pick from, in logical pixels.
-
Select a Marker marked as
interactivefrom the map view.The pick result will be delivered to the
mapViewDelegatebymapView:didSelectFeature:atScreenPosition:.To pick a marker you must set the
interactiveproperty when styling it:TGMarker marker; marker.styling = "{ style: 'points', interactive : true, color: 'white', size: [30px, 30px], order: 500 }"Declaration
Objective-C
- (void)pickMarkerAt:(CGPoint)viewPosition;Swift
func pickMarker(at viewPosition: CGPoint)Parameters
viewPositionThe position in the view to pick from, in logical pixels.
-
Reduce memory usage by freeing currently unused resources.
Declaration
Objective-C
- (void)didReceiveMemoryWarning;Swift
func didReceiveMemoryWarning()
-
Set a
TGDebugFlagon the map view.Declaration
Objective-C
- (void)setDebugFlag:(TGDebugFlag)debugFlag value:(BOOL)on;Swift
func setDebugFlag(_ debugFlag: TGDebugFlag, value on: Bool)Parameters
debugFlagThe debug flag to set.
onWhether the flag is on or off.
-
Get the status of a
TGDebugFlag.Declaration
Objective-C
- (BOOL)getDebugFlag:(TGDebugFlag)debugFlag;Swift
func getDebugFlag(_ debugFlag: TGDebugFlag) -> BoolParameters
debugFlagThe debug flag to get the status of.
Return Value
Whether the flag is currently on or off for the map view.
-
Invert the state of a
TGDebugFlag.Declaration
Objective-C
- (void)toggleDebugFlag:(TGDebugFlag)debugFlag;Swift
func toggle(_ debugFlag: TGDebugFlag)Parameters
debugFlagThe debug flag to toggle the state of.
View on GitHub
TGMapView Class Reference
