简体   繁体   English

iOS Mapview-缩放至图钉,然后激活标注

[英]iOS mapview - zoom to pin and then activate callout

I have a mapview in xcode, which is all working well. 我在xcode中有一个mapview,一切正常。

What my page does just now is like this: 我的页面现在所做的是这样的:

  1. downloads a bunch of data and locations from a backend database 从后端数据库下载一堆数据和位置
  2. populates a mapview with locations and drops pins 使用位置填充地图视图并放下图钉
  3. populates a table underneath the mapview 在地图视图下方填充表格

That all works great, and I end up with a mapview with a load of pins, and a tableview that has the details of those pins. 一切都很好,最后我得到了一个带有大量图钉的mapview和一个包含这些图钉细节的表格视图。

What I want to do now, is allow the user to tap on a row from the tableview, and have the map zoom and centre to the corresponding map pin, and then automatically activate the annotation pin callout. 我现在想做的是允许用户点击表格视图中的一行,并使地图缩放并居中到相应的地图图钉,然后自动激活注释图钉标注。

In my 'didselectrow' method, I have the following: 在我的'didselectrow'方法中,我具有以下内容:

MKCoordinateSpan span = MKCoordinateSpanMake(0.1f, 0.1f);
CLLocationCoordinate2D coordinate = { [item.latitude floatValue], [item.longitude floatValue] };
MKCoordinateRegion region = { coordinate, span };
[self.mapView setRegion:region animated:YES];

This works great too. 这也很好。 Tapping on the table row will zoom to and centre the map pin at this location. 点击表格行将放大地图图钉并将其定位在此位置。

I just can't get the last step of firing the annotation pin callout to work. 我就是无法启动注释引脚标注的最后一步。

I have tried: 我努力了:

[mapview annotationsInMapRect:mapview.visibleMapRect];

But this isn't working, and it is possible that there still might be 2 or 3 map pins in the visible area. 但这是行不通的,并且可能在可见区域中仍可能有2或3个地图图钉。

What I need to do is to get the pin nearest to the centred location (see above - item.latitude / item.longitude) to automatically open it's callout. 我需要做的是使图钉最靠近中心位置(请参见上文-item.latitude / item.longitude)以自动打开它的标注。

Everything in the code is set up and working, and the map pins have callouts that fire when tapped on, I just need this last stage of having the pin nearest the centre location to open automatically. 代码中的所有内容均已设置并正常工作,并且在点击图钉时会触发标注,我只需要最后一步使图钉靠近中心位置即可自动打开。

Can anyone help with this? 有人能帮忙吗?

I have tried various other suggestions on SO, but none seem to fit this requirement. 我曾尝试过其他有关SO的建议,但似乎没有一个适合此要求。

I think I have got solution for your problem you need to use this [_mapView setSelectedAnnotations:@[[[self.mapView annotations] lastObject]]]; 我想我已经为您的问题找到了解决方案,您需要使用此[_mapView setSelectedAnnotations:@[[[self.mapView annotations] lastObject]]];

For testing I have created an small project that have these 2 methods . 为了进行测试,我创建了一个具有这两种方法的小项目

- (IBAction)buttonTouched:(id)sender {
    [_mapView showAnnotations:[self.mapView annotations] animated:YES];
    [self performSelector:@selector(showAnnotationCallOut) withObject:nil afterDelay:1.0f];
}

- (void) showAnnotationCallOut {
    [_mapView setSelectedAnnotations:@[[[self.mapView annotations] lastObject]]];
}

Note: I have called just one annotation for test that why I am calling last object. 注意:我只调用了一个注释来测试为什么调用最后一个对象。 You'll need to call it for specific annotation of your annotation array. 您需要为注释数组的特定注释调用它。

Edit: According to Richerd's comment here is solution for problem of finding the annotion and showing the callout fro that. 编辑:根据Richerd的评论,这里是找到注释并显示标注的解决方案。

    for (MapViewAnnotation *annotion in [self.mapView annotion]) {
        if ([annotion.identifire isEqualToString:annotationToCallCallOutIdentifier]) {
            //[_mapView setSelectedAnnotations:@[annotation]];
            [_mapView selectAnnotation:annotation animated:YES];
            break;//don't break if there are can be more than one callouts
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM