iOS开发 在google地图上显示本身的位置,值得收藏

一行代码显示你的位置

iOS中的MapKit集成了定位的功能,使用一行代码就能够在google地图上展现出本身当前的位置,代码以下: html


-(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. git

使用CLLocationManager和MKMapView

还有就是经过CoreLocation框架写代码去请求当前的位置,同样也很是简单: 框架

第一步:建立一个CLLocationManager实例 ide

CLLocationManager *locationManager = [[CLLocationManager alloc] init];


第二步:设置CLLocationManager实例委托和精度 google


locationManager.delegate = self; 
locationManager.desiredAccuracy = kCLLocationAccuracyBest;


第三步:设置距离筛选器distanceFilter,下面表示设备至少移动1000米,才通知委托更新 spa


locationManager.distanceFilter = 1000.0f;


或者没有筛选器的默认设置: code


locationManager.distanceFilter = kCLDistanceFilterNone;


第四步:启动请求 orm


[locationManager startUpdatingLocation];


使用下面代码中止请求: htm


[locationManager stopUpdatingLocation];

CLLocationManagerDelegate委托


这个委托中有:locationManager:didUpdateToLocation: fromLocation方法,用于获取经纬度。 get

能够使用下面代码从CLLocation 实例中获取经纬度


CLLocationDegrees latitude = theLocation.coordinate.latitude; 
CLLocationDegrees longitude = theLocation.coordinate.longitude;


使用下面代码获取你的海拔:


CLLocationDistance altitude = theLocation.altitude;


使用下面代码获取你的位移:


CLLocationDistance distance = [fromLocation distanceFromLocation:toLocation];


总结:本文主要是讲解了如何在iOS设备google地图上展现本身的当前位置。

相关文章
相关标签/搜索