在我的地圖中,我必須通過URL顯示來自kml文件的註釋。我還需要僅顯示多邊形或圓形區域內的註釋(如果用戶同時繪製了兩個疊加層,則都需要顯示這兩個註釋)。僅在疊加層內顯示註釋
我已經看到了問題How to determine if an annotation is inside of MKPolygonView (iOS),但我有兩個困惑:
- 關於註釋座標,我應該用註釋的座標從
addAnnotation
方法? - 在上述問題中創建了一個新的疊加層,但我在其他地方創建了兩個不同的疊加層。所以我的問題是:什麼是最適合放這個代碼(或類似的東西)的地方?
編輯:我已經創建了一些代碼:
-(IBAction)showKmlData:(id)sender
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"KMLGenerator" ofType:@"kml"];
kml = [[KMLParser parseKMLAtPath:path] retain];
NSArray *annotationsImmut = [kml points];
//[mapview addAnnotations:annotations]; not anymore
NSMutableArray *annotations = [annotationsImmut mutableCopy];
[self filterAnnotations:annotations];
MKMapRect flyTo = MKMapRectNull;
for (id <MKAnnotation> annotation in annotations) {
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
if (MKMapRectIsNull(flyTo)) {
flyTo = pointRect;
} else {
flyTo = MKMapRectUnion(flyTo, pointRect);
}
}
mapview.visibleMapRect = flyTo;
}
-(void)filterAnnotations:(NSMutableArray *)annotationsToFilter {
for (int i=0; i<[annotationsToFilter count]; i++) {
CLLocationCoordinate2D mapCoordinate = [[annotationsToFilter objectAtIndex:i] coordinate];
MKMapPoint mapPoint = MKMapPointForCoordinate(mapCoordinate);
MKPolygonView *polygonView =
(MKPolygonView *)[mapView viewForOverlay:polygonOverlay];
MKCircleView *circleView =
(MKCircleView *)[mapView viewForOverlay:circleOverlay];
CGPoint polygonViewPoint = [polygonView pointForMapPoint:mapPoint];
CGPoint circleViewPoint = [circleView pointForMapPoint:mapPoint];
BOOL mapCoordinateIsInPolygon =
CGPathContainsPoint(polygonView.path, NULL, polygonViewPoint, NO);
BOOL mapCoordinateIsInCircle =
CGPathContainsPoint(circleView.path, NULL, circleViewPoint, NO);
if(mapCoordinateIsInPolygon || mapCoordinateIsInCircle)
[annotationsToFilter removeObjectAtIndex:i];
}
[mapView addAnnotations:annotationsToFilter];
}
編輯NR 2這是我實現viewForOverlay委託method.I看到我創建的疊加層,圓形和多邊形。我看到他們的所有annotations.ALL,這些內部和外部的覆蓋...
-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id)overlay
{
MKCircleView* circleView = [[[MKCircleView alloc] initWithOverlay:overlay] autorelease];
circleOverlay = circleView;
circleView.fillColor = [UIColor blueColor];
circleView.strokeColor = [UIColor blueColor];
circleView.lineWidth = 5.0;
circleView.alpha = 0.20;
MKPolygonView *polygonView = [[[MKPolygonView alloc] initWithOverlay:overlay] autorelease];
polygonOverlay = polygonView;
polygonView.fillColor = [UIColor blueColor];
polygonView.strokeColor = [UIColor blueColor];
polygonView.lineWidth = 5.0;
polygonView.alpha = 0.20;
if ([overlay isKindOfClass:[MKCircle class]])
{
return circleView;
}
else
return polygonView;
}
因此,您添加了兩個疊加層,並且您想要檢查列表中的哪個座標位於疊加層內?在鏈接的問題中,它不會創建新的疊加層 - 只需獲取對現有疊加層視圖的引用即可。 – Anna
哦,現在我明白了。但是註釋怎麼樣? kml文件只返回註釋,如何檢查它們是否在覆蓋內? – Hari
創建覆蓋圖,然後遍歷kml中的座標併爲每個座標執行該邏輯。嘗試一下,如果有任何問題,請發佈代碼。 – Anna