【问题标题】:Alternative way to get user Lat and Lng without didUpdateLocations?在没有 didUpdateLocations 的情况下获取用户 Lat 和 Lng 的替代方法?
【发布时间】:2015-07-09 21:53:26
【问题描述】:

有没有其他方法可以不使用didUpdateLocations函数来获取用户的经纬度。我只想执行一次并在加载视图时设置初始区域,而不是每次更新用户位置时。

我知道我可以使用下面的代码获取用户的 Lat 和 Lng,但是否有替代方法?

func locationManager(manager:CLLocationManager, didUpdateLocations locations:[AnyObject]) {

    var userLocation:CLLocation = locations[0] as CLLocation

    var latitude:CLLocationDegrees = userLocation.coordinate.latitude

    var longitude:CLLocationDegrees = userLocation.coordinate.longitude

    var latDelta:CLLocationDegrees = 0.01

    var lonDelta:CLLocationDegrees = 0.01

    var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)

    var location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)

    var region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)

    myMap.setRegion(region, animated: true)

}

【问题讨论】:

    标签: ios swift userlocation


    【解决方案1】:

    适用于 iOS 9:CLLocationManager.requestLocation()。此 API 仅用于您的目的 - 一种在不开启连续跟踪的情况下在谨慎时间获取位置的方法。

    请参阅this page 预览它的工作原理。

    在 iOS 8 及更低版本中,我不知道 API。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-21
      • 2021-01-16
      • 1970-01-01
      • 2022-01-02
      • 1970-01-01
      相关资源
      最近更新 更多