当前位置: 首页 > 面试题库 >

将坐标转换为城市名称?

晁英彦
2023-03-14
问题内容

如何使用MapKit从坐标获取地址?

当在地图上长按时,我得到了以下代码:

func didLongPressMap(sender: UILongPressGestureRecognizer) {

    if sender.state == UIGestureRecognizerState.Began {
        let touchPoint = sender.locationInView(self.mapView)
        let touchCoordinate = self.mapView.convertPoint(touchPoint, toCoordinateFromView: self.mapView)
        var annotation = MKPointAnnotation()
        annotation.coordinate = touchCoordinate
        annotation.title = "Your position"
        self.mapView.addAnnotation(annotation) //drops the pin
        println("lat:  \(touchCoordinate.latitude)")
        var num = (touchCoordinate.latitude as NSNumber).floatValue
        var formatter = NSNumberFormatter()
        formatter.maximumFractionDigits = 4
        formatter.minimumFractionDigits = 4
        var str = formatter.stringFromNumber(num)
        println("long: \(touchCoordinate.longitude)")
        var num1 = (touchCoordinate.longitude as NSNumber).floatValue
        var formatter1 = NSNumberFormatter()
        formatter1.maximumFractionDigits = 4
        formatter1.minimumFractionDigits = 4
        var str1 = formatter1.stringFromNumber(num1)
        self.adressLoLa.text = "\(num),\(num1)"
                }
}

我想annotation.title用完整的地址(街道,城市,邮政编码,国家)打印。


问题答案:

SWIFT 4.2:编辑

MapKit 框架确实提供了一种从坐标获取地址详细信息的方法。

您需要使用地图套件的 反向地理编码
CLGeocoder类用于从地址获取位置,并从位置(坐标)获取地址。该方法reverseGeocodeLocation将从坐标返回地址详细信息。

方法接受CLLocation作为参数并返回CLPlacemark,其中包含地址字典。

所以现在上述方法将更新为:

@objc func didLongPressMap(sender: UILongPressGestureRecognizer) {

    if sender.state == UIGestureRecognizer.State.began {
        let touchPoint = sender.location(in: mapView)
        let touchCoordinate = mapView.convert(touchPoint, toCoordinateFrom: self.mapView)
        let annotation = MKPointAnnotation()
        annotation.coordinate = touchCoordinate
        annotation.title = "Your position"
        mapView.addAnnotation(annotation) //drops the pin
        print("lat:  \(touchCoordinate.latitude)")
        let num = touchCoordinate.latitude as NSNumber
        let formatter = NumberFormatter()
        formatter.maximumFractionDigits = 4
        formatter.minimumFractionDigits = 4
        _ = formatter.string(from: num)
        print("long: \(touchCoordinate.longitude)")
        let num1 = touchCoordinate.longitude as NSNumber
        let formatter1 = NumberFormatter()
        formatter1.maximumFractionDigits = 4
        formatter1.minimumFractionDigits = 4
        _ = formatter1.string(from: num1)
        self.adressLoLa.text = "\(num),\(num1)"

        // Add below code to get address for touch coordinates.
        let geoCoder = CLGeocoder()
        let location = CLLocation(latitude: touchCoordinate.latitude, longitude: touchCoordinate.longitude)
        geoCoder.reverseGeocodeLocation(location, completionHandler:
            {
                placemarks, error -> Void in

                // Place details
                guard let placeMark = placemarks?.first else { return }

                // Location name
                if let locationName = placeMark.location {
                    print(locationName)
                }
                // Street address
                if let street = placeMark.thoroughfare {
                    print(street)
                }
                // City
                if let city = placeMark.subAdministrativeArea {
                    print(city)
                }
                // Zip code
                if let zip = placeMark.isoCountryCode {
                    print(zip)
                }
                // Country
                if let country = placeMark.country {
                    print(country)
                }
        })
    }
}


 类似资料:
  • 我有这样的代码,当长按地图时,它得到坐标: 并且我想在中打印完整的地址(街道、城市、邮编、国家)。

  • 问题内容: 我没有看到关于SO的问题,但所有问题都在Swift 2中已经存在。我从Apple网站获得了此功能,可以将城市名称转换为纬度和经度,但是我不确定该函数将返回什么(因为return语句后没有任何内容) )以及我应该通过什么。有人可以解释一下吗,或者告诉我如何使用它。 问题答案: 您可以按照以下步骤进行操作: 用法:

  • 获取城市ID 说明 通过坐标获取城市id 地址URL /v1/common/Cities/getCityIdByPoi 支持格式 Json HTTP请求方式 GET 是否需要登录 是 关于登录授权,参见 如何登录授权 访问授权限制 暂无 请求参数 名称 类型 必选 描述 client_id string yes 申请应用时分配的APP_KEY access_token string yes 乘客认

  • 本文向大家介绍使用HTML5将坐标转换为地名,包括了使用HTML5将坐标转换为地名的使用技巧和注意事项,需要的朋友参考一下 为此,请使用Google Maps API进行反向地理编码。 地理编码 是指将人类可读的地址翻译成映射上的某个位置。 将映射上的位置转换为人类可读地址的相反过程称为反向地理编码。 示例 让我们看一个设置纬度和经度的示例- 反向地址解析器将匹配国家,省,城市和邻里,街道地址和邮

  • 我正在尝试将球坐标转换为笛卡尔坐标,以绘制一个简单的三维金字塔。 下面是获取金字塔四个主要角的代码,具体取决于极角yrad和方位角xrad以及顶点坐标x和y: 方位角轴似乎工作正常,但问题是,在操纵极轴角时,当它们越过天顶或底部时,左右会互换,如图所示(选择顶部的mp4以更平滑地播放):http://gyazo.com/4a245713c232893960863cf4ea4186f6 怎么了?

  • 我试图将图像从极坐标转换为笛卡尔坐标,但在应用公式后,我得到了浮点坐标(r和teta),我不知道如何使用x和y的浮点来表示空间中的点。可能有一种方法可以将它们转换为整数,并仍然保留分布,但我不知道如何。我知道OpenCV中有像warpPolar这样的函数,但我想自己实现它。任何想法都会有帮助:) 这是我的代码: