Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
141 views
in Technique[技术] by (71.8m points)

ios - distanceInMeters Problems with arrays and sort

I want to sort my TableView by distance, but I have mixed up:( Please help me if You can, I would appreciate it, really :(

I have an array with 100 objects.

var malls: [Mall] = [

    Mall(name: "Европейский",  type: "торговый центр", image: "europe.jpg", time: "с 10.00 до 22.00",
         timeWeek: "с 10.00 до 23.00", city: "Москва", location: "Киевская",
         adress: "г. Москва, Киевского вокзала площадь, д.2", cinemaName:"formulakino.png",
         productName: "perekrestok.png", numShop: "309", website: "http://europe-tc.ru", schemeWeb:"http://www.europe-tc.ru/shops/", longitude:37.566, latitude:55.745),

    Mall(name: "Золотой Вавилон Ростокино",  type: "торговый центр", image: "vavilon.jpg", time: "с 10.00 до 22.00",
         timeWeek: "с 10.00 до 22.00", city: "Москва", location: "Проспект Мира",
         adress: "г. Москва, Проспект Мира, д. 211", cinemaName: "Люксор",
         productName: "okay.png", numShop: "280", website: "http://zolotoy-vavilon.ru/rostokino", schemeWeb:"http://www.zolotoy-vavilon.ru/rostokino/map", longitude:37.663, latitude:55.846), ]

and so on.. sorry for Cyrillic

and after override func tableView I have this

func locationManager(_  manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{


    let currentLocation = locations[0]


    if (currentLocation.horizontalAccuracy > 0 ) {
        locationManager.stopUpdatingLocation()
        let coords = CLLocation(latitude: currentLocation.coordinate.latitude, longitude: currentLocation.coordinate.longitude)
        **let mallLocate = CLLocation(latitude: mall.latitude, longitude: mall.longitude)**
        let distanceInMeters = mallLocate.distance(from: coords)
        print (distanceInMeters)
    }

}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! EateriesTableViewCell
    let mall = mallToDisplayAt(indexPath: indexPath)
    cell.thumbnailImageView.image = UIImage(named: mall.image)
    cell.thumbnailImageView.clipsToBounds = true
    cell.nameLabel.text = mall.name
    cell.locationLabel.text = mall.location
    cell.typeLabel.text = mall.time


    return cell

}

I have problem in this row

let mallLocate = CLLocation(latitude: mall.latitude, longitude: mall.longitude)

Use of unresolved identifier 'mall'

How can I fix it? and how to do sort by directions? Thank you so much.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to choose the index of your array:

let mallLocate = CLLocation(latitude: malls[0].latitude, longitude: malls[0].longitude)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...