如何在场地/企业名称上使用Swift地理编码器获取地址

ffdz8vbo  于 5个月前  发布在  Swift
关注(0)|答案(1)|浏览(58)

我试图获得一个地点/企业名称的地址。有了地点名称,我有城市和州,但它是成功的不到10%的时间。问题似乎是找到位置,而不是破译地址。
示例输入:“Jazz Kitchen Indianapolis,Indiana”
典型输出:“未找到位置”
谢谢你,谢谢

geocoder.geocodeAddressString(newSearch) { (placemarks, error) in
                    if let error = error {
                        print("Geocoding error: \(error.localizedDescription)")
                        geoError = true
                    }
                    if let placemark = placemarks?.first {
                        if let address = placemark.location?.coordinate {
                            print("Address: \(address)")
                            let webLoc = CLLocation(latitude: address.latitude, longitude: address.longitude)
                            uploadLoc(geoLoc: webLoc)
                        } else {
                            print("No address found")
                            geoError = true
                        }
                    } else {
                        print("No location found")
                        geoError = true
                    }
            }

字符串

9cbw7uwe

9cbw7uwe1#

对于您的问题中的示例请求,使用MKLocalSearchCLGeocoder更好。
代码如下:

import MapKit

let req = MKLocalSearch.Request()
req.naturalLanguageQuery = "Jazz Kitchen Indianapolis,Indiana"
let srch = MKLocalSearch(request: req)
srch.start { resp, error in
    if let resp {
        print(resp.mapItems)
    } else if let error {
        print(error)
    } else {
        print("nothing")
    }
}

字符串
给出以下结果:

[<MKMapItem: 0x600002df8d00> {
    isCurrentLocation = 0;
    name = "The Jazz Kitchen";
    phoneNumber = "<redacted for SO>";
    placemark = "The Jazz Kitchen, 5377 N College Ave, Indianapolis, IN 46220, United States @ <+39.85087740,-86.14554230> +/- 0.00m, region CLCircularRegion (identifier:'<+39.85087741,-86.14554230> radius 141.17', center:<+39.85087741,-86.14554230>, radius:141.17m)";
    timeZone = "America/Indiana/Indianapolis (EST) offset -18000";
    url = "http://www.thejazzkitchen.com";
}]

相关问题