Swift开发教程--如何通过定位来获取经纬度

前端之家收集整理的这篇文章主要介绍了Swift开发教程--如何通过定位来获取经纬度前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

首先,定义和继承协议CLLocationManagerDelegate

var@H_403_4@ locManager:@H_403_4@CLLocationManager@H_403_4@?;@H_403_4@


@H_403_4@

然后调用

@H_403_4@self@H_403_4@.@H_403_4@locManager@H_403_4@ = @H_403_4@CLLocationManager@H_403_4@();@H_403_4@//[[CLLocationManager alloc] init];@H_403_4@

@H_403_4@self@H_403_4@.@H_403_4@locManager@H_403_4@!.@H_403_4@delegate@H_403_4@ = @H_403_4@self@H_403_4@;@H_403_4@

@H_403_4@self@H_403_4@.@H_403_4@locManager@H_403_4@!.@H_403_4@desiredAccuracy@H_403_4@ = @H_403_4@kCLLocationAccuracyBest@H_403_4@;@H_403_4@

@H_403_4@self@H_403_4@.@H_403_4@locManager@H_403_4@!.@H_403_4@distanceFilter@H_403_4@ = 5.0;@H_403_4@

@H_403_4@//@H_403_4@设置定位权限@H_403_4@ @H_403_4@仅@H_403_4@ios8@H_403_4@有意义@H_403_4@

@H_403_4@self@H_403_4@.@H_403_4@locManager@H_403_4@?.@H_403_4@requestWhenInUseAuthorization@H_403_4@();@H_403_4@//@H_403_4@前台定位@H_403_4@@H_403_4@

@H_403_4@self@H_403_4@.@H_403_4@locManager@H_403_4@?.@H_403_4@startUpdatingLocation@H_403_4@();@H_403_4@


最后,根据回调来获取经纬度值

func@H_403_4@ locationManager(manager: @H_403_4@CLLocationManager@H_403_4@!,didUpdateToLocation newLocation: @H_403_4@CLLocation@H_403_4@!,fromLocation oldLocation: @H_403_4@CLLocation@H_403_4@!) {@H_403_4@

@H_403_4@println@H_403_4@(@H_403_4@"@H_403_4@经度:@H_403_4@%g"@H_403_4@,newLocation.@H_403_4@coordinate@H_403_4@.@H_403_4@latitude@H_403_4@);@H_403_4@

@H_403_4@println@H_403_4@(@H_403_4@"@H_403_4@纬度:@H_403_4@%g"@H_403_4@,newLocation.@H_403_4@coordinate@H_403_4@.@H_403_4@longitude@H_403_4@);@H_403_4@

}@H_403_4@

@H_403_4@func@H_403_4@ locationManager(manager: @H_403_4@CLLocationManager@H_403_4@!,didFailWithError error: @H_403_4@NSError@H_403_4@!) {@H_403_4@

@H_403_4@println@H_403_4@(@H_403_4@"locationManager error"@H_403_4@);@H_403_4@

}@H_403_4@


还有一点,别忘了在info.plist里,加一项:

NSLocationWhenInUseUsageDescription,设为YES

猜你在找的Swift相关文章