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.
Solved! Go to Solution.
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)")
}
}
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)")
}
}