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.
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:
<SPAN class="keyword token">override</SPAN> func <SPAN class="token function">viewDidLoad</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN> super<SPAN class="punctuation token">.</SPAN><SPAN class="token function">viewDidLoad</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> mapView<SPAN class="punctuation token">.</SPAN><SPAN class="token function">addObserver</SPAN><SPAN class="punctuation token">(</SPAN>self<SPAN class="punctuation token">,</SPAN> forKeyPath<SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"mapScale"</SPAN><SPAN class="punctuation token">,</SPAN> options<SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="keyword token">new</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">.</SPAN>old<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> context<SPAN class="punctuation token">:</SPAN> nil<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="keyword token">override</SPAN> func <SPAN class="token function">observeValue</SPAN><SPAN class="punctuation token">(</SPAN>forKeyPath keyPath<SPAN class="punctuation token">:</SPAN> String<SPAN class="operator token">?</SPAN><SPAN class="punctuation token">,</SPAN> of <SPAN class="keyword token">object</SPAN><SPAN class="punctuation token">:</SPAN> Any<SPAN class="operator token">?</SPAN><SPAN class="punctuation token">,</SPAN> change<SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">[</SPAN>NSKeyValueChangeKey <SPAN class="punctuation token">:</SPAN> Any<SPAN class="punctuation token">]</SPAN><SPAN class="operator token">?</SPAN><SPAN class="punctuation token">,</SPAN> context<SPAN class="punctuation token">:</SPAN> UnsafeMutableRawPointer<SPAN class="operator token">?</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="keyword token">if</SPAN> <SPAN class="keyword token">let</SPAN> mapViewThatChanged <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">object</SPAN> <SPAN class="keyword token">as</SPAN><SPAN class="operator token">?</SPAN> AGSMapView<SPAN class="punctuation token">,</SPAN> keyPath <SPAN class="operator token">==</SPAN> <SPAN class="string token">"mapScale"</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="token function">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Map scale changed to \(mapViewThatChanged.mapScale)"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="punctuation token">}</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
or if you're using Swift 4, it might look like this:
<SPAN class="keyword token">private</SPAN> <SPAN class="keyword token">var</SPAN> scaleKvoToken<SPAN class="punctuation token">:</SPAN>NSKeyValueObservation<SPAN class="operator token">?</SPAN> <SPAN class="keyword token">override</SPAN> func <SPAN class="token function">viewDidLoad</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN> super<SPAN class="punctuation token">.</SPAN><SPAN class="token function">viewDidLoad</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> scaleKvoToken <SPAN class="operator token">=</SPAN> mapView<SPAN class="punctuation token">.</SPAN><SPAN class="token function">observe</SPAN><SPAN class="punctuation token">(</SPAN>\<SPAN class="punctuation token">.</SPAN>mapScale<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN> mapViewThatChanged<SPAN class="punctuation token">,</SPAN> _ <SPAN class="keyword token">in</SPAN> <SPAN class="token function">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Map scale changed to \(mapViewThatChanged.mapScale)"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Aangemelde leden kunnen berichten plaatsen, updates volgen en meer. Nieuw hier? Registreer een gratis account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.