PDA

View Full Version : [iOS] Annotation moves when zooming in



guidove
24.04.2015, 05:31
I have the weirdest problem. An annotation on my map is jumping to another position when I pinch-zoom the map. I'm using SKMaps 2.4.

The situation is as follows:
I have a route with via 10 points. I have a customized annotation for the route start, the route end and also for the via points on the route. When I add the annotations to the mapView they are in the correct position, like in screenshot below.
2046

Then I start zooming in and viaPoint1 annotation spontaneously jumps to the same coordinate as the route end-point. See screenshot.
2047

I have the same problem with other routes! It's always the first viaPoint annotation that always jumps to the route end-point. All the other annotations remain in their correct position.

I use below function to add the annotation to the mapView.


func addAnnotationToMapView(mapView: SKMapView, #id: Int32, #coordinate: CLLocationCoordinate2D, #image: UIImage, #width: CGFloat, #height: CGFloat, #offsetX: CGFloat, #offsetY: CGFloat, #reuseIdentifier: String) {
let imageView = UIImageView(frame: CGRectMake(0.0, 0.0, width, height))
imageView.contentMode = UIViewContentMode.ScaleAspectFit
imageView.image = image
let annotationView = SKAnnotationView(view: imageView, reuseIdentifier: reuseIdentifier)
let annotation = SKAnnotation()
annotation.annotationView = annotationView
annotation.identifier = id
annotation.location = coordinate
annotation.offset = CGPointMake(offsetX, offsetY)
mapView.addAnnotation(annotation, withAnimationSettings: nil)
}

And I call this function for the start and end annotation from viewDidAppear:

addAnnotationToMapView(self.mapView, id: 98, coordinate: track.start.coordinate, image: UIImage(named: trackStartMarker)!, width: 40, height: 40, offsetX: 0, offsetY: 30, reuseIdentifier: trackStartMarker)
addAnnotationToMapView(self.mapView, id: 99, coordinate: track.end.coordinate, image: UIImage(named: trackEndMarker)!, width: 40, height: 40, offsetX: 0, offsetY: 38, reuseIdentifier: trackEndMarker)
trackStartMarker and trackEndMarker are predefined strings.

And for the viaPoints later-on, by user tapping a button:

for var i = 0; i < viaPointArray.count; ++i {
let vp = viaPointArray[i]
addAnnotationToMapView(mapView, id: Int32(i+1), coordinate: vp.coordinate, image: viaPointIconWithNr(i+1), width: 20, height: 37, offsetX: 0, offsetY: 32, reuseIdentifier: "vp\(i+1)")
}
viaPointIconWithNr(number: Int) is a function that return the viaPoint image with the number indicated.

As you can see I'm using unique identifiers (viaPointArray never has more then 50 points) and all the reuseIdentifiers are also unique.

Not sure what's going on here. It's definitely very weird :-)

I thought I had a workaround for it: In the callback functions didDoubleTapAtCoordinate and didStartRegionChangeFromRegion I added this:

mapView.removeAnnotationWithID(1)
addAnnotationToMapView(mapView, id: Int32(1), coordinate: viaPointArray[0].coordinate, image: viaPointIconWithNr(1), width: 20, height: 37, offsetX: 0, offsetY: 32, reuseIdentifier: "vp1")
However this doesn't really work as the annotation moves AFTER the pinch-zoom action i.e. AFTER the callbacks were called. So then it requires another pinch-zoom action to initiate the callback and put the annotation back in it's place. That does work. I'm thinking it has something to do with redrawing the mapView and it's annotations after a zoom action. This, if at all, is happening inside the framework so perhaps you can shed some more light on it?

A bit off topic, I also have 2 older threads where I recently replied to (was tackling some other problems so it took me some time to reply).
I didn't get a response yet, while usually response is next day. Not trying to rush you or anything, just in case you're not monitoring these older threads anymore.
http://forum.skobbler.com/showthread.php/6696-Route-rendering-problems-in-iOS (this one might actually be related to the issue described here)
http://forum.skobbler.com/showthread.php/6964-Get-notified-when-map-has-been-loaded

Thanks very much in advance for any help or pointers you could give me.
Cheers,
Guido

guidove
24.04.2015, 18:31
Did some more digging and found a workable workaround.
In stead of adding

mapView.removeAnnotationWithID(1)
addAnnotationToMapView(mapView, id: Int32(1), coordinate: viaPointArray[0].coordinate, image: viaPointIconWithNr(1), width: 20, height: 37, offsetX: 0, offsetY: 32, reuseIdentifier: "vp1")
to didDoubleTapAtCoordinate and didStartRegionChangeFromRegion I added it to didChangeToRegion

This method is called many times DURING a zoom action so it's a bit heavy, but it's the only thing that works.
I also tried adding it to didEndRegionChangeToRegion, but that didn't work.

In stead of above code I also tried this, more light, code:

mapView.annotationForIdentifier(1).location = viaPointArray[0].coordinate
but strangely enough that didn't work either.

During my investigation I could conclude that, although the annotation moves, the actual location property of the annotation is not changing. I tested this by adding following code to didChangeToRegion:

if tempCoor.longitude != mapView.annotationForIdentifier(1).location.longit ude {
println("annotation location shifted")
}
tempCoor is set to the annotationLocation just after adding it to the map.

Well, although I have a workaround now, a "real" solution would be better as this is a quite "heavy" workaround.

Cheers and regards,
Guido

Adela_Silvia
08.05.2015, 17:35
Happy to hear you managed to find a way to solve it- we'll keep a close eye on it for the next release

guidove
30.10.2015, 23:45
Still the same problem in 2.5.1
Any luck finding a solution?

Adela_Silvia
02.11.2015, 14:35
We haven't experienced the issue in 2.5.1. Please allow us to check that again

guidove
05.11.2015, 05:17
By dumb luck I found out that this mysterious behaviour occurs for the annotation with identifier 0 (zero). If I change the identifier the mysterious behaviour is gone in my case.
My guess is, but perhaps you (Adela) can confirm, is that somehow the 0 identifier is reserved for the current location indicator.

Adela_Silvia
06.11.2015, 11:28
Hi guidove,

I'll have to check this information with our developers. I'll get back with an update

guidove
06.11.2015, 16:48
Be sure to also include this thread in your communication. It's the same problem. http://forum.skobbler.com/showthread.php/7255-Routing-between-2-annotations-moves-1st-annotation-to-location-of-2nd-Annotation