Animate Map Style Layer

This example shows how to animate runtime map style layer.

  • Add Image Source layer

  • Update Image source every second

docs-image

For all code examples, refer to Maps Code Examples

AnimateMapLayerViewController view source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//
//  AnimateMapLayerViewController.swift
//  maps-ios-demo
//
//
import Foundation
import UIKit
import Nbmap
class AnimateMapLayerViewController: UIViewController {
    
    var nbMapView: NGLMapView! {
        didSet {
            oldValue?.removeFromSuperview()
            if let mapView = nbMapView {
                view.insertSubview(mapView, at: 0)
                mapView.delegate = self
            }
        }
    }
    var radarSuffix = 0
    
    var points : Array = [CLLocationCoordinate2D]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        nbMapView = NGLMapView(frame:self.view.bounds)
    }
    
    func addImageSourceLayer() {
        let coordinateQuad: NGLCoordinateQuad = NGLCoordinateQuad(
            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 imageSource = NGLImageSource(identifier: "style-image-source-id", coordinateQuad: coordinateQuad, image: UIImage(named: "southeast_radar_0")!)
        if let source = nbMapView.style?.source(withIdentifier: "style-image-source-id") {
            nbMapView.style?.removeSource(source)
        }
        nbMapView.style?.addSource(imageSource)
        let rasterLayer = NGLRasterStyleLayer(identifier: "style-raster-image-layer-id", source: imageSource)
        if let layer = nbMapView.style?.layer(withIdentifier: "style-raster-image-layer-id") {
            nbMapView.style?.removeLayer(layer)
        }
                                               
        nbMapView.style?.addLayer(rasterLayer)
        Timer.scheduledTimer(timeInterval: 0.8, target: self, selector: #selector(updateAnimatedImageSource(_:)), userInfo: imageSource, repeats: true)
    }
    
    @objc func updateAnimatedImageSource(_ timer: Timer) {
        guard let imageSource = timer.userInfo as? NGLImageSource else { return }
        let uiImage = UIImage(named: "southeast_radar_\(radarSuffix)")
        imageSource.setValue(uiImage, forKey: "image")
        radarSuffix += 1
        if radarSuffix > 3 {
            radarSuffix = 0
        }
    }
    
}
extension AnimateMapLayerViewController: NGLMapViewDelegate {
    func mapView(_ mapView: NGLMapView, didFinishLoading style: NGLStyle){
        
        nbMapView.setCenter(CLLocationCoordinate2DMake(41.437, -76.425), zoomLevel: 5, animated: true)
        addImageSourceLayer()
    }
}

The main functionality of this example is to display a map view with an animated image overlay (radar images). The addImageSourceLayer() method sets up the image source and raster layer, and the updateAnimatedImageSource(_:) method is called periodically to update the image source with new radar images.

  1. Declaration and initialization of nbMapView, radarSuffix, and points variables.

  2. viewDidLoad() method where the nbMapView is created and added as a subview to the view.

  3. addImageSourceLayer() method to add an image source and raster layer to the map view.

  4. updateAnimatedImageSource(_:) method to update the image source with a new image.

  5. mapView(_:didFinishLoading:) delegate method that sets the initial center and zoom level of the map and calls addImageSourceLayer().

Have Questions ?