2016-12-09 7 views
1

私の現在の場所から別の場所への私の総距離を表示したいと私はAnnotationのサブタイトルの距離のショーをしたい。 は、ここに私のコードですが、それは動作しません:現在の場所から別の場所への注釈の距離を表示

func shopDeal() { 
    var inKm = "" 
    let locationsq = 
     [["title": "Shop Deal","subtitle": "\(inKm) km", "latitude": 31.352792, "longitude": 74.253201], 
     ["title": "Shop Deal", "subtitle": "\(inKm) km", "latitude": 31.403563, "longitude": 74.258200]] 

    // Span 
    for location in locationsq { 
     let annotation = MKPointAnnotation() 
     annotation.title = location["title"] as? String 
     annotation.subtitle = location["subtitle"] as? String 
     annotation.coordinate = CLLocationCoordinate2D(latitude: location["latitude"] as! Double, longitude: location["longitude"] as! Double) 
     mapView.addAnnotation(annotation) 
     func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 

      let myCurrentLoc = locations[locations.count - 1] 
      let coor1 = CLLocation(latitude: annotation.coordinate.latitude, longitude: annotation.coordinate.longitude) 
      let coor2 = CLLocation (latitude: myCurrentLoc.coordinate.latitude, longitude: myCurrentLoc.coordinate.longitude) 

      let distanceInMeters:CLLocationDistance = coor1.distanceFromLocation(coor2) 

      inKm = String(format: "%.2f", distanceInMeters/1000) 

      print("\(inKm) km") 
     } 
    } 
    mapView.delegate = self 
} 

すべてのヘルプは高く評価されています。事前のおかげで地図上の2点(LAT and LONG)との間に

+0

距離を2か所(つまり、LATとLONG)に計算しますか? –

+0

はい....しかし、基本的に私は距離が注釈の副題に表示されたい... – Kamboh

答えて

1

検索距離:

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 

    if annotation is MKUserLocation { 
      return nil 
    } 

    let reuseId = "pinIdentifier" 

    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView 
    if pinView == nil { 
     pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId) 
     pinView!.canShowCallout = true 
    } 
    else { 
     pinView!.annotation = annotation 
    } 

    //Adding Subtitle text 
    let subtitleView = UILabel() 
    //Decalare distanceInMeters as global variables so that you can show distance on subtitles 
    subtitleView.text = "\(distanceInMeters)" 
    pinView!.detailCalloutAccessoryView = subtitleView 

    return pinView 
} 

場合はコメントをお気軽に:今、あなたはその後、注釈の字幕で距離を表示したい場合は

let coordinate1 = CLLocation(latitude: 5.0, longitude: 5.0) 
let coordinate2 = CLLocation(latitude: 5.0, longitude: 3.0) 
//Decalare distanceInMeters as global variables so that you can show distance on subtitles 
let distanceInMeters = coordinate1.distance(from: coordinate2) 

さらに問題が発生する

+0

あなたの助けをありがとう...それは私のために非常にうまくいく...もう一つの事...... – Kamboh

+0

@Kambohこの答えがあなたの問題を解決するならば、それを正しくマークし、他の人もそれを助けることができるようにupvoteする必要があります。あなたの次の問題は何ですか? –

+0

私はそれを正しくマークして投票します(私はstakの通知に従って15の評判より低いので表示できませんでした) – Kamboh

0

試してみてください!

// 1. Get Source & Destination coordinates (Hard-cored here) 
let locSource : CLLocation = CLLocation.init(latitude: 17.428031, longitude: 78.377837) 
let locDestination : CLLocation = CLLocation.init(latitude: 17.441879, longitude: 78.429990) 


// 2. Find distance between the Source & Destination 
let nDistanceInMeters : CLLocationDistance = locSource.distance(from: locDestination) 
let nDistanceInKiloMeters : CLLocationDistance = nDistanceInMeters/1000.0; 

// 3. Convert to String 
let strDistanceInMeters : String = String.init(format: "%f", nDistanceInMeters) 
let strDistanceInKM : String = String.init(format: "%f", nDistanceInKiloMeters) 


// 4. Assign the calculated distance to your label, 
// Probably in mapView(mapView:viewForAnnotation annotation: 
lblDistanceLabelOnAnnotationView.text = strDistanceInKM 

希望します。

+0

それは役に立ちます.. どのように私は座標の配列を使用する...?各座標に応じた距離を表示するには? – Kamboh

関連する問題