MKMapView iOS6 上的多个 MKPolyline

2023-12-19

我正在与MKMapView使用绘制路线MKPolyline,我可以用一组航路点绘制单个路线。现在我想绘制多个MKPolyline是在MKMapView例如,蓝色MKPolyline from 28.102021, 77.10129 to 28.20320, 77.3021和一个红色MKPolyline from 28.50930, 77.89192 to 28.60291, 77.87328。我怎样才能做到这一点?

Code:

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

    }
}
}

Thanks


您在那里展示了很多代码,唯一相关的是viewForOverlay。在那里你会得到一个符合以下条件的对象MKOverlay协议,如果您创建了自己的类来实现该协议,则可以添加一个变量,该变量可用于识别您当时正在处理的覆盖层,从而调整您的颜色。在你的代码下面我看到你正在创建MKPolylines,这些是传递给的覆盖层viewForOverlay因为他们继承自MKShape他们有title and subTitle您可以使用的属性。

所以当你创建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;
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

MKMapView iOS6 上的多个 MKPolyline 的相关文章

  • iOS App布局错误,调用状态栏

    在主动通话和应用程序布局期间面临状态栏问题 我正在使用自动布局 当我运行应用程序 然后开始通话时 一切正常 UI 会随着状态栏的更改而正确缩放 但是 如果我首先开始通话 然后运行应用程序 应用程序屏幕会移动到底部 20pt 就像它们对新状态
  • 如何在我的 iOS 项目中添加和执行 .sql 文件?

    我找到了很多关于在 iOS 中使用 SQLite 数据库的教程 但没有找到任何直接引用 sql 文件的内容 谁能告诉我如何将现有的 SQL 数据库链接到我的应用程序 编辑 这是一个 MySQL 转储 我们有一个基于浏览器的抽认卡程序 现在我
  • iOS:从非图像数据生成图像(Godus,如风景)

    所以看到图像后Godus http www kickstarter com projects 22cans project godus我想知道如何生成简单的 非交互式的 2D 图像 with 不同高度或层数的颜色不同就像下面的图片一样 我只
  • 在 Interface Builder 中的资产目录上使用图像

    是否可以直接在界面生成器上使用添加到资产目录中的图像 这是怎么做到的 在 UIImageView 属性上 我看不到任何引用资产目录上任何图像的选项 Import the images into the xcassets folder 单击右
  • UIBezierPath 的起始和结束角度?

    我在 iOS 中使用如下代码编写了半圆UI贝塞尔路径 and CAShape层 clockWiseLayer CAShapeLayer alloc init CGFloat startAngle M PI 2 CGFloat endAngl
  • 如何从 NSString 中删除十六进制字符

    我面临一个与字符串中的某些十六进制值相关的问题 我需要从字符串中删除十六进制字符 The problem is when i print object it prints as BLANK line And in debug mode it
  • iPad 3 中配备 Xcode 4.2 和 Retina 的 iOS 5.1

    我有一台装有 Mac OS X Snow Leopard 的 Mac 我可以添加 iOS 5 1 吗 使用 iPad 3 的新分辨率 我们将如何处理图像 因为如果该应用程序将在 iPhone 3GS 4 和 iPad 3 中运行 我认为我们
  • 在模拟器中运行应用程序时删除本地通知的 iOS 权限警报

    我正在尝试编写验收测试KIF https github com kif framework KIF在一个很早就要求本地通知权限的应用程序上 不幸的是 由于 iOS 模拟器安全原因无法使用 KIF 自动接受 iOS 权限警报 https gi
  • 打印附加结构(swift 4)

    我有三个 textifled 用于将数据附加到结构中 如何打印我附加的内容 现在我收到一条错误消息 import UIKit class ViewController UIViewController IBOutlet var c UITe
  • Cognito/IAM 策略和 S3 获取对象

    我正在尝试将 S3 和 Cognito 集成到我的 iOS 应用程序中 但到目前为止尚未成功 我相信该错误与我针对 Auth 和 Unauth 用户的 IAM 策略有关 所以这是我的政策 Version 2012 10 17 Stateme
  • 通用类不会将委托调用转发给具体子类

    鉴于以下情况 protocol EntityType var displayString String get extension String EntityType var displayString String return self
  • UICollectionView 列的垂直偏移

    右图是我试图实现的目标 Does anyone know how I could achieve this on a two column UICollectionView I m able to discern my columns by
  • 如何比 CGContextStrokePath 更快地渲染线条?

    我正在使用 CGContextStrokePath 绘制约 768 个点的图表 问题是 每一秒我都会得到一个新的数据点 从而重新绘制图表 目前 这个已经很繁忙的应用程序占用了 50 的 CPU 图形绘制是在UIView 中的drawRect
  • Xcode 9.0.1 应用程序分发程序已更改

    最近我将 Xcode 升级到了 Xcode 9 1 之后 当我尝试发布我的应用程序时 Xcode 不允许选择我为临时创建的分发配置文件 这么说 配置文件不是 IOS 应用商店配置文件 如果 Xcode 9 1 的应用程序分发过程有变化 请告
  • ObjC <-> Swift 桥接和工厂方法

    我想从 Swift 使用一些 Objective C 类 由于超出了这个问题范围的原因 我的班级init方法被标记为不可用 相反 有一个工厂方法 这是我的班级的简化形式 interface MyClass NSObject instance
  • 将第 3 方库添加到 iPhone 应用程序时如何设置“标题搜索路径”的路径

    我想添加第 3 方库语音转文本 https github com todoroo iPhone Speech To Textto my Xcode项目 我只是拖xcodeproj文件在我的 iPhone 项目中 然后我按照目标依赖项 将二进
  • 从 AF.Request 响应获取数据

    我需要使用 Alamofire 的 Post 请求调用的 json 响应中的数据 但由于某种原因我无法访问该数据 我尝试按照 Alamofire github 文档以及这篇文章进行操作从 AF 响应 JSON 获取数据 https stac
  • 实时获取 Apple Watch heartRateVariabilitySDNN 吗?

    我正在使用下面的函数来获取 heartRateVariabilitySDNN 但它只获取一次并且不能像 heartbeat 那样实时计算 func HRVstart guard let quantityType HKObjectType q
  • swift 3.0 中的 Sha 256 加密语法错误

    func SHA256 gt String let data self data using String Encoding utf8 let res NSMutableData length Int CC SHA256 DIGEST LE
  • 后台模式下的 AVSpeechSynthesizer

    我无法获取 iOS 7AVSpeechSynthesizer当我的 iOS 应用程序处于后台模式时工作 我添加了 应用程序播放音频 应用程序支持的后台模式的关键 但我仍然无法让它工作 我还研究了创建一个AVMutableCompositio

随机推荐