Ios MapKit中沿路线的注释

Ios MapKit中沿路线的注释,ios,iphone,ios7,mkmapview,mapkit,Ios,Iphone,Ios7,Mkmapview,Mapkit,我正在使用MapKit显示位置之间的方向,我正在寻找一种方法来添加一个注释,其工作原理类似于Apple Maps应用程序中的路线注释,其中注释显示每条路线的旅行时间(如下图所示)。我已经正确地画出了方向,现在的问题是如何计算路线上的一对坐标。即,放置注释的位置 我想知道如何使用 MKOrths(它包含完整的方向,一步一步),但我不确定如何生成一对在路线中间的坐标。 我在MapKit文档中找不到对此的任何支持。有什么想法吗 这就是我生成路线并显示它的方式 - (void)generateRout

我正在使用MapKit显示位置之间的方向,我正在寻找一种方法来添加一个注释,其工作原理类似于Apple Maps应用程序中的路线注释,其中注释显示每条路线的旅行时间(如下图所示)。我已经正确地画出了方向,现在的问题是如何计算路线上的一对坐标。即,放置注释的位置

我想知道如何使用<代码> MKOrths(它包含完整的方向,一步一步),但我不确定如何生成一对在路线中间的坐标。

我在MapKit文档中找不到对此的任何支持。有什么想法吗

这就是我生成路线并显示它的方式

- (void)generateRoute {
    MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
    request.source = [MKMapItem mapItemForCurrentLocation];
    request.destination = self.destinationMapItem;
    MKDirections *directions = [[MKDirections alloc] initWithRequest:request];

    [directions calculateDirectionsWithCompletionHandler:
     ^(MKDirectionsResponse *response, NSError *error) {
         if (error) {
             // Handle Error
         } else {
             [self showRoute:response];
         }
     }];
}

- (void)showRoute:(MKDirectionsResponse *)response {
    [self.mapView removeOverlays:self.mapView.overlays];
    for (MKRoute *route in response.routes)
    {
        [self.mapView addOverlay:route.polyline level:MKOverlayLevelAboveRoads];
    }
    [self fitRegionToRoute];
}

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id < MKOverlay >)overlay
{
    MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
    renderer.strokeColor = [UIColor blueColor];
    renderer.alpha = 0.7;
    renderer.lineWidth = 4.0;

    return renderer;
}
-(void)GeneratorOute{
MKDirectionsRequest*请求=[[MKDirectionsRequest alloc]init];
request.source=[MKMapItem-mapItemForCurrentLocation];
request.destination=self.destinationMapItem;
MKDirections*directions=[[MKDirections alloc]initWithRequest:request];
[方向使用CompletionHandler计算方向:
^(MKDirectionsResponse*响应,N错误*错误){
如果(错误){
//处理错误
}否则{
[自我展示路线:回应];
}
}];
}
-(无效)showRoute:(MKDirectionsResponse*)响应{
[self.mapView removeOverlays:self.mapView.overlays];
for(MKRoute*响应中的route.routes)
{
[self.mapView addOverlay:route.polyline级别:mkoverlayleveloverroads];
}
[自助路线];
}
-(MKOverlayRenderer*)地图视图:(MKMapView*)地图视图渲染器ForOverlay:(id)overlay
{
MKPolylineRenderer*渲染器=[[MKPolylineRenderer alloc]initWithOverlay:overlay];
renderer.strokeColor=[UIColor blueColor];
α=0.7;
渲染器。线宽=4.0;
返回渲染器;
}

提问者的编辑:

在这个答案的帮助下,它终于成功了。我在下面的代码中添加了这一点,上面写着“施展魔法”:

原始答案:

我不知道这是否有效。关于你的问题,我只是有我的想法

我想你会创建如下的路线 (查看我的在线评论)

添加注释

-(void) createAndAddAnnotationForCoordinate : (CLLocationCoordinate2D) coordinate{
    MyAnnotation* annotation= [[MyAnnotation alloc] init];
    annotation.coordinate = coordinate;

    annotation.title = @"Any Title";
    annotation.subtitle = @"Any Subtitle";

   [yourMap addAnnotation: annotation];

}

我建议你看看,与路线相结合。

也许不是最有效的方法,但可能还是很快的,那就是计算起点和终点的中点(即平均纬度和长径)。然后,遍历多段线点,检查从每个点到该中点的距离。以最接近的比赛为例


即使这条线弯曲得很厉害,中点也会直接在两端之间。宽曲线上的某个点最接近该外部中点,可能是放置注释的好位置

一旦有了
MKRoute
,可以使用以下方法找到近似的中心点:

route.polyline.coordinate
这将返回一个
CLLocationCoordinate2D
,您可以使用它来居中注释


感谢这是一个老问题,但我已经搜索了一段时间类似的东西,最终手动计算中心。事实证明,自从iOS 8以来,它非常简单。

如果您想知道swift的中间部分,可以使用以下代码:

MKCoordinateForMapPoint(route.polyline.points()[route.polyline.pointCount/2])

使用示例:

directions.calculateDirectionsWithCompletionHandler ({
     (response: MKDirectionsResponse?, error: NSError?) in

     if error == nil {
        self.showRoute(response!)
     }
     else{
        print("some error")    
     }
})

func showRoute(response:MKDirectionsResponse){
    for route in response.routes {
        self.map.addOverlay(route.polyline, level: MKOverlayLevel.AboveRoads)
        self.map.setCenterCoordinate(route.polyline.coordinate, animated: true)
        self.map.setRegion(MKCoordinateRegionMakeWithDistance(route.polyline.coordinate, route.distance*0.75, route.distance*0.75), animated: true)

        let routeAnnotation = MKPointAnnotation()
        routeAnnotation.coordinate = MKCoordinateForMapPoint(route.polyline.points()[route.polyline.pointCount/2])
        map.addAnnotation(routeAnnotation)
    }
}

您是否希望绘制路线,以及希望在RouTeI中间删除注释,也希望绘制路线(我已经在做)。我不明白为什么您需要在叠加中添加注释。你知道叠加也是注释吗?在底部。嗯,有两个独立的叠加,其中一个是注释。首先,是蓝色路线叠加。然后还有指向路线的蓝色和白色注释。你会建议在地图上的什么地方添加它们?我喜欢你的想法,但这只有在路线是直线的情况下才好看。考虑一个循环的路线,注释不会在圆圈中间而不是在路线上结束吗?如果我们正在考虑一个MKCURLY,你的问题是正确的,但是当我们考虑MkPuliin时,我猜MkPulLin的坐标应该在我猜想的范围内。我的建议值得一试。我不确定输出。我会尝试一下,知道后再给你回复!令人惊叹的。等待结果另外,只有当多段线中的线段长度大致相等时,使用多段线中点阵列中点的坐标的解决方案才是合理的。如果你有一条有3个点(2条线段)的多段线,并且第一条线段比第二条线段短得多,那么使用数组中点的坐标将不会在“多段线的中间”放置注释。谢谢,看起来是一个很好的框架。尽管对于这个任务来说有点太多了,但我自己已经实现了许多这些特性。
route.polyline.coordinate
directions.calculateDirectionsWithCompletionHandler ({
     (response: MKDirectionsResponse?, error: NSError?) in

     if error == nil {
        self.showRoute(response!)
     }
     else{
        print("some error")    
     }
})

func showRoute(response:MKDirectionsResponse){
    for route in response.routes {
        self.map.addOverlay(route.polyline, level: MKOverlayLevel.AboveRoads)
        self.map.setCenterCoordinate(route.polyline.coordinate, animated: true)
        self.map.setRegion(MKCoordinateRegionMakeWithDistance(route.polyline.coordinate, route.distance*0.75, route.distance*0.75), animated: true)

        let routeAnnotation = MKPointAnnotation()
        routeAnnotation.coordinate = MKCoordinateForMapPoint(route.polyline.points()[route.polyline.pointCount/2])
        map.addAnnotation(routeAnnotation)
    }
}