objective-c 如何在 Swift 中绘制地址,将地址转换为经纬度坐标?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24706885/
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-09-03 21:22:58  来源:igfitidea点击:

How can I plot addresses in Swift, converting address to longitude and latitude coordinates?

objective-cswiftmkmapviewgeocoding

提问by agf119105

In Objective-C this code works fine to plot an address and it finds the longitude and latitude coordinates:

在 Objective-C 中,这段代码可以很好地绘制地址并找到经度和纬度坐标:

     NSString *address = @"1 Infinite Loop, CA, USA";
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:address 
             completionHandler:^(NSArray* placemarks, NSError* error){
                 // Check for returned placemarks
                 if (placemarks && placemarks.count > 0) {
                     CLPlacemark *topResult = [placemarks objectAtIndex:0];
                     // Create a MLPlacemark and add it to the map view
                     MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
                     [self.mapView addAnnotation:placemark];
                     [placemark release];
                 }
                 [geocoder release];
             }];

I have been searching for days on how to do this with Swift?! Could anyone point me in the right direction? Could you explain if a user needs an internet connection for this to work. Sorry if these questions sounds a bit basic, but I am quite new to this and learning every day.

我一直在寻找如何用 Swift 做到这一点的日子?!有人能指出我正确的方向吗?你能解释一下用户是否需要互联网连接才能工作。对不起,如果这些问题听起来有点基础,但我对此很陌生并且每天都在学习。

I understand that I have to use this code, but cannot find a way to implement it

我知道我必须使用此代码,但找不到实现它的方法

func geocodeAddressString(_ addressString: String!,
    completionHandler completionHandler: CLGeocodeCompletionHandler!)

回答by realtimez

XCode 9 & Swift 3:

XCode 9 和 Swift 3:

import CoreLocation

let address = "1 Infinite Loop, CA, USA"
let geocoder = CLGeocoder()

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
      }
    })

回答by kviksilver

Try something like

尝试类似的东西

var address = "1 Infinite Loop, CA, USA"
var geocoder = CLGeocoder()
geocoder.geocodeAddressString(address, {(placemarks: [AnyObject]!, error: NSError!) -> Void in
    if let placemark = placemarks?[0] as? CLPlacemark {
        self.mapView.addAnnotation(MKPlacemark(placemark: placemark))
    }
})

回答by MirekE

var address = "1 Infinite Loop, CA, USA"
var geocoder = CLGeocoder()    

geocoder.geocodeAddressString(address) {
    if let placemarks = ##代码## {
       println(placemarks)
    } else {
       println()
    }
}