MKMapView iOS6上有多个MKPolyline [英] Multiple MKPolyline on MKMapView iOS6

查看:96
本文介绍了MKMapView iOS6上有多个MKPolyline的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 MKMapView 使用 MKPolyline 绘制路线,我能够使用数组绘制单个路线waypoints。现在我想在 MKMapView 上绘制多个 MKPolyline ,例如,A blue MKPolyline 28.102021,77.10129 28.20320,77.3021 和红色 MKPolyline 28.50930,77.89192 28.60291,77.87328 。我怎样才能做到这一点?

I am working with MKMapView to draw route using MKPolyline, I am able to draw single route with an array of waypoints.Now I want to draw multiple MKPolylines on the MKMapView for instance, A blue MKPolyline from 28.102021, 77.10129 to 28.20320, 77.3021 and A red MKPolyline from 28.50930, 77.89192 to 28.60291, 77.87328. How can I achieve this ?

代码:

- (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 。在那里,您将获得一个符合 MKOverlay 协议的对象,如果您已创建自己的类来实现,则可以添加一个可用于标识哪个叠加层的变量那时你正在处理,从而调整你的颜色。在你的代码中,我看到你正在创建 MKPolyline s,这些是传递给 viewForOverlay 的叠加层,因为它们继承了来自 MKShape 他们有 title subTitle 您可以使用的属性。

That's a lot of code you've shown there, the only bit that is relevant is viewForOverlay. In there you are given an object that conforms to the MKOverlay protocol and if you have created your own class to implement that you could add a variable that could be used to identify which overlay you were dealing with at that point and thus adjust your colours. Further down your code I see you are creating MKPolylines, those are the overlays passed in to viewForOverlay and because they inherit from MKShape they have title and subTitle attributes you can use.

因此,当您创建 MKPolyline 时,您可以设置标题,也许是typeA和typeB,然后在 viewForOverlay 中你需要将叠加层转换为MKPolyline并检查它有哪个标题来决定哪个颜色为使用:

So when you create the MKPolyline you could set the title, maybe to "typeA" and "typeB", then in viewForOverlay you need to cast the overlay to a MKPolyline and check which title it has to decide which colour to use:

- (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;
    }
}

这篇关于MKMapView iOS6上有多个MKPolyline的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆