2017-06-26 10 views
0

こんにちは私の座標抽出コードは以下のとおりです。私は逆ジオロケーションの助けを求めていた。座標からユーザーの都市を抽出する方法

import UIKit 
import CoreLocation 

class ViewController: UIViewController, CLLocationManagerDelegate { 

@IBOutlet weak var citybtn: UIButton! 
@IBOutlet weak var citylbl: UILabel! 

let locationManager = CLLocationManager() 
var currentLocation : CLLocation! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

    // Used to start getting the users location 
    //let locationManager = CLLocationManager() 


    // For use when the app is open 
    //locationManager.requestAlwaysAuthorization() 

    // If location services is enabled get the users location 
    //if CLLocationManager.locationServicesEnabled() { 
    locationManager.delegate = self 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest // You can change the locaiton accuary here. 
    locationManager.requestWhenInUseAuthorization() 



} 

override func viewDidAppear(_ animated: Bool) { 
    super.viewDidAppear(true) 

    locationAuthStatus() 
} 

func locationAuthStatus() { 

    if CLLocationManager.authorizationStatus() == .authorizedWhenInUse { 

     currentLocation = locationManager.location 
     print(currentLocation.coordinate.latitude) 
     print(currentLocation.coordinate.longitude) 

    } else { 
     locationManager.requestWhenInUseAuthorization() 
     locationAuthStatus() 
    } 
} 



@IBAction func buttonpresses(_ sender: Any) { 

} 



} 
+0

あなたはあなたの正確な問題/質問を説明していただけますか? – derHugo

+0

場所の気象を取得する場合や、既存の関数がある場合のように、APIを使用していますか? –

答えて

0
let geoCoder = CLGeocoder() 
    geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in 

       // Place details 
       var placeMark: CLPlacemark! 
       placeMark = placemarks?[0] 

       // Address dictionary 
        print(placeMark.addressDictionary) 

          // Location name 
          if let locationName = placeMark.addressDictionary!["Name"] as? NSString { 
           print(locationName) 
          } 

          // Street address 
          if let street = placeMark.addressDictionary!["Thoroughfare"] as? NSString { 
           print(street) 
          } 

       if placeMark != nil{ 
        // City 
        if let city = placeMark.addressDictionary!["City"] as? NSString { 
         // print(city) 
         self.locCity = city as String 
        } 

           // Zip code 
           if let zip = placeMark.addressDictionary!["ZIP"] as? NSString { 
            print(zip) 
           } 

        // Country 
        if let country = placeMark.addressDictionary!["Country"] as? NSString { 
         print(country) 

        } 



       } 

      }) 
0

Try This worked for me

you will get everything here 

:)事前に感謝します "聞かせ午後=目印[0]!CLPlacemarkとして"

関連する問題