iOS MapKit 通过道路连接地图上的多个点

     2023-05-06     295

关键词:

【中文标题】iOS MapKit 通过道路连接地图上的多个点【英文标题】:iOS MapKit connect multiple points on map over roads 【发布时间】:2014-06-25 03:34:15 【问题描述】:

我正在使用此代码将引脚(地图上的点)与折线连接:

CLLocationCoordinate2D coordinates[allLocations.count];
    int i = 0;
    for (locHolder *location in allLocations) 
        coordinates[i] = CLLocationCoordinate2DMake([location.lat floatValue], [location floatValue]);
        i++;
    
    MKPolyline *polyline = [MKPolyline polylineWithCoordinates:coordinates count:[allLocations count]];
    self->polyline = polyline;
    [self.mapView addOverlay:self->polyline level:MKOverlayLevelAboveRoads];

但是这段代码通过无线方式连接它们(忽略道路),是否可以将多个 CLLocationCoordinate2D 位置与跟随道路的折线连接?

** 还有一个简短的问题, [allLocations count] 和 allLocations.count 之间的性能是否存在差异。

谢谢。

【问题讨论】:

使用 MKDirectionsRequest 获取位置之间的行车路线。有关示例,请参阅***.com/questions/18805613/…。此外,[allLocations count]allLocations.count 完全相同(参见***.com/questions/1249392/…)。 我已经查看了 MKDirectionsRequest,但它允许连接点 A 和 B,但我需要的是通过 B、C、D、E 连接点 A 和 X...达到了吗? A 到 B 调用它,然后 B 到 C 调用它,然后...... 【参考方案1】:

这几天我遇到了同样的问题,到处寻找答案,我发现thisRob的答案对这个案例非常有用。

首先,假设您的 MapViewController 中有一个包含起点和终点的对象数组,每个对象的类型均为 CLLocationCoordinate2D

在你的 MapViewController.h

@property (nonatomic, strong) NSArray *locations;

然后,在实现中,使用如下所示的一些数据填充该数组:

_locations = [NSArray arrayWithObjects:
    [RouteObject routeWithOrigin:CLLocationCoordinate2DMake(-23.595571, -46.684408) destination:CLLocationCoordinate2DMake(-23.597886, -46.673950)],
    [RouteObject routeWithOrigin:CLLocationCoordinate2DMake(-23.597886, -46.673950) destination:CLLocationCoordinate2DMake(-23.597591, -46.666805)],
    [RouteObject routeWithOrigin:CLLocationCoordinate2DMake(-23.597591, -46.666805) destination:CLLocationCoordinate2DMake(-23.604061, -46.662728)], nil];

现在您已经有了起点和终点的数组,您可以开始在它们之间绘制一条路线(从 A 到 B、从 B 到 C 以及从 C 到 D)。

添加一个按钮,然后连接一个IBAction,这样您就可以使用这种方法来发挥作用了。

- (IBAction)btnDirectionsPressed:(id)sender 
    [self enableUI:NO];    // This method enables or disables all the UI elements that interact with the user

    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);     // Create the semaphore    

    __block NSMutableArray *routes = [[NSMutableArray alloc] init];         // Arrays to store MKRoute objects and MKAnnotationPoint objects, so then we can draw them on the map
    __block NSMutableArray *annotations = [[NSMutableArray alloc] init];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^
        for (RouteObject *routeObject in _locations) 
            MKDirectionsRequest *directionsRequest = [MKDirectionsRequest new];
            [directionsRequest setTransportType:MKDirectionsTransportTypeAutomobile];
            [directionsRequest setSource:[routeObject originMapItem]];
            [directionsRequest setDestination:[routeObject destinationMapItem]];
            [directionsRequest setRequestsAlternateRoutes:NO];

            MKDirections *direction = [[MKDirections alloc] initWithRequest:directionsRequest];

            // For each object in the locations array, we request that route from its origin and its destination
            [direction calculateDirectionsWithCompletionHandler: ^(MKDirectionsResponse *response, NSError *error)    
                if (error) 
                    NSLog(@"There was an error getting your directions");
                    return;
                

                MKRoute *route = [response.routes firstObject];   
                [routes addObject:route];
                [annotations addObject:[routeObject destinationAnnotation]];

                dispatch_semaphore_signal(semaphore);    // Send the signal that one semaphore is ready to consume
            ];
        
    );

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^
        for (int i = 0; i < _locations.count; i++) 
            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);    // Wait until one semaphore is ready to consume

            dispatch_async(dispatch_get_main_queue(), ^    // For each element, dispatch to the main queue to draw route and annotation corresponding to that location
                MKRoute *route = routes[i];
                [self.mapView addOverlay:route.polyline];
                [self.mapView addAnnotation:annotations[i]];
            );
        

        dispatch_async(dispatch_get_main_queue(), ^   // Finally, dispatch to the main queue enabling elements and resizing map to show all the annotations
            [self enableUI:YES];
            [self fitRegionToRoutes];
        );
    );

希望对您有所帮助!

乔尔。

【讨论】:

嗨,经过多年的差距,我正在开发 iOS。尝试运行此代码并给出错误为“使用未声明的标识 RouteObject”。我已经包含了 Mapkit 和 CoreLocation 框架并导入到文件中。请建议。我想在起点和终点以外的多个点之间创建一个查找距离。 @Pawriwes RouteObject 是我为此目的制作的一些 POJO。它有两个属性,都是 CLLocationCoordinate2D 类型,还有一些有用的方法。

MKAnnotation 点击​​上的 Swift/iOS MapKit 功能

】MKAnnotation点击​​上的Swift/iOSMapKit功能【英文标题】:Swift/iOSMapKitfunctiononMKAnnotationtap【发布时间】:2015-07-0314:03:45【问题描述】:我有一张地图,我已经完美地添加了12个注释,我想要的是,当有人点击这些注释时,它会在注... 查看详情

快速拖动地图或更改缩放级别时,MapKit 上的自定义集群注释崩溃

】快速拖动地图或更改缩放级别时,MapKit上的自定义集群注释崩溃【英文标题】:CustomclusteredannotationsonMapKitcrashwhenrapidlydraggingthemaporchangingthezoom-level【发布时间】:2020-04-1215:47:28【问题描述】:在实现了对自定义注释进行集群的... 查看详情

ios:mapkit如何在地图上启用当前位置按钮?

】ios:mapkit如何在地图上启用当前位置按钮?【英文标题】:ios:mapkithowtoenablecurentlocationbuttononmap?【发布时间】:2014-02-1013:43:28【问题描述】:我正在开发显示地图的应用程序。我在地图属性中启用了用户当前位置,它现在显示... 查看详情

使用 MapKit 在 IOS 地图上放置图钉? [关闭]

】使用MapKit在IOS地图上放置图钉?[关闭]【英文标题】:DroppingpinsonanIOSmapwithMapKit?[closed]【发布时间】:2018-06-0618:24:29【问题描述】:我正在尝试通过创建示例项目来学习使用MapKit;我想创建一个地图,其中包含特定连锁餐厅的... 查看详情

1774:大逃杀(代码片段)

...内存限制:262144KB   提交数:49    通过数:19   【题目描述】      将地图上的所有地点标号为1到n,地图中有n-1      条双向道路连接这些点,通过一条... 查看详情

MapKit:地图上的位置是不是具有唯一 ID?

】MapKit:地图上的位置是不是具有唯一ID?【英文标题】:MapKit:Dolocationsonamaphaveuniqueid\'s?MapKit:地图上的位置是否具有唯一ID?【发布时间】:2015-11-1520:15:15【问题描述】:我正在尝试在您搜索某物的地方制作一些东西,它会在... 查看详情

iPhone 3.0 MapKit——提供点之间的路由

】iPhone3.0MapKit——提供点之间的路由【英文标题】:iPhone3.0MapKit--providingroutingbetweenpoints【发布时间】:2009-08-1716:36:44【问题描述】:是否可以让MapKit使用GoogleMapsAPI类型的路由连接来连接定义的点。【问题讨论】:【参考方案1】... 查看详情

iOS 6 上的 MapKit 崩溃

】iOS6上的MapKit崩溃【英文标题】:CrashwithMapKitoniOS6【发布时间】:2012-10-3120:36:19【问题描述】:我自己无法重现此特定崩溃,但已通过我们的崩溃报告工具进行了报告,并且数百名用户正在体验它。发现很难调试或重现。它发... 查看详情

地理标记/绘图 MapKit 核心定位

】地理标记/绘图MapKit核心定位【英文标题】:Geotagging/PlottingMapKitCorelocation【发布时间】:2012-08-1617:48:45【问题描述】:我创建了一个应用程序,它使用MapKit在谷歌地图上简单地显示用户的当前位置。但是我现在想更进一步,通... 查看详情

Swift mapkit未指向位置

】Swiftmapkit未指向位置【英文标题】:Swiftmapkitnotpointingatlocation【发布时间】:2018-03-0510:03:04【问题描述】:我有一个使用mapkit和corelocation构建的ios应用程序。该应用程序可以正常工作,但地图没有在xcode模拟器中确定我当前的位... 查看详情

在 iOS MapKit 中,如何在地图旋转时为注释设置动画?

】在iOSMapKit中,如何在地图旋转时为注释设置动画?【英文标题】:IniOSMapKit,howdoyouanimateanannotationasthemapisrotating?【发布时间】:2017-11-0822:15:09【问题描述】:所以我可以创建贴在地图上的注释,我可以创建叠加层,比如多边形... 查看详情

百度地图怎么设置

...现一个可供您拖动的途经点,将鼠标拖动至您想要经过的道路并松开,更新的驾车方案将经过您选择的道路。问题三:手机百度地图导航不想走高速怎么设置设置方法如下:1、首先打开百度地图,点击【路线】,在界面输入终... 查看详情

百度地图上的两个图标各代表啥意思?

...,在使用百度地图JavaScriptAPI服务前,需先将非百度坐标通过坐标转换接口转换成百 查看详情

百度地图上★号标注的位置是啥含义

...中,这三个DOM元素分别位于不同的容器中,这些容器可以通过map.getPanes()方法获得,其中markerMouseTarget就是标注点击区域 查看详情

iOS 如何让 MapKit 显示自定义室内地图?

】iOS如何让MapKit显示自定义室内地图?【英文标题】:iOShowcanImakeMapKitdisplaycustomindoormap?【发布时间】:2014-06-0522:20:28【问题描述】:我正在尝试创建我正在工作的办公室的地图。我有一张高分辨率图像,并希望将其自动旋转到... 查看详情

如何在 IOS 中使用 mapkit 在地图上显示用户

】如何在IOS中使用mapkit在地图上显示用户【英文标题】:HowtoshowuseronMapusingmapkitinIOS【发布时间】:2015-12-1604:22:49【问题描述】:。此代码仅在地图上显示印度,而不是定义的坐标。mapView.delegate=self;mapView.showsUserLocation=YES;objLocatio... 查看详情

使用 mapKit 的地图显示相反的方向(iOS Swift)

】使用mapKit的地图显示相反的方向(iOSSwift)【英文标题】:MapsusingmapKitdisplaystheoppositedirections(iOSSwift)【发布时间】:2015-07-0906:00:43【问题描述】:我正在尝试使用Apple地图显示从用户当前位置到之前固定位置的说明。这是我的... 查看详情

将uipopovercontroller的箭头放置在mapkit的注释点

】将uipopovercontroller的箭头放置在mapkit的注释点【英文标题】:Placingarrowofuipopovercontrolleratannotationpointonmapkit【发布时间】:2010-05-0406:36:34【问题描述】:我试图让弹出框出现在地图工具包注释点,但在注释视图属性中找不到“矩... 查看详情