iOS開發之在google地圖上顯示本身的位置

一行代碼顯示你的位置 git

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. ide

使用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地圖上展現本身的當前位置。 google

相關文章
相關標籤/搜索