【问题标题】:Multiple MKPolyline on MKMapView iOS6MKMapView iOS6上的多个MKPolyline
【发布时间】:2012-11-28 03:37:13
【问题描述】:

我正在与MKMapView 合作使用MKPolyline 绘制路线,我可以使用一组航点绘制单条路线。现在我想在MKMapView 上绘制多个MKPolylines,例如,A蓝色MKPolyline28.102021, 77.1012928.20320, 77.3021 和红色MKPolyline28.50930, 77.8919228.60291, 77.87328。我怎样才能做到这一点?

代码:

- (void)viewDidLoad
{
[super viewDidLoad];

locationManager = [[CLLocationManager alloc] init];
locationManager.desiredAccuracy = 6.0;
locationManager.distanceFilter = 1.0;
[locationManager startUpdatingLocation];
locationManager.delegate = self;
map.delegate = self;
[map setShowsUserLocation:YES];
[map setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
wayPoints = [[NSMutableArray alloc] initWithCapacity:30];
newWayPoints = [[NSMutableArray alloc]initWithCapacity:10];
totalDistance = 0.0;

stopTime = [NSDate dateWithTimeIntervalSinceNow:100];
startTime = [NSDate date];

SEL sel = @selector(timerTargetMethod);
NSInvocation* inv = [NSInvocation invocationWithMethodSignature:
                     [self methodSignatureForSelector:sel]];
[inv setTarget:self];
[inv setSelector:sel];

stopTimer = [NSTimer scheduledTimerWithTimeInterval:5 invocation:inv repeats:true];
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}



- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{
if(newLocation != nil && oldLocation != newLocation)
{
    tempNewLocation = newLocation;
    tempOldLocation = oldLocation;

}

}


- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views
{    
MKAnnotationView *annotationView = [views objectAtIndex:0];
id<MKAnnotation> mp = [annotationView annotation];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate] ,4000,4000);

[mv setRegion:region animated:YES];
}

// MKMapViewDelegate
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
MKOverlayView* overlayView = nil;
MKPolylineView  * routeLineView = [[MKPolylineView alloc] initWithPolyline:self.routeLine];
routeLineView.fillColor = [UIColor colorWithRed:0.0-1.0 green:0.0-1.0 blue:0.0-1.0 alpha:1.0f];
routeLineView.strokeColor = [UIColor colorWithRed:0.0-1.0 green:0.0-1.0 blue:0.0-1.0 alpha:1.0f];

routeLineView.lineWidth = 3;
routeLineView.lineCap = kCGLineCapSquare;
overlayView = routeLineView;
return overlayView;

}

//define the targetmethod
-(void) timerTargetMethod 
{

if([[NSDate date] timeIntervalSinceDate:startTime] >= 100)
{
    [stopTimer invalidate];
    [locationManager stopUpdatingLocation];
    NSLog(@"Time started at %@", startTime);
    NSLog(@"Time up at %@", stopTime);
}
else if (tempOldLocation.coordinate.latitude == tempNewLocation.coordinate.latitude   && tempNewLocation.coordinate.longitude == tempOldLocation.coordinate.longitude) 
{
    NSLog(@" Fix location found ");
}
else if( [[NSDate date] timeIntervalSinceDate:startTime] >= 19 )
{
    if(roundf([[NSDate date] timeIntervalSinceDate:startTime]) == 20)
    {
        NSLog(@"First Time Location Update");
        latitudeLongitude.text = [[ NSString alloc] initWithFormat:@"%g , %g", tempNewLocation.coordinate.latitude, tempNewLocation.coordinate.longitude];

        float interval = [[NSDate date] timeIntervalSinceDate:startTime];
        int okInterval = roundf(interval);
        NSLog(@"Interval 1 , %d", okInterval );
        time.text = [[ NSString alloc] initWithFormat:@"%d", okInterval  - 20];
        speed.text = @"0";
        totalDistance =  0;
        distance.text = @"0 meters";
    }
    else
    {
        latitudeLongitude.text = [[ NSString alloc] initWithFormat:@"%g , %g", tempNewLocation.coordinate.latitude, tempNewLocation.coordinate.longitude];

        float interval = [[NSDate date] timeIntervalSinceDate:startTime];
        int okInterval = roundf(interval);
        time.text = [[ NSString alloc] initWithFormat:@"%d", okInterval  - 20];
        NSLog(@"Interval 2 , %d , %f", okInterval , interval);
        if((tempNewLocation.coordinate.latitude == tempOldLocation.coordinate.latitude && tempNewLocation.coordinate.longitude == tempOldLocation.coordinate.longitude) || tempNewLocation.speed < 0)
            speed.text = @"0";
        else
            speed.text = [[ NSString alloc] initWithFormat:@"%g meters/sec", tempNewLocation.speed];

        if(tempNewLocation.coordinate.latitude == tempOldLocation.coordinate.latitude && tempNewLocation.coordinate.longitude == tempOldLocation.coordinate.longitude)
        {

        }
        else if ([tempNewLocation distanceFromLocation:tempOldLocation] - tempNewLocation.horizontalAccuracy >= 0) 
            totalDistance +=  [tempNewLocation distanceFromLocation:tempOldLocation] - (tempNewLocation.horizontalAccuracy / 2);
        else
            totalDistance +=  [tempNewLocation distanceFromLocation:tempOldLocation];

        if (totalDistance < 0) 
            distance.text = @"0 meters";
        else
            distance.text = [[ NSString alloc] initWithFormat:@"%g meters", totalDistance];
    }


    [wayPoints addObject:tempNewLocation];
    MKMapPoint * pointsArray =
    malloc(sizeof(CLLocationCoordinate2D)*2);

    pointsArray[0]= MKMapPointForCoordinate(tempOldLocation.coordinate); 
    pointsArray[1]= MKMapPointForCoordinate(tempNewLocation.coordinate);

    routeLine = [MKPolyline polylineWithPoints:pointsArray count:2];
    free(pointsArray);

    if (tempNewLocation.coordinate.latitude - tempOldLocation.coordinate.latitude < 1) {
        [map addOverlay:routeLine];

    }
}
}

谢谢

【问题讨论】:

  • 即使您不使用我在下面的答案,您也应该采取一些措施。在viewForOverlay 中,您将忽略作为参数给出的覆盖并使用类变量routeLine。如果您希望在地图上有两个叠加层,则需要根据地图要求的视图返回一个视图。另外,这个“0.0-1.0”的东西是什么?只需使用“-1”或预制颜色,如[UIColor whiteColor]

标签: ios mkmapview polyline


【解决方案1】:

您在此处显示了很多代码,唯一相关的是viewForOverlay。在那里,您将获得一个符合 MKOverlay 协议的对象,如果您创建了自己的类来实现它,您可以添加一个变量,该变量可用于识别您当时正在处理的叠加层,从而调整您的颜色。再往下看你的代码,我看到你正在创建MKPolylines,这些是传递给viewForOverlay的叠加层,因为它们继承自MKShape,它们具有titlesubTitle可以使用的属性。

因此,当您创建MKPolyline 时,您可以设置title,可能设置为“typeA”和“typeB”,然后在viewForOverlay 中,您需要将叠加层投射到 MKPolyline 并检查它必须使用哪个标题决定使用哪种颜色:

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    if([overlay class] == MKPolyline.class)
    {
        MKOverlayView* overlayView = nil;
        MKPolyline* polyline = (MKPolyline *)overlay;
        MKPolylineView  * routeLineView = [[MKPolylineView alloc] initWithPolyline:polyline];
        if([polyline.title isEqualToString:@"typeA"])
        {
            routeLineView.fillColor = [UIColor redColor];
            routeLineView.strokeColor = [UIColor redColor];
        } else {
            routeLineView.fillColor = [UIColor blueColor];
            routeLineView.strokeColor = [UIColor blueColor];

        }
        routeLineView.lineWidth = 3;
        routeLineView.lineCap = kCGLineCapSquare;
        overlayView = routeLineView;
        return overlayView;
    } else {
        return nil;
    }
}

【讨论】:

  • 感谢@Craig 的帮助。
  • 再问一个问题,我可以添加 2 个MKPolylines,如果我像添加第二个一样添加第三个,它不会出现。我们可以添加多少MKPolylines 有限制吗?
  • 如果有限制,它不是 2,更有可能是几十个或几百个。您可能混淆了变量,使两个变量具有相同的坐标,或者将一个变量置于当前地图视图之外。如果您无法解决,请使用一些代码发布另一个问题。
  • 感谢 Craig,问题已解决。我对 2 MKPolylines 使用相同的坐标。
  • @Craig:抱歉,我不知道如何与您联系:在对 SO stackoverflow.com/a/16167959/1987726> 的回答中,我曾经提到过如何将折线添加到 mapView 的教程.链接 spitzkoff.com/craig/?p=136> 现在已断开。我认为这是您编写的教程。可以在其他地方找到,以便我更新我的答案吗?
猜你喜欢
  • 1970-01-01
  • 2013-01-29
  • 1970-01-01
  • 2016-05-20
  • 1970-01-01
  • 1970-01-01
  • 2012-10-17
  • 1970-01-01
  • 2012-09-16
相关资源
最近更新 更多