首页 > IT职业 > IT职场 > iOS开发如何在google地图上显示自己的位置
一行代码显示你的位置
iOS中的MapKit集成了定位的功能,使用一行代码就可以在google地图上展示出自己当前的位置,代码如下:
-(IBAction) showLocation:(id) sender {
if ([[btnShowLocation titleForState:UIControlStateNormal]
isEqualToString:@"Show My Location"]) {
[btnShowLocation setTitle:@"Hide My Location"
forState:UIControlStateNormal];
mapView.showsUserLocation = YES;
} else {
[btnShowLocation setTitle:@"Show My Location"
forState:UIControlStateNormal];
mapView.showsUserLocation = NO;
}
} |
关键的代码就是:mapView.showUserLocation=YES.
使用CLLocationManager和MKMapView
还有就是通过CoreLocation框架写代码去请求当前的位置,一样也非常简单:
第一步:创建一个CLLocationManager实例
CLLocationManager *locationManager = [[CLLocationManager alloc] init]; |
第二步:设置CLLocationManager实例委托和精度
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest; |
第三步:设置距离筛选器distanceFilter,下面表示设备至少移动1000米,才通知委托更新
locationManager.distanceFilter = 1000.0f; |
或者没有筛选器的默认设置:
locationManager.distanceFilter = kCLDistanceFilterNone; |
第四步:启动请求
[locationManager startUpdatingLocation]; |
使用下面代码停止请求:
[locationManager stopUpdatingLocation]; |
CLLocationManagerDelegate委托
这个委托中有:locationManager:didUpdateToLocation: fromLocation方法,用于获取经纬度。
可以使用下面代码从CLLocation 实例中获取经纬度
CLLocationDegrees latitude = theLocation.coordinate.latitude; CLLocationDegrees longitude = theLocation.coordinate.longitude; |
使用下面代码获取你的海拔:
CLLocationDistance altitude = theLocation.altitude; |
使用下面代码获取你的位移:
CLLocationDistance distance = [fromLocation distanceFromLocation:toLocation];
总结:本文主要是讲解了如何在iOS设备google地图上展示自己的当前位置。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4422/viewspace-2808409/ ,如需转载,请注明出处,否则将追究法律责任。