如何在Swift中绘制地址,将地址转换为经度和纬度坐标? [英] How can I plot addresses in Swift, converting address to longitude and latitude coordinates?

查看:153
本文介绍了如何在Swift中绘制地址,将地址转换为经度和纬度坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

     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];
             }];

我一直在寻找如何使用Swift进行此操作的日子?!有人能指出我正确的方向吗?您能否解释一下用户是否需要互联网才能正常工作.抱歉,这些问题听起来有点基本,但是我对此很陌生,每天都在学习.

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.

我了解我必须使用此代码,但是找不到实现它的方法

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

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

推荐答案

尝试类似

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

这篇关于如何在Swift中绘制地址,将地址转换为经度和纬度坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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