iOS開發之在Google地圖上顯示所在位置
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地圖上顯示所在位置的內容介紹完了,本篇文章主要是講解了如何在iOS設備google地圖上展示自己的當前位置,***希望本文對你有所幫助!