3

我已经根据坐标创建了一个MKMapViewwith s 。MKPolygon地图上有多个多边形(在此处查看我作为应用程序重新创建的示例)。

我想要做的是当用户触摸多边形时,它会打开一个包含位置信息的弹出视图。此信息当前存储在带有坐标的 plist 文件中。

到目前为止,我能够获得触摸事件并打印到多边形被触摸的日志。

我的问题是:
一旦用户点击 pin,就 可以MKPolygonViewMKAnnotationViewwhere 一样使用更多关于当前位置的信息?

我想对多边形视图做同样的事情。触摸时,用户将看到有关存储在 plist 中的位置的更多信息。如果可能的话,让它发挥作用的最佳方法是什么?

我当前的代码如下。

#import "outagemapViewController.h"
#import "MyAnnotation.h"
#import "WildcardGestureRecognizer.h"
#define METERS_PER_MILE 46309.344
@interface outagemapViewController ()

@end

@implementation outagemapViewController
- (void)viewDidLoad {

outages = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"outages"ofType:@"plist"]];


for (NSDictionary *coloredAreas in outages) {
   coordinateData = coloredAreas[@"coords"];
test = coloredAreas[@"outages"];

    NSLog(@"test %@", test);
    coordsLen = [coordinateData count];
      NSLog(@"coords %d", coordsLen);
    CLLocationCoordinate2D coords[coordsLen];
    for (i=0; i < coordsLen; i++) {
        NSString *lat = coordinateData[i];
        NSArray *latt = [lat componentsSeparatedByString:@","];
        double latitude = [[latt objectAtIndex:0] doubleValue];
        double longitude = [[latt objectAtIndex:1] doubleValue];
       coords[i] = CLLocationCoordinate2DMake(latitude, longitude);

    }


 MKPolygon* poly2 = [MKPolygon polygonWithCoordinates:coords count:coordsLen];
 poly2.title=@"test";
 [self.mapView addOverlay:poly2];
 }
 }
 - (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {
{
    if ([overlay isKindOfClass:[MKPolygon class]])
    {

        MKPolygonView*    aView = [[MKPolygonView alloc] initWithPolygon:(MKPolygon*)overlay];

        int numbers = [test intValue];

        if(numbers >= 10){
            aView.fillColor = [[UIColor greenColor] colorWithAlphaComponent:0.6];
            aView.strokeColor = [[UIColor greenColor] colorWithAlphaComponent:1.0];
            aView.lineWidth = 3;
        }else if(numbers < 10){
            aView.fillColor = [[UIColor yellowColor] colorWithAlphaComponent:0.6];
            aView.strokeColor = [[UIColor yellowColor] colorWithAlphaComponent:1.0];
            aView.lineWidth = 3;

                    }

        return aView;
    }


    return nil;
}
 }

-(void)viewWillAppear:(BOOL)animated{
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = 35.20418;
zoomLocation.longitude = -89.86862;

MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation,         0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);

[_mapView setRegion:viewRegion animated:YES];

WildcardGestureRecognizer * tapInterceptor = [[WildcardGestureRecognizer alloc] init];
tapInterceptor.touchesBeganCallback = ^(NSSet * touches, UIEvent * event) {
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.mapView];

    CLLocationCoordinate2D coord = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
    MKMapPoint mapPoint = MKMapPointForCoordinate(coord);
    for (id overlay in self.mapView.overlays)
    {
        if ([overlay isKindOfClass:[MKPolygon class]])
        {
            MKPolygon *poly = (MKPolygon*) overlay;
            id view = [self.mapView viewForOverlay:poly];
            if ([view isKindOfClass:[MKPolygonView class]])
            {
                MKPolygonView *polyView = (MKPolygonView*) view;
                CGPoint polygonViewPoint = [polyView pointForMapPoint:mapPoint];
                BOOL mapCoordinateIsInPolygon = CGPathContainsPoint(polyView.path, NULL, polygonViewPoint, NO);
                if (mapCoordinateIsInPolygon) {
                   // debug(@"hit!");
                    NSLog(@"hit");
                } else {
                     NSLog(@"miss");
                }
            }
        }
    }

};
[self.mapView addGestureRecognizer:tapInterceptor];
}

- (void)didReceiveMemoryWarning
{
 [super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end
4

1 回答 1

3

不幸的是,对于叠加层,没有像注释那样的内置触摸检测和标注视图。

您必须像已经在做的那样手动进行触摸检测(看起来应该可以工作)。
(更不幸的是,直接将手势识别器添加到覆盖视图中是行不通的——您必须将其添加到整个地图中,然后检查触摸点是否在任何覆盖中。)

对于覆盖标注视图,一旦您检测到覆盖上的触摸,您可以创建自定义UIView并执行addSubview. 我建议将其添加到地图而不是覆盖视图中,您也许可以使用CGPoint point您已经在计算的来确定自定义标注视图的框架。

您可能还希望保留对覆盖标注视图的 ivar/属性引用,以便在用户点击另一个覆盖层而另一个覆盖层的标注已经显示时可以轻松删除和重新添加它。

另一个可能更容易的选项是创建自定义UIViewController并呈现或推送它。显示它的细节取决于您是否使用导航控制器和/或情节提要。

如果您的应用程序也是为 iPad 构建的,您还可以使用UIPopoverController.
请参阅如何将 UIPopoverView 显示为地图视图的注释?(iPad)用于代码示例(它带有注释,但您应该能够对其进行调整以适应覆盖)。


一旦您确定了哪个覆盖被点击,您需要显示原始数据源(outages数组)中的相关数据。现在,创建并添加了叠加层,但没有对原始数据对象(outages数组中的中断字典)的引用。

MKPolygon添加自定义属性的子类化存在问题和解决方法,创建完全自定义的MKOverlay类会引入许多其他额外的工作。)

对于您当前的数据源结构,一个简单、快速(并且有点粗略)的选项是将覆盖的title属性设置为outages与覆盖关联的中断对象的数组中的索引。由于title属性是 anNSString并且数组索引是整数,我们将其转换为字符串:

NSUInteger outageIndex = [outages indexOfObject:coloredAreas];
poly2.title = [NSString stringWithFormat:@"%d", outageIndex];
[self.mapView addOverlay:poly2];

viewForOverlay中,您似乎正在使用test(来自中断对象)来确定多边形的颜色。外部声明/设置test变量的值不一定与overlay当前调用的委托方法同步(映射可以viewForOverlay为同一个叠加层调用多次,不一定按照添加它们的顺序)。您必须根据overlay参数的某些属性来检索中断对象。由于我们将覆盖的title属性设置为中断的索引:

//int numbers = [test intValue];  <-- remove this line

int outageIndex = [overlay.title intValue];
NSDictionary *outageDict = [outages objectAtIndex:outageIndex];
id outageNumbersObject = outageDict[@"outages"]; 
//replace id above with actual type
//can't tell from code in question whether it's NSString or NSNumber 
int numbers = [outageNumbersObject intValue];

//use "numbers" to set polygon color...

最后,当点击覆盖时,您可以使用与中相同的方法viewForOverlay来获取中断对象:

if (mapCoordinateIsInPolygon) {
    int outageIndex = [overlay.title intValue];
    NSDictionary *outageDict = [outages objectAtIndex:outageIndex];
    NSLog(@"hit, outageDict = %@", outageDict);
    //show view with info from outageDict...
} 
于 2012-12-05T16:04:01.400 回答