There seems to be an "issue" with overlays and the MapKit
. Unlike annotations, overlays aren't reused and therefore when adding multiple overlays would cause memory-problems on a real device. I've had this problem multiple times. So my question is, how can I reuse the MKOverlay and so improve the performance of overlays on MapKit
?
相关问题
- What uses more memory in c++? An 2 ints or 2 funct
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Why are memory addresses incremented by 4 in MIPS?
- Swift - hide pickerView after value selected
- How do you detect key up / key down events from a
- didBeginContact:(SKPhysicsContact *)contact not in
The Answer to this is not "reusing" but to draw them all in to one
MKOverlayView
and then draw that on the map.Multiple
MKPolygons
,MKOverlays
etc. cause heavy memory-usage when drawing on the map. This is due the NOT reusing of overlays byMapKit
. As annotations have thereuseWithIdentifier
, overlays however don't. Each overlay creates a new layer as aMKOverlayView
on the map with the overlay in it. In that way memory-usage will rise quite fast and map-usage becomes... let's say sluggish to almost impossible.Therefore there is a work-around: Instead of plotting each overlay individually, you can add all of the
MKOverlays
to oneMKOverlayView
. This way you're in fact creating only oneMKOverlayView
and thus there's no need to reuse.This is the work-around, in this case for
MKPolygons
but should be not very different for others likeMKCircles
etc.create a class:
MultiPolygon
(subclass ofNSObject
)in
MultiPolygon.h
:in
MultiPolygon.m
:Now create a class:
MultiPolygonView
(subclass ofMKOverlayPathView
)in
MultiPolygonView.h
:In
MultiPolygonView.m
:To us it import
MultiPolygon.h
andMultiPolygonView.h
in your ViewControllerCreate one polygon from all: As an example I've got an array with polygons:
polygonsInArray
.Add the allPolygonsInOne to the mapView:
Also change your
viewForOverlay
method:And this greatly reduced memory usage for multiple overlays on the
mapView
. You're not reusing now because only oneOverlayView
is drawn. So no need to reuse.