ios 将地址快速转换为坐标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42279252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:53:48  来源:igfitidea点击:

Convert address to coordinates swift

iosswiftcoordinatescore-location

提问by Paul Bénéteau

How can I convert a Stringaddress to CLLocationcoordinates with Swift? I have no code yet, I looked for a solution but I didn't find anyone yet.

如何使用 Swift将String地址转换为CLLocation坐标?我还没有代码,我寻找了一个解决方案,但我还没有找到任何人。

Thanks.

谢谢。

回答by naglerrr

This is pretty easy.

这很容易。

    let address = "1 Infinite Loop, Cupertino, CA 95014"

    let geoCoder = CLGeocoder()
    geoCoder.geocodeAddressString(address) { (placemarks, error) in
        guard
            let placemarks = placemarks,
            let location = placemarks.first?.location
        else {
            // handle no location found
            return
        }

        // Use your location
    }

You will also need to add and import CoreLocation framework.

您还需要添加和导入 CoreLocation 框架。

回答by Greg

You can use CLGeocoder, you can convert address(string) to coordinate and you vice versa, try this:

您可以使用 CLGeocoder,您可以将地址(字符串)转换为坐标,反之亦然,试试这个:

import CoreLocation

var geocoder = CLGeocoder()
geocoder.geocodeAddressString("your address") {
    placemarks, error in
    let placemark = placemarks?.first
    let lat = placemark?.location?.coordinate.latitude
    let lon = placemark?.location?.coordinate.longitude
    print("Lat: \(lat), Lon: \(lon)")
}

回答by Adrian

Here's what I came up with to return a CLLocationCoordinat2Dobject:

这是我想出的返回CLLocationCoordinat2D对象的方法:

func getLocation(from address: String, completion: @escaping (_ location: CLLocationCoordinate2D?)-> Void) {
    let geocoder = CLGeocoder()
    geocoder.geocodeAddressString(address) { (placemarks, error) in
        guard let placemarks = placemarks,
        let location = placemarks.first?.location?.coordinate else {
            completion(nil)
            return
        }
        completion(location)
    }
}

So let's say I've got this address:

所以假设我有这个地址:

let address = "Springfield, Illinois"

Usage

用法

getLocation(from: address) { location in
    print("Location is", location.debugDescription)
    // Location is Optional(__C.CLLocationCoordinate2D(latitude: 39.799372, longitude: -89.644458))

}

回答by Azhar

This works

这有效

let geocoder = CLGeocoder() 
let address = "8787 Snouffer School Rd, Montgomery Village, MD 20879"
        geocoder.geocodeAddressString(address, completionHandler: {(placemarks, error) -> Void in
            if((error) != nil){
                print("Error", error ?? "")
            }
            if let placemark = placemarks?.first {
                let coordinates:CLLocationCoordinate2D = placemark.location!.coordinate
                print("Lat: \(coordinates.latitude) -- Long: \(coordinates.longitude)")
            }
        })

回答by Deep Ocean

Swift 5 and Swift 5.1

Swift 5 和 Swift 5.1

import CoreLocation

var geocoder = CLGeocoder() 
geocoder.geocodeAddressString("your address") { placemarks, error in
    let placemark = placemarks?.first
    let lat = placemark?.location?.coordinate.latitude
    let lon = placemark?.location?.coordinate.longitude
    print("Lat: \(lat), Lon: \(lon)") 
}