Select to view content in your preferred language

i need the delegate method that about zoom the map

2570
1
Jump to solution
11-01-2017 01:59 AM
wangchao1
Emerging Contributor

i need to do something when map is zoomed to a scale.  i need to know the mapscale in real-time when the map is being zoomed.

1 Solution

Accepted Solutions
Nicholas-Furness
Esri Regular Contributor

Hi,

Assuming you have a reference to the AGSMapView named mapView, then if you're using Runtime 100.x, you can use KVO on the map's mapScale property, like this:

override func viewDidLoad() {
    super.viewDidLoad()

    mapView.addObserver(self, forKeyPath: "mapScale", options: [.new, .old], context: nil)
}

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if let mapViewThatChanged = object as? AGSMapView, keyPath == "mapScale" {
        print("Map scale changed to \(mapViewThatChanged.mapScale)")
    }
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

or if you're using Swift 4, it might look like this:

private var scaleKvoToken:NSKeyValueObservation?

override func viewDidLoad() {
    super.viewDidLoad()

    scaleKvoToken = mapView.observe(\.mapScale) { mapViewThatChanged, _ in
        print("Map scale changed to \(mapViewThatChanged.mapScale)")
    }
}
‍‍‍‍‍‍‍‍‍

View solution in original post

1 Reply
Nicholas-Furness
Esri Regular Contributor

Hi,

Assuming you have a reference to the AGSMapView named mapView, then if you're using Runtime 100.x, you can use KVO on the map's mapScale property, like this:

override func viewDidLoad() {
    super.viewDidLoad()

    mapView.addObserver(self, forKeyPath: "mapScale", options: [.new, .old], context: nil)
}

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if let mapViewThatChanged = object as? AGSMapView, keyPath == "mapScale" {
        print("Map scale changed to \(mapViewThatChanged.mapScale)")
    }
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

or if you're using Swift 4, it might look like this:

private var scaleKvoToken:NSKeyValueObservation?

override func viewDidLoad() {
    super.viewDidLoad()

    scaleKvoToken = mapView.observe(\.mapScale) { mapViewThatChanged, _ in
        print("Map scale changed to \(mapViewThatChanged.mapScale)")
    }
}
‍‍‍‍‍‍‍‍‍