2014-01-08 164 views
12

我正在使用MapKit來顯示位置之間的方向,我正在尋找一種方法來添加註釋,該註釋的作用類似於Apple Maps應用程序中的路線註釋,其中註釋顯示每個路線旅行時間(如下圖所示)。我已經正確地繪製了方向,手頭的問題是如何計算沿路線的一對座標。也就是說,在哪裏放棄註釋。沿着MapKit路線的註釋

我想了一下使用MKDirection(它包含完整的方向,一步一步),但我不知道如何生成一對座標在路徑中間的某處。

我一直沒能在MapKit文檔中找到任何支持。有任何想法嗎?

enter image description here

我這是怎麼生成的路線並顯示。

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

你想畫的路線,以及你想要在路線中間放棄註釋 – iPrabu

+0

我也想繪製路線(我已經在做)。 –

+0

我不明白你爲什麼需要向疊加層添加註釋。你知道疊加層也是註釋嗎? – Desdenova

回答

7

發問的編輯:

終於做到了這個答案的工作有所幫助。我說這下面的代碼,它說:這裏做魔術

MKMapPoint middlePoint = route.polyline.points[route.polyline.pointCount/2]; 
[self createAndAddAnnotationForCoordinate:MKCoordinateForMapPoint(middlePoint)]; 

原來的答覆:

我不知道這是否會工作或沒有。只是我的想法在你的問題。

我猜你可能已經被創建的路線如下 (檢查我的在線評論)

MKDirectionsRequest *request = 
     [[MKDirectionsRequest alloc] init]; 
request.source = [MKMapItem mapItemForCurrentLocation]; 
request.destination = _destination; 
request.requestsAlternateRoutes = NO; 
MKDirections *directions = 
     [[MKDirections alloc] initWithRequest:request]; 

    [directions calculateDirectionsWithCompletionHandler: 
^(MKDirectionsResponse *response, NSError *error) { 
    if (error) { 
     // Handle error 
    } else { 
     for (MKRoute *route in response.routes) 
     { 
      [_routeMap addOverlay:route.polyline level:MKOverlayLevelAboveRoads]; 
      //Here do the magic 
      //MKPolyline confronts to MKOverlay so you can get the coordinate like 
      //route.polyline.coordinate once you get the coordinate then you can build 
      //a annotation. A annotation is nothing but a coordinate with some title. 
      //According to MKOverlay coordinate property it justs gives you the 
      //center point of the overlay area 
      [self createAndAddAnnotationForCoordinate:route.polyline.coordinate] 
     } 
    } 
}]; 

添加註釋

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

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

    [yourMap addAnnotation: annotation]; 

} 
+0

我喜歡你的主意,但是當這條路線是直線時,這隻會看起來不錯。考慮一個圍成一圈的路線,註釋不會在圓圈中間而不是在路線上? –

+0

如果我們正在考慮一個MKCircle,你的問題將是正確的,但正如我們正在考慮一個MKPolyine,我猜MKPolyline的座標應該在我猜的線上。我的建議值得一試。我不確定輸出。 – iPrabu

+0

我會試試看,當我知道的時候回覆你! –

-1

我建議你看this,集成與路由。

+1

謝謝你,看起來像一個偉大的框架。雖然這個任務有點太多了,但我已經自己實現了許多這些功能。 –

0

也許不是最有效的方法,但應該可能很快,將計算您的開始和結束點的中點(即平均他們的lats和longs)。然後,遍歷您的多段線點,檢查每個點到該中點的距離。採取最接近的比賽。

即使該線是非常彎曲的,中點將直接位於兩端之間。野外曲線上的某些點最接近該外部中點,可能是放置註釋的好地方。

0

一旦你有一個MKRoute,近似中心點可以使用發現:

route.polyline.coordinate 

這會返回一個CLLocationCoordinate2D,您可以使用居中的註釋。

欣賞這是一個老問題,但我一直在尋找這樣的一段時間,並最終手動計算中心。從iOS 8開始就很簡單。

0

如果你想知道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) 
    } 
}