如何根据选定的注释移动 MKMapView

发布于 2024-10-22 02:48:40 字数 116 浏览 2 评论 0原文

我有一个 MKMapView,它填充了我的整个视图,但是当选择一个图钉时,我会在地图顶部向上滑动另一个视图。我想移动地图,以便图钉出现在地图可见区域的中心。

很难解释,但希望它是有道理的! 提前致谢。

I have an MKMapView which populates the entirety of my view, but when a pin is selected, I am sliding up another view on top of the map. I want to move the map so the pin will appear in the centre of the visible area of the map.

Difficult to explain but hopefully it makes sense!
Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

尾戒 2024-10-29 02:48:40

您可以尝试从地图视图的 visibleMapRect 获取 MKMapRect,将注释的坐标转换为 MKMapPoint,重置 MKMapRect 的原点以使 MKMapPoint 位于适当的位置,然后使用 setVisibleMapRect:animated : 将可见区域设置为新的 MKMapRect。

例如,如果您想要移动地图,使注释水平居中并垂直向下 25%,您可以执行以下操作:

MKMapRect r = [mapView visibleMapRect];
MKMapPoint pt = MKMapPointForCoordinate([annotation coordinate]);
r.origin.x = pt.x - r.size.width * 0.5;
r.origin.y = pt.y - r.size.height * 0.25;
[mapView setVisibleMapRect:r animated:YES]; 

You could try getting the MKMapRect from visibleMapRect for the map view, converting the annotation's coordinate to an MKMapPoint, resetting the MKMapRect's origin so the MKMapPoint is in the appropriate position, and then using setVisibleMapRect:animated: to set the visible region to the new MKMapRect.

For example, if you wanted to move the map so the annotation is centered horizontally and 25% of the way down vertically, you could do something like this:

MKMapRect r = [mapView visibleMapRect];
MKMapPoint pt = MKMapPointForCoordinate([annotation coordinate]);
r.origin.x = pt.x - r.size.width * 0.5;
r.origin.y = pt.y - r.size.height * 0.25;
[mapView setVisibleMapRect:r animated:YES]; 
吾家有女初长成 2024-10-29 02:48:40

使用 MKMapViewDelegate 方法:

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {

    // center the mapView on the selected pin
    let region = MKCoordinateRegion(center: view.annotation!.coordinate, span: mapView.region.span)
    mapView.setRegion(region, animated: true)
}

Using MKMapViewDelegate method:

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {

    // center the mapView on the selected pin
    let region = MKCoordinateRegion(center: view.annotation!.coordinate, span: mapView.region.span)
    mapView.setRegion(region, animated: true)
}
像极了他 2024-10-29 02:48:40

我根据这里的答案在 Swift 4 上构建了它

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {

        // center the mapView on the selected pin
        var dest = view.annotation!.coordinate
        if view.annotation?.title == "Name of Annotation View"{//This is for move a little down depending on the MKAnnotationView size
            dest.latitude = dest.latitude + 0.002
        }
        let span = MKCoordinateSpan.init(latitudeDelta: 0.01, longitudeDelta: 0.01)
        let region = MKCoordinateRegion(center: dest, span: span)
        mapView.setRegion(region, animated: true)


    }

I built it on Swift 4, based on answers here

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {

        // center the mapView on the selected pin
        var dest = view.annotation!.coordinate
        if view.annotation?.title == "Name of Annotation View"{//This is for move a little down depending on the MKAnnotationView size
            dest.latitude = dest.latitude + 0.002
        }
        let span = MKCoordinateSpan.init(latitudeDelta: 0.01, longitudeDelta: 0.01)
        let region = MKCoordinateRegion(center: dest, span: span)
        mapView.setRegion(region, animated: true)


    }
白馒头 2024-10-29 02:48:40

我最终得到了以下过程:

** 以注释为中心:**

- (void) centerOnSelection:(id<MKAnnotation>)annotation
{
    MKCoordinateRegion region = self.mapView.region;
    region.center = annotation.coordinate;

    CGFloat per = ([self sizeOfBottom] - [self sizeOfTop]) / (2 * self.mapView.frame.size.height);
    region.center.latitude -= self.mapView.region.span.latitudeDelta * per;

    [self.mapView setRegion:region animated:YES];
}

** 缩放注释:**

- (void) zoomAndCenterOnSelection:(id<MKAnnotation>)annotation
{
    DLog(@"zoomAndCenterOnSelection");

    MKCoordinateRegion region = self.mapView.region;
    MKCoordinateSpan span = MKCoordinateSpanMake(0.005, 0.005);

    region.center = annotation.coordinate;

    CGFloat per = ([self sizeOfBottom] - [self sizeOfTop]) / (2 * self.mapView.frame.size.height);
    region.center.latitude -= self.mapView.region.span.latitudeDelta * span.latitudeDelta / region.span.latitudeDelta * per;

    region.span = span;

    [self.mapView setRegion:region animated:YES];
}

-(CGFloat) sizeOfBottom-(CGFloat) sizeOfTop 都返回布局指南中覆盖地图视图的面板高度

I ended up with following procedures:

** Centering on annotation:**

- (void) centerOnSelection:(id<MKAnnotation>)annotation
{
    MKCoordinateRegion region = self.mapView.region;
    region.center = annotation.coordinate;

    CGFloat per = ([self sizeOfBottom] - [self sizeOfTop]) / (2 * self.mapView.frame.size.height);
    region.center.latitude -= self.mapView.region.span.latitudeDelta * per;

    [self.mapView setRegion:region animated:YES];
}

** Zooming on annotation:**

- (void) zoomAndCenterOnSelection:(id<MKAnnotation>)annotation
{
    DLog(@"zoomAndCenterOnSelection");

    MKCoordinateRegion region = self.mapView.region;
    MKCoordinateSpan span = MKCoordinateSpanMake(0.005, 0.005);

    region.center = annotation.coordinate;

    CGFloat per = ([self sizeOfBottom] - [self sizeOfTop]) / (2 * self.mapView.frame.size.height);
    region.center.latitude -= self.mapView.region.span.latitudeDelta * span.latitudeDelta / region.span.latitudeDelta * per;

    region.span = span;

    [self.mapView setRegion:region animated:YES];
}

-(CGFloat) sizeOfBottom and -(CGFloat) sizeOfTop both return height of panels covering the mapview from the layout guides

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文