2016-05-01 4 views

答えて

1

S.、

私はあなたがここに求めているのかわからないんだけど、私はあなたがポリライン上のどこかのアノテーションを表示したいと仮定します。

はじめにポリラインを取得する方法: したがって、地図上にポリラインを描画するCLLocationオブジェクトの配列があるとします。このロケーションオブジェクトの配列をmyLocationsと呼び、それは[CLLocation]タイプのものです。アプリケーションのどこかでポリラインを作成するメソッドを呼び出すと、このメソッドはcreateOverlayObject(場所:[CLLocation]) - > MKPolylineと呼ばれます。

あなたの呼び出しは次のようになります。

let overlayPolyline = createOverlayObject(myLocations) 

をあなたがして呼び出されたメソッドは次のようになります。

func createOverlayObject(locations: [CLLocation]) -> MKPolyline { 
    //This method creates the polyline overlay that you want to draw. 
    var mapCoordinates = [CLLocationCoordinate2D]() 

    for overlayLocation in locations { 

     mapCoordinates.append(overlayLocation.coordinate) 
    } 

    let polyline = MKPolyline(coordinates: &mapCoordinates[0], count: mapCoordinates.count) 

    return polyline 
} 

これは最初の部分だった、(のMapViewを実装することを忘れないでください_:rendererForOverlay overlay :)レンダリングされた行を取得します。この部分は次のようになります。

func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer { 
    //This function creatss the renderer for the polyline overlay. This makes the polyline actually display on screen. 
    let renderer = MKPolylineRenderer(overlay: overlay) 
    renderer.strokeColor = mapLineColor //The color you want your polyline to be. 
    renderer.lineWidth = self.lineWidth 

    return renderer 
} 

ここで、2番目の部分はマップのどこかでアノテーションを取得します。注釈を配置したい座標が何であるかわかっている場合は、実際にはまっすぐです。注釈を作成し、表示することは、再び簡単です、あなたはmyNiceMapViewと呼ばれるマップビューに定義されていると仮定すると: - > MKAnnotationView MKMapView、viewForAnnotation注釈:):

func createAnnotation(myCoordinate: CLLocationCoordinate2D) { 
    let myAnnotation = MKPointAnnotation() 
    myAnnotation.title = "My nice title" 
    startAnnotation.coordinate = myCoordinate 

    self.myNiceMapView.addAnnotations([myAnnotation]) 
} 

ことのMapView(_を実装することを忘れないでください?方法は次のようになります。

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 
    //This is the mapview delegate method that adjusts the annotation views. 

    if annotation.isKindOfClass(MKUserLocation) { 

     //We don't do anything with the user location, so ignore an annotation that has to do with the user location. 
     return nil 
    } 

    let identifier = "customPin" 
    let trackAnnotation = MKAnnotationView.init(annotation: annotation, reuseIdentifier: identifier) 

    trackAnnotation.canShowCallout = true 

    if annotation.title! == "Some specific title" { //Display a different image 
     trackAnnotation.image = UIImage(named: "StartAnnotation") 
     let offsetHeight = (trackAnnotation.image?.size.height)!/2.0 
     trackAnnotation.centerOffset = CGPointMake(0, -offsetHeight) 
    } else { //Display a standard image. 
     trackAnnotation.image = UIImage(named: "StopAnnotation") 
     let offsetHeight = (trackAnnotation.image?.size.height)!/2.0 
     trackAnnotation.centerOffset = CGPointMake(0, -offsetHeight) 
    } 

    return trackAnnotation 
} 

ここで課題は、アノテーションを配置する場所を正しく見つけることです。注釈を配置する場所を参照するCLLocationCoordinate2Dを持つよりも優れたものは何も見つかりません。その後のためのインループあなたは注釈を入れたい場所を見つけ、このようなもので:myLocations内の場所のための

{

if (location.latitude == myReferenceCoordinate.latitude) && (location.longitude == myReferenceCoordinate.longitude) { 
    self.createAnnotation(location: CLLOcationCoordinate2D) 
} 

}

これは、あなたの質問に答える願っています。

関連する問題