2012-03-20 68 views
0

我想要一个IBAction中心我的mapview的地区的注释。注释添加到的MapView这样的:如何检索地图注记坐标?

myAnnotation *pinLoc=[[myAnnotation alloc] init]; 
pinLoc.coordinate = mapview.centerCoordinate; 
pinLoc.title = @"Title"; 
pinLoc.subtitle = @"subtitle"; 
[mapview addAnnotation:pinLoc]; 

我想中检索,并以某种方式移动该地区的中心是这样的:

- (IBAction)findPin:(id)sender { 

    MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } }; 

    region.center.latitude = pinLoc.coordinate.latitude; 
    region.center.longitude = pinloc.coordinate.longitude; 
    region.span.latitudeDelta = 0.001f; 
    region.span.longitudeDelta = 0.001f; 
    [mapview setRegion:region animated:YES]; 
    [mapview setDelegate:self]; 
} 

我也在寻找一种方式来告诉我们,如果该针在多边形内。我已经在使用用户位置工作了,但同样,我需要检索引脚的坐标以使其与引脚一起工作。

回答

1

最简单的方法是在添加注释时存储对注释的引用。

例如,你可以声明称为myAnnotationpinToRemember并添加注释后保留的财产,这样做是为了保存以后:

[mapview addAnnotation:pinLoc]; 
self.pinToRemember = pinLoc; 

然后在findPin:,你会怎么做:

if (pinToRemember == nil) 
    return; 

region.center = pinToRemember.coordinate; 


或者,您可以搜索地图视图的annotations数组属性,然后通过查找您感兴趣的注释其title或其他一些自定义属性。这可以通过使用简单的for循环完成。


你说你有“办法告诉引脚是一个多边形内”的工作,但这个答案也可能是有用的:
How to determine if an annotation is inside of MKPolygonView (iOS)

+0

有时候,我看不出是正确的答案在我面前,谢谢安娜。您在StackOverflow上的存在为我在早期的iOS开发职业生涯中为我节省了无数次。我实际上习惯使用你的代码来确定一个点是否在一个多边形内! – 2012-03-20 09:23:20