将MKMapView点移动到像素坐标

时间:2021-01-27 21:19:22

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.

我有一个快速的问题。我使用自定义视图作为我的地图视图的标注附件。我有一些问题,让注释移动到所述视图的右下角。我目前正在尝试获得所选注释的CGPoint坐标,但除此之外,我已经画了一个空白任何帮助将不胜感激。

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 个解决方案

#1


7  

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).

让我快速解释一下这是做什么的;假设您希望注释移动到距右边缘20个点的位置,并从地图视图的底部边缘向上移动20个点。如果你考虑一下,如果当前注释位于地图视图的中心,那么到这一点的距离与到达(20,20)的距离相同。这意味着我们可以通过首先将我们的地图视图置于注释上,然后设置为(20,20)动画来将注释发送到此点。

#1


7  

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).

让我快速解释一下这是做什么的;假设您希望注释移动到距右边缘20个点的位置,并从地图视图的底部边缘向上移动20个点。如果你考虑一下,如果当前注释位于地图视图的中心,那么到这一点的距离与到达(20,20)的距离相同。这意味着我们可以通过首先将我们的地图视图置于注释上,然后设置为(20,20)动画来将注释发送到此点。