在Swift中查找纬度和经度的城市名称和国家 [英] Find city name and country from latitude and longitude in Swift

查看:178
本文介绍了在Swift中查找纬度和经度的城市名称和国家的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Swift3
的应用程序,我有字母问题,我找不到它的答案。

I'm working on application in Swift3 and I have letter problem i can't find the answer for it.

我如何根据纬度和经度知道城市名称和国家/地区的短名称?

How can I know city name and country short names base on latitude and longitude?

import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate{
    let locationManager = CLLocationManager()
    var latitude: Double = 0
    var longitude: Double = 0
    override func viewDidLoad() {
        super.viewDidLoad()
        // For use when the app is open & in the background
        locationManager.requestAlwaysAuthorization()
        // For use when the app is open
        //locationManager.requestWhenInUseAuthorization()
        locationManager.delegate = self
        locationManager.startUpdatingLocation()
        if CLLocationManager.locationServicesEnabled() {
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
            locationManager.startUpdatingLocation()
        }
    }
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if let location = locations.first {
            print(location.coordinate)
            latitude = location.coordinate.latitude
            longitude = location.coordinate.longitude
        }
    }
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        if (status == CLAuthorizationStatus.denied){
            showLocationDisabledpopUp()
        }
    }
    func showLocationDisabledpopUp() {
        let alertController = UIAlertController(title: "Background Location Access  Disabled", message: "We need your location", preferredStyle: .alert)
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
        alertController.addAction(cancelAction)
        let openAction = UIAlertAction(title: "Open Setting", style: .default) { (action) in
            if let url = URL(string: UIApplicationOpenSettingsURLString){
                UIApplication.shared.open(url, options: [:], completionHandler: nil)
            }
        }
        alertController.addAction(openAction)
        self.present(alertController, animated: true, completion: nil)
    }
}


推荐答案

我建议将 Google Maps API 与您的项目集成。如果您这样做,可以使用反向地理编码来完成您的任务提供。

I would recommend integrating Google Maps API with your project. If you do, your task can be achieved using Reverse Geocoding Google provides.

此外,Google还有用于IOS开发的Google Maps SDK 也值得考虑。

Furthermore, Google there is Google Maps SDK for IOS development, which is also worth considering.

UPD:您可以在不将地图集成到您的项目。基于答案,您可以使用对Google API的http请求来实现这一点。请求:

UPD: You can do that without integrating maps into your project. Basing on this answer, you can achieve that using http requests to Google API. The request to:

https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=API_KEY 

将返回 JSON 包含所请求地点信息的对象,包括国家和城市名称。

would return JSON object with information about the requested place, including country and city name.

BTW,我强烈建议使用 Alamofire 在Swift中发出http请求。

BTW, I highly recommend using Alamofire to make http requests in Swift.

这篇关于在Swift中查找纬度和经度的城市名称和国家的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆