在SwiftUI中获取当前的经度和纬度

我有一个应用程序,可以按经度和纬度向我显示有关餐馆的信息。如何获取当前位置并将其放置在URL中?我的想法是,我按该位置而不是给定的坐标进行搜索。

composer.json
hejunyan2010 回答:在SwiftUI中获取当前的经度和纬度

这不是一个Swift UI问题,而是一个通用的Swift问题。您可以使用CLLocationManager来获取用户的当前位置。确保先导入CoreLocation

import CoreLocation

...

init() {
    let latitude = CLLocationManager().location?.coordinate.latitude
    let longitude = CLLocationManager().location?.coordinate.longitude
    let url1 = "https://developers.zomato.com/api/v2.1/geocode?lat=\(latitude ?? 0.0)8&lon=\(longitude ?? 0.0)"
    let key = "c913841060ea614f7c0f5b5f120a21cb"
    //latitude = 33.4592298 - longitude = -70.645348

    print(url1)

  ...
}

您还必须确保获得用户访问其位置的权限。这需要在info.plist中设置NSLocationWhenInUseUsageDescription,并使用一个字符串值来说明您如何使用用户的位置。您可以通过CLLocationManager().requestWhenInUseAuthorization()请求访问。您可以通过CLLocationManager.authorizationStatus()检查位置授权状态。

本文链接:https://www.f2er.com/3099563.html

大家都在问