【问题标题】:Render mapLines in MKMapView my map get frozen在 MKMapView 中渲染 mapLines 我的地图被冻结
【发布时间】:2013-01-30 15:57:47
【问题描述】:

大家好,希望大家能帮到我:

- (IBAction) addPoints: (id) sender
{
NSArray *latitudePoints;
NSArray *longitudePoints;
BOOL flag = NO;
@try
{
switch (lugar)
{
    case 0:
        break;

    case 1:
        latitudePoints = @[@27.781361, @27.777894, @27.766683, @27.757066, @27.745613, @27.737327, @27.728247, @27.720827, @27.720866, @27.719083, @27.722266, @27.721097, @27.739263, @27.758177, @27.780688];
        longitudePoints = @[@-15.714188, @-15.720175, @-15.726072, @-15.740874, @-15.746066, @-15.759544, @-15.763944, @-15.774408, @-15.764822, @-15.752658, @-15.735975, @-15.715497, @-15.718150, @-15.712244, @-15.713213];
        flag = YES;
        break;

    case 2:
        latitudePoints = @[@28.249792, @28.254583, @28.259865, @28.264000, @28.265621, @28.267694, @28.270308, @28.272192, @28.273966, @28.275698, @28.277723, @28.279901];
        longitudePoints = @[@-16.841880, @-16.842477, @-16.842657, @-16.844832, @-16.848396, @-16.851215, @-16.852329, @-16.856142, @-16.859281, @-16.860882, @-16.862011, @-16.863116];
        flag = YES;
        break;

    default:
        [NSException raise:@"Unknown place" format:@"Unknown place"];
        break;
}
// NSLog(@"Lugar %d Flag %d",lugar,flag);
if (flag && [longitudePoints count] == [latitudePoints count])
{
    anotacionesMapa *point; // anotacionesMapa is a custom MKAnnotation class, just have title, subtitle and coord atribute
    CLLocationCoordinate2D placePoint;
    NSMutableArray *pointList = [[NSMutableArray alloc] initWithCapacity:[longitudePoints count]];
    for (int c = 0; c < [longitudePoints count]; c++)
    {
        placePoint.latitude = [[latitudePoints objectAtIndex:c] doubleValue];
        placePoint.longitude = [[longitudePoints objectAtIndex:c] doubleValue];
        if (c % 2 == 0)
            point = [[anotacionesMapa alloc] initWithTitle:[NSString stringWithFormat:@"[Picture here] Nothing"]
                                                  subtitle:@""
                                             andCoordinate: placePoint];
        else
            point = [[anotacionesMapa alloc] initWithTitle:[NSString stringWithFormat:@"Something"]
                                                  subtitle:@"[Pic here] test"
                                             andCoordinate: placePoint];
        [self.mapView addAnnotation:point];
        CLLocation *linePoint = [[CLLocation alloc] initWithLatitude:placePoint.latitude longitude:placePoint.longitude];
        [pointList addObject: linePoint];
    }
    ruta = [[CSMapRouteLayerView alloc] initWithRoute:pointList
                                              mapView:self.mapView];
}
}
@catch (NSException *exception)
{
UIAlertView *alert;
alert = [[UIAlertView alloc] initWithTitle:@"Error..."
                                   message:[NSString stringWithFormat:@"%@", exception]
                                  delegate:self
                         cancelButtonTitle:@"Aceptar"
                         otherButtonTitles:nil];
[alert show];
}
}

我有这段代码,然后我需要打印存储纬度和经度的 PuntosLT 和 PuntosLG 坐标之间的线,但是当它应该打印线时(实际上,这些线以蓝色打印,我想打印红色)MKMapView 被冻结,无法移动,也无法缩放,我认为它会出现错误并绘制不在坐标中的其他线条,最后我的注释中的自定义图像又是红色针脚,喜欢默认和标题,副标题等信息不起作用。

谢谢。

编辑:方法/构造函数

-(id) initWithRoute:(NSArray*)routePoints mapView:(MKMapView*)mapView
{
self = [super initWithFrame:CGRectMake(0, 0, mapView.frame.size.width, mapView.frame.size.height)];
[self setBackgroundColor:[UIColor clearColor]];

[self setMapView:mapView];
[self setPoints:routePoints];

// determine the extents of the trip points that were passed in, and zoom in to that area. 
CLLocationDegrees maxLat = -90;
CLLocationDegrees maxLon = -180;
CLLocationDegrees minLat = 90;
CLLocationDegrees minLon = 180;

for(int idx = 0; idx < self.points.count; idx++)
{
    CLLocation* currentLocation = [self.points objectAtIndex:idx];
    if(currentLocation.coordinate.latitude > maxLat)
        maxLat = currentLocation.coordinate.latitude;
    if(currentLocation.coordinate.latitude < minLat)
        minLat = currentLocation.coordinate.latitude;
    if(currentLocation.coordinate.longitude > maxLon)
        maxLon = currentLocation.coordinate.longitude;
    if(currentLocation.coordinate.longitude < minLon)
        minLon = currentLocation.coordinate.longitude;
}

MKCoordinateRegion region;
region.center.latitude     = (maxLat + minLat) / 2;
region.center.longitude    = (maxLon + minLon) / 2;
region.span.latitudeDelta  = maxLat - minLat;
region.span.longitudeDelta = maxLon - minLon;

[self.mapView setRegion:region];
[self.mapView setDelegate:self];
[self.mapView addSubview:self];

return self;
}

【问题讨论】:

  • 您的代码中缺少某些内容。您添加了 MKAnnotation 标签,但您似乎没有使用它。此外,更改变量名称以便它们可以用英文阅读将非常有用。
  • 应用是否使用 ARC?另外,如果可能,请在CSMapRouteLayerView 中显示initWithRoute:mapView: 的代码。
  • 使用@try / @catch 非常不合可可!
  • 嗨,@AnnaKarenina 我的应用程序使用 ARC,这是我为测试 MKMapView 而不是“官方”应用程序所做的尝试测试应用程序...我编辑并添加了该方法。那么Abizem为什么是un-Cocoa,你用什么来检查错误或者异常呢?因为可可抛出了太多异常...
  • 我们使用 Exceptions 来捕获程序员错误,并使用 NSError 来处理运行时条件。有一个完整的Error Handling Programming Guide 值得您阅读。

标签: ios google-maps mkmapview mkannotation mkannotationview


【解决方案1】:

我修复了使用来自苹果 mapkit 的一些新变量,我删除了 CSMapRouteLayerView 库。我补充说:

MKPolyline *routeLine;
MKPolylineView *routeLineView;

这个代码在我的问题方法中添加行:

- (IBAction) addPuntos: (id) sender
{
NSArray *puntosLt;
NSArray *puntosLg;
BOOL flag = NO;
@try
{
    switch (lugar)
    {
        case 0:
            break;

        case 1:
            puntosLt = @[@27.781361, @27.777894, @27.766683, @27.757066, @27.745613, @27.737327, @27.728247, @27.720827, @27.720866, @27.719083, @27.722266, @27.721097, @27.739263, @27.758177, @27.780688];
            puntosLg = @[@-15.714188, @-15.720175, @-15.726072, @-15.740874, @-15.746066, @-15.759544, @-15.763944, @-15.774408, @-15.764822, @-15.752658, @-15.735975, @-15.715497, @-15.718150, @-15.712244, @-15.713213];
            flag = YES;
            break;

        case 2:
            puntosLt = @[@28.249792, @28.254583, @28.259865, @28.264000, @28.265621, @28.267694, @28.270308, @28.272192, @28.273966, @28.275698, @28.277723, @28.279901];
            puntosLg = @[@-16.841880, @-16.842477, @-16.842657, @-16.844832, @-16.848396, @-16.851215, @-16.852329, @-16.856142, @-16.859281, @-16.860882, @-16.862011, @-16.863116];
            flag = YES;
            break;

        default:
            [NSException raise:@"Lugar desconocido" format:@"Lugar desconocido"];
            break;
    }
    // NSLog(@"Lugar %d Flag %d",lugar,flag);
    if (flag && [puntosLg count] == [puntosLt count])
    {
        anotacionesMapa *punto;
        CLLocationCoordinate2D lugarPunto;
        CLLocationCoordinate2D puntitos[[puntosLg count]];
        for (int c = 0; c < [puntosLg count]; c++)
        {
            lugarPunto.latitude = [[puntosLt objectAtIndex:c] doubleValue];
            lugarPunto.longitude = [[puntosLg objectAtIndex:c] doubleValue];
            punto = [[anotacionesMapa alloc] initWithTitle:[NSString stringWithFormat:@"[Foto barco] Sin avistamientos"]
                                                  subtitle:@""
                                             andCoordinate: lugarPunto];
            [self.mapView addAnnotation:punto];
            puntitos[c] = CLLocationCoordinate2DMake(lugarPunto.latitude, lugarPunto.longitude);
        }
        self.routeLine = [MKPolyline polylineWithCoordinates:puntitos count:[puntosLg count]];
        [self.mapView setVisibleMapRect: [self.routeLine boundingMapRect]];
        [self.mapView addOverlay: self.routeLine];
    }
}
@catch (NSException *exception)
{
    UIAlertView *alert;
    alert = [[UIAlertView alloc] initWithTitle:@"Error..."
                                       message:[NSString stringWithFormat:@"%@", exception]
                                      delegate:self
                             cancelButtonTitle:@"Aceptar"
                             otherButtonTitles:nil];
    [alert show];
}
}

PD:我不会使用@try/@catch,这是我使用 MKMapView 的示例应用,所以我会将它们保留在这个迷你项目中,其余的我将遵循 http://developer.apple.com/library/ios/#Documentation/Cocoa/Conceptual/ErrorHandlingCocoa/ErrorHandling/ErrorHandling.html

谢谢。

【讨论】:

  • 是的,使用真正的 MKOverlay 对象比 CSMapRouteLayerView 似乎正在做的更好(将 UIView 子视图添加到地图视图)。但请注意,如果您计划一次添加多个叠加层,则需要停止使用单个 routeLine 参考(来自一些不考虑多个叠加层的教程)。
  • 我只需要一个叠加层,所以我每次添加新点(案例 1 或案例 2)时都使用if ([anotaciones count] &gt;= 1) { **self.routeLineView = nil;** self.routeLine = nil; [self.mapView removeOverlay: routeLine]; [self.mapView removeAnnotations: anotaciones]; [anotaciones removeAllObjects]; },因为我只需要其中一个,即使将来我添加 2000案例只会显示 1,如果我显示其他,上一个。如果您打印一个新的,将删除原因是因为您将不再使用最后一个。
猜你喜欢
  • 1970-01-01
  • 2017-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多