2017-09-29 14 views
1

私はSwift 4に変換されました。firebaseからデータを取り出し、ユーザーの場所から距離を置いて整理した私のtableviewは動作を停止しました。問題は私のユーザーの場所がゼロになることです。最近、変換のためにmapkitに問題がある人もいると聞いてきましたが、同じカテゴリに該当するかどうかは不明です。したがって、 "let userLocation = CLLocation(latitude:latitude!、longitude:longitude!)"という行はnilを返します。ありがとう。スウィフト4 CLLocationユーザーの所在地経度と緯度

BreakPoint

override func viewDidLoad() { 
    super.viewDidLoad() 

    searchController.searchResultsUpdater = self 
    searchController.dimsBackgroundDuringPresentation = false 
    definesPresentationContext = true 
    tableView.tableHeaderView = searchController.searchBar 

    locationManager.delegate = self 
    locationManager.distanceFilter = kCLLocationAccuracyNearestTenMeters 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest 
    locationManager.startUpdatingLocation() 
    locationManager.stopUpdatingLocation() 
    locationManager.requestAlwaysAuthorization() 

    getTableViewData() 

    refresher = UIRefreshControl() 
    refresher.attributedTitle = NSAttributedString(string: "Pull to refresh") 
    refresher.addTarget(self, action: #selector(RegisteredLocations.handleRefresh(refreshControl:)), for: UIControlEvents.valueChanged) 
    tableView.addSubview(refresher) 
} 

func getTableViewData() { 
    Database.database().reference().child("Businesses").queryOrdered(byChild: "businessName").observe(.childAdded, with: { (snapshot) in 

     let key = snapshot.key 

     if(key == self.loggedInUser?.uid) { 
      print("Same as logged in user, so don't show!") 
     } else { 
      if let locationValue = snapshot.value as? [String: AnyObject] { 
       let lat = Double(locationValue["businessLatitude"] as! String) 
       let long = Double(locationValue["businessLongitude"] as! String) 
       let businessLocation = CLLocation(latitude: lat!, longitude: long!) 

       let latitude = self.locationManager.location?.coordinate.latitude 
       let longitude = self.locationManager.location?.coordinate.longitude 
       let userLocation = CLLocation(latitude: latitude!, longitude: longitude!) 

       let distanceInMeters: Double = userLocation.distance(from: businessLocation) 
       let distanceInMiles: Double = distanceInMeters * 0.00062137 
       let distanceLabelText = String(format: "%.2f miles away", distanceInMiles) 

       var singleChildDictionary = locationValue 
       singleChildDictionary["distanceLabelText"] = distanceLabelText as AnyObject 
       singleChildDictionary["distanceInMiles"] = distanceInMiles as AnyObject 
       self.usersArray.append(singleChildDictionary as NSDictionary) 
       self.usersArray = self.usersArray.sorted { 
        !($0?["distanceInMiles"] as! Double > $1?["distanceInMiles"] as! Double) 
       } 

      } 
      //insert the rows 
      //self.followUsersTableView.insertRows(at: [IndexPath(row:self.usersArray.count-1,section:0)], with: UITableViewRowAnimation.automatic) 
      self.followUsersTableView.reloadData() 
     } 

    }) { (error) in 
     print(error.localizedDescription) 
    } 

} 
+0

これらの行は何を返しますか:let latitude = self.locationManager.location?.coordinate.latitude let longitude = self.locationManager.location?coordinate.longitude – azamsharp

+0

私はちょうどブレークポイントとlong&latのスクリーンショットを追加しました実際に返されたユーザーの場所の –

+0

ありがとう!あなたの緯度と経度の値は非常に奇妙に見えます。アプリが初めて実行されたときに、その場所を使用する許可を求められました。 – azamsharp

答えて

3

あなたはlocationManager.startUpdatingLocation()を呼び出した直後にlocationManager.stopUpdatingLocation()を呼んでいます。

場所を特定する時間が必要なので、これは機能しません。代理人方法didUpdateLocationsへの呼び出しを受け取るまで、場所を使用しないでください。

iOS 9以降をターゲットにしていて、単一の場所だけが必要な場合は、startUpdatingLocation()ではなくrequestLocation()に電話する必要がありますが、引き続き委任方法へのコールバックを待つ必要があります。

ロケーションを受け取ったら、テーブルデータを読み込むことができます。

+0

私はinfo.plistのプライバシー設定の一部も欠落していました。 –

関連する問題