如何找到地址的坐标?

时间:2016-04-16 06:54:37

标签: swift mapkit

我正在创建一个使用MapKit在地址坐标上放置一个点的应用程序。

我上课了,所有的功能都在课堂上设置。

我需要能够返回“getLocation()”,它将是地址的字符串,并将该字符串与CLGeoCoder()。geocodeAdressString一起使用并获取坐标。

这是我的课程

import UIKit

class Vendor {

    private var name = String()
    private var type = String()
    private var location = String()
    private var logo = UIImage()

    required init(name: String, type: String) {
        self.name = name
        self.type = type
        self.location = ""
        self.logo = UIImage()
    }


    func getName() -> String {
        return self.name
    }
    func setName(name: String) {
        self.name = name
    }


    func getType() -> String {
        return self.type
    }
    func setType(type: String) {
        self.type = type
    }

    func getLocation() -> String {
        return self.location
    }
    func setLocation(address: String) {
        self.location = address
    }

    func getLogo() -> UIImage {
        return self.logo
    }
    func setLogo(image: UIImage) {
        self.logo = image
    }

}

1 个答案:

答案 0 :(得分:0)

假设供应商类中的位置是供应商的街道,城市和国家/地区,您的代码可能如下所示:

    func geoCodeVendor() {

    let vendorLocation = myVendor.getLocation
    let geocoder = CLGeocoder()
    geocoder.geocodeAddressString(vendorLocation) { (placemarks, error) in

        self.centerCoordinate = (placemarks?.first?.location?.coordinate)!
        //Whather more you want to do in this completion handler.

    }
}

在这里,您已经在要对供应商进行地理编码的类中创建了Vendor,myVendor的实例。在该类中,您创建了一个属性centerCoordinate = CLLocationCoordinate2D()。

通常,您有一个供应商类,其名称,地址和城市位于不同的属性中。例如:

class Vendor {

    private var name = String()
    private var type = String()
    private var address = String()
    private var city = String()
    private var logo = UIImage()
}

然后在getLocation函数中附加地址和城市。请提醒一下,当地址尽可能完整时,地理编码更准确,即地址(包括号码,城市,国家。

希望这有帮助。

相关问题