moving/updating MKOverlay on MKMapView
Asked Answered
G

6

8

is there a way to update (i.e. moving around) a MKOverlay that is already added to the MKMapView. Removing a old one and adding a new one is terrible (slow).

i.e i would like to trigger the background function that is calling this function when an overlay moves on the screen:

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay

(with MKAnnotions its a little better i think, but i cant use MKPolyline, MKPolygon, etc. and the whole information is reduced to a single point)

Geoid answered 22/7, 2010 at 9:12 Comment(1)
addition: to give you an impression where i need this function: i use a MapView and place Annotations on top of it to show the positions of satellites. Each satellite should also show a footprint (the area where you can "see" the satellite above the horizon). This information (the position and the shape of the footprint) has to be updated periodically. In the Moment i have a solution with one View vor all Footprints where i draw with CoreGraphics the right shapes. But i would prefer a Solution with MKOverlay, but i can´t get them dynamic... any ideas?Geoid
C
4

MKOverlayView has the following methods which force MapKit to re-render the given mapRect:

- (void)setNeedsDisplayInMapRect:(MKMapRect)mapRect

- (void)setNeedsDisplayInMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale

If you’re using a timer (or periodical HTTP request or some sort of other method for determining that your overlay should be updated), calling one of the above methods on the overlayView will cause it to re-render that spot on the map (i.e. -canDrawMapRect:zoomScale: will be called again, and then -drawMapRect:zoomScale:inContext: will be called if the former returns YES).


Update:

If you’re not sure what mapRect you need to re-render, you might be able to use the MKMapRectWorld constant as the mapRect — which I believe would cause the overlay across the entire map to reload (once visible).

Corri answered 11/8, 2010 at 18:34 Comment(2)
It looks to me like... as of iOS 7, these methods are on MKOverlayRenderer, not MKOverlayView.Instrumentation
If you want to update the entire map, you can just call setNeedsDisplay Fermanagh
R
1

Use MKAnnotations. You can change the coordinate of them. Just disable any touch-related stuff. You would need your own drawing code to draw the annotations, OpenGL would probably do the trick. Nobody would know the difference.

Rafi answered 12/9, 2013 at 0:33 Comment(1)
AnnotationViews stay the same size if you zoom though, overlay sizes are relative to the zoom scale right? I need to specify a circle on the map that should mark a specific area (maybe a park or something).Parceling
N
0

I actually had to force the overlay view to invalidate the path, to clear out the old path before setting up a new one. [polygonView invalidatePath]. Telling the map view that it needs a display refresh was insufficient for me.

Novena answered 10/10, 2010 at 20:43 Comment(0)
A
0

This seems to force the map to refresh overlays. Probably annotations as well.

[self.mapView setCenterCoordinate:self.mapView.centerCoordinate];

Austroasiatic answered 17/2, 2017 at 0:51 Comment(1)
this animates/moves the overlay or not ?Afrit
C
0

Swift 3+ update, this worked for me, thanks @zenchemical for the inspiration.

            DispatchQueue.main.async {
                self.map.add(overlay, level: .aboveRoads)
                self.map.setCenter(self.map.centerCoordinate, animated: false)
            }

Side note, I can't help feeling that this shouldn't be necessary and there is a more appropriate solution, however, I haven't found it. Even though the add command is dispatched to the main queue, the overlay reproducibly does not render until I move the map.

Capriccio answered 13/8, 2018 at 16:17 Comment(0)
S
0

there is a problem with raw setNeedsDisplay if the bounding rectangle of the polygon changes then MKMapView would not update whole rectangle correctly,and secondly,MKPolygon/MKPolygonRenderer's polygon is not updatable,and the problem with add/remove pipeline is if there is too much request to update the MapView it can get slower in the pipeline, I currently use a 30Hz thread loop if there is an update in polygon i do add/remove

Scorch answered 19/10, 2018 at 5:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.