2016-07-08 4 views
0

맵보기 및 테이블보기를 사용하여 뷰 믹스를 작성합니다. 사용자 위치와의 거리를 기준으로 셀을 정렬 할 수 있으면 distanceFromLoaction 메서드를 사용하려고 시도하지만 CLLocationCoordinate에 멤버가 없음을 보여줍니다. distanceFromLocaiton, 그것을 할 또 다른 방법이 있습니까?tableview by distance swift

이 내 배열 구조

import UIKit 
import MapKit 

class tripSpot: NSObject, MKAnnotation{ 
var title: String? 
var coordinate: CLLocationCoordinate2D 
var regionRadius: Double 
var location: String? 
var type: String? 


init(title:String , coordinate: CLLocationCoordinate2D , regionRadius: Double, location: String, type: String){ 
    self.title = title 
    self.coordinate = coordinate 
    self.regionRadius = regionRadius 
    self.location = location 
    self.type = type 
    } 

    } 

을 내 tableviewcode 어떤 조언을

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("MapCell") as! mapTableViewCell 

    self.tripspot.sort({ $0.coordinate.distanceFromLoaction(self.coordinate) < $1.coordinate.distanceInKilometersTo(self.coordinate)}) 
    if searchController.active{ 
    cell.title.text = searchResults[indexPath.row].title 
    cell.location.text = searchResults[indexPath.row].location 
    return cell 
    }else{ 
    cell.title.text = tripspot[indexPath.row].title 
    cell.location.text = tripspot[indexPath.row].location 
    return cell 
    } 

덕분이다.

+1

distanceFromLocation '는()의''CLLocation' 방법 아닌'CLLocationCoordinate2D'이다. 그래서 당신은'CLLocationCoordinate2D' 객체를'CLLocation' (아주 쉽게)로 변환 한 다음'distanceFromLocation()'을 호출해야합니다. – Larme

+0

안녕하세요 @Larme CLLocationCoordinate2d를 신속하게 CLLocation으로 전환하는 법을 가르쳐 주실 수 있습니까? 나는 stackoverflow 온통 검색했지만 여전히 그것을 얻을 수 있습니다. 너. –

+0

'var clLocationObject = CLLocation (위도 : cllocationCoordinate2D.latitude, 경도 : cllocationCoordinate2D.longitude) '또는 이와 유사한 항목입니다. – Larme

답변

6
import UIKit 
import CoreLocation 

final class Places { 
var title: String? 
var cllocation: CLLocation 
var regionRadius: Double 
var location: String? 
var type: String? 
var distance : Double? 
var coordinate : CLLocationCoordinate2D 

init(title:String , cllocation: CLLocation , regionRadius: Double, location: String, type: String ,distance:Double!,coordinate: CLLocationCoordinate2D){ 
    self.title = title 
    self.cllocation = cllocation 
    self.coordinate = coordinate 
    self.regionRadius = regionRadius 
    self.location = location 
    self.type = type 
    self.distance = distance 
} 


// Function to calculate the distance from given location. 
func calculateDistance(fromLocation: CLLocation?) { 

    distance = cllocation.distanceFromLocation(fromLocation!) 
} 
} 


let fromLocation:CLLocation = CLLocation(latitude: 24.186965, longitude: 120.633268) 

var places:[Places] = [ 


Places(title: "Title1", cllocation: CLLocation(latitude :24.181143, longitude: 120.593158), regionRadius: 300.0, location: "LocationTitle1", type: "Food",distance : CLLocation(latitude :24.181143, longitude: 120.593158).distanceFromLocation(fromLocation),coordinate : CLLocationCoordinate2DMake(24.181143,120.593158)), 
Places(title: "Title2", cllocation: CLLocation(latitude:24.14289,longitude:120.679901), regionRadius:150.0, location:"LocationTitle2",type: "Food",distance : CLLocation(latitude:24.14289,longitude:120.679901).distanceFromLocation(fromLocation),coordinate : CLLocationCoordinate2DMake(24.14289,120.679901)), 
Places(title: "Title3", cllocation: CLLocation(latitude : 24.180407, longitude:120.645086), regionRadius: 300.0, location:"LocationTitle3", type: "Food",distance : CLLocation(latitude : 24.180407, longitude:120.645086).distanceFromLocation(fromLocation),coordinate : CLLocationCoordinate2DMake(24.180407,120.645086)), 
Places(title: "Title4", cllocation: CLLocation(latitude: 24.149062,longitude:120.684891), regionRadius: 300.0, location: "LocationTitle4", type: "Food",distance : CLLocation(latitude: 24.149062,longitude:120.684891).distanceFromLocation(fromLocation),coordinate : CLLocationCoordinate2DMake(24.149062,120.684891)), 
Places(title: "Title5", cllocation: CLLocation(latitude:24.138598,longitude:120.672096), regionRadius:150.0, location:"LocationTitle5",type: "Food",distance : CLLocation(latitude:24.138598,longitude:120.672096).distanceFromLocation(fromLocation),coordinate : CLLocationCoordinate2DMake(24.138598,120.672096)), 
Places(title: "Title6", cllocation: CLLocation(latitude :24.1333434,longitude:120.680744), regionRadius:100.0, location:"LocationtTitle6",type: "Culture",distance : CLLocation(latitude :24.1333434,longitude:120.680744).distanceFromLocation(fromLocation),coordinate : CLLocationCoordinate2DMake(24.1333434,120.680744)) 

] 

for k in 0...(places.count-1) { 
     print("\(places[k].distance)") 
} 
for place in places { 
     place.calculateDistance(fromLocation) // Replace YOUR_LOCATION   with the location you want to calculate the distance to. 
} 


    places.sortInPlace({ $0.distance < $1.distance }) 

    for n in 0...(places.count-1) { 
    print("\(places[n].distance)") 
    } 

출력 :

//before sort array 
Optional(4126.1395817058) 
Optional(6803.61030342841) 
Optional(1403.39181021788) 
Optional(6718.92222011204) 
Optional(6653.47447563344) 
Optional(7651.92757760459) 


//after sort array 
Optional(1403.39181021788) 
Optional(4126.1395817058) 
Optional(6653.47447563344) 
Optional(6718.92222011204) 
Optional(6803.61030342841) 
Optional(7651.92757760459) 
+0

귀하의 방법을 사용하지만 여전히 준수 할 수 없으므로 더 구체적으로 기재 할 수 있습니까? 고맙습니다. –

+0

거리를 비교할 수 없습니까? –

+0

두 개에서 거리를 얻는 방법을 모릅니다. CLLocationCooridinate2D –

관련 문제