Move MKMapView point to pixel coordinates

匿名 (未验证) 提交于 2019-12-03 08:57:35

问题:

I have a quick question. I am using a custom view as a callout accessory for my map view. I am having some issues with getting the annotation to move to the right bottom corner of the said view. I am currently trying to get the CGPoint coords of the selected annotation, but beyond that I've drawn a blank Any help would be greatly appreciated.

The current code I'm using (I know it is incorrect is:)

   CGPoint bottomLeftPoint = CGPointMake(xOffset,[self.internalAnnotationCallout view].frame.size.height); CLLocationCoordinate2D bottomLeftCoord = [self.branchLocation convertPoint:bottomLeftPoint toCoordinateFromView:self.branchLocation]; MKCoordinateSpan span = {latitudeDelta: kMapMultiLatDelta, longitudeDelta: kMapMultiLatDelta};   MKCoordinateRegion region = {bottomLeftCoord, span};  [self.branchLocation setRegion:region animated:YES]; //[self.branchLocation setCenterCoordinate:newCenterCoordinate animated:YES]; 

回答1:

EDIT:

Ok so I messed around with this a little and was able to to put something together that seems to work, hoping that I now actually understand what you're trying to achieve!

- (void)shiftToCorner{     //ignore "Annotation", this is just my own custom MKAnnotation subclass     Annotation *currentAnnotation = [[mapView selectedAnnotations] objectAtIndex:0];     [mapView setCenterCoordinate:currentAnnotation.coordinate];      CGPoint fakecenter = CGPointMake(20, 20);     CLLocationCoordinate2D coordinate = [mapView convertPoint:fakecenter toCoordinateFromView:mapView];     [mapView setCenterCoordinate:coordinate animated:YES]; } 

Let me quickly explain what this does; Let's say you want your annotation to move to a position 20 points in from the right edge and 20 points up from the bottom edge of the map view. If you think about it, if the current annotation is at the center of the map view, then the distance to this point is the same as the distance to (20, 20). This means that we can send our annotation to this point by first centering our map view on the annotation, then animating to (20, 20).



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!