国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > iOS > 正文

iOS-用MapKit和CoreLocation來實(shí)現(xiàn)移動(dòng)設(shè)備(地圖與定位)

2019-11-14 20:44:14
字體:
供稿:網(wǎng)友

1.前言                              

發(fā)現(xiàn)在很多的社交軟件都引入了地圖和定位功能,如果我們要想實(shí)現(xiàn)這兩大功能,需要利用到兩個(gè)框架:MapKitCoreLocation
 
我們先來看看CoreLocation框架:
 
它可以使用硬件設(shè)備來進(jìn)行定位服務(wù),不需要地圖,精度相對(duì)略差,省電。
 
MapKit框架:
 
能夠使應(yīng)用程序做一些地圖展示與交互的相關(guān)功能,必須有地圖,精度相對(duì)較高,費(fèi)電。
 
下面我就說說它的能實(shí)現(xiàn)的一些常用功能

2.地圖常見操作                        

@PRoperty (weak, nonatomic) IBOutletMKMapView *mapView;

2.1. 標(biāo)記用戶當(dāng)前位置、跟蹤用戶位置                        

1     // 標(biāo)記用戶當(dāng)前位置2     // 跟蹤用戶位置3     [_mapView setUserTrackingMode:MKUserTrackingModeFollow];

 

 

2.1. 地圖的類型                                  

可以通過設(shè)置MKMapView的mapViewType設(shè)置地圖類型
MKMapTypeStandard 普通地圖
MKMapTypeSatellite 衛(wèi)星云圖
MKMapTypeHybrid普通地圖覆蓋于衛(wèi)星云圖之上                     
 // 地圖類型    [_mapView setMapType:MKMapTypeHybrid];

 

2.2.通過代理的方式可以跟蹤用戶的位置變化                     

 

2.2.1.mapViewWillStartLoadingMap: 當(dāng)?shù)貓D界面將要加載時(shí)調(diào)用

 

2.2.2.mapView:viewForAnnotation: 當(dāng)?shù)貓D上有大頭針時(shí)調(diào)用

 

2.2.3.mapViewWillStartLocatingUser:當(dāng)準(zhǔn)備進(jìn)行一個(gè)位置定位時(shí)調(diào)用

 

2.2.4.mapView:regionDidChangeAnimated: 當(dāng)顯示的區(qū)域發(fā)生變化時(shí)調(diào)用
 

2.2.5.mapView:didUpdateUserLocation:當(dāng)用戶位置發(fā)生變化時(shí)調(diào)用

1 // 通過代理的方式可以跟蹤用戶的位置變化2     _mapView.delegate = self;

 

#pragma mark - 地圖代理方法#pragma mark 會(huì)頻繁調(diào)用,非常費(fèi)電- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{        // 顯示用戶周邊信息 拉近地圖 設(shè)置地圖顯示區(qū)域

    CLLocationCoordinate2D center = userLocation.location.coordinate;

    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(center, 100.0, 100.0);

 

    [mapView setRegion:region animated:YES];

}

 

2.3.添加默認(rèn)大頭針(地標(biāo))                              

1.通過MapView的addAnnotation方法可以添加一個(gè)大頭針到地圖上
2.通過MapView的addAnnotations方法可以添加多個(gè)大頭針到地圖上
頭文件里說明:- (void)addAnnotation:(id <MKAnnotation>)annotation;
4.說明需要傳入一個(gè)遵守了MKAnnotation協(xié)議的對(duì)象
1     MyAnnotation *annotation2 = [[MyAnnotation alloc] init];2     annotation2.coordinate = CLLocationCoordinate2DMake(30, 106);3     annotation2.title = @"重慶";4     annotation2.subtitle = @"重慶詳細(xì)描述";5     annotation2.imageName = @"head0";
     [_mapView addAnnotation:annotation2];

 

2.4.添加自定義大頭針(重用地標(biāo))                            

 1 #pragma mark 自定義大頭針視圖 2 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 3 { 4     // 判斷annotation參數(shù)是否是MyAnnotation 5     // 如果不是MyAnnotaion說明是系統(tǒng)的大頭針,無需做處理 6     if (![annotation isKindOfClass:[MyAnnotation class]]) { 7         // 使用系統(tǒng)默認(rèn)的大頭針 8         return nil; 9     }10     11     // 可重用標(biāo)示符12     static NSString *ID = @"MyAnno";13     14     // 查詢可重用的大頭針15     MKAnnotationView *annoView = [mapView dequeueReusableAnnotationViewWithIdentifier:ID];16     17     // 如果沒有找到,再去實(shí)例化大頭針18     if (annoView == nil) {19         annoView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:ID];20         21         // 自定義大頭針視圖,如果要接受用戶響應(yīng),需要設(shè)置此屬性22         annoView.canShowCallout = YES;23     }24     25     // 設(shè)置大頭針26     annoView.annotation = annotation;27     // 轉(zhuǎn)換成MyAnnotation28     // 設(shè)置大頭針視圖的圖像29     MyAnnotation *anno = annotation;30     annoView.image = [UIImage imageNamed:anno.imageName];31     32     NSLog(@"自定義大頭針");33     34     return annoView;35 }

 3.定位                              

 

   // 定位服務(wù)管理器    CLLocationManager *_locationManager;        // 使用地理編碼器    CLGeocoder          *_geocoder;

 

 

 

 

 1. 在開發(fā)LBS類的應(yīng)用時(shí),獲取用戶定位信息之前,一定要判斷一下定位服務(wù)是否允許  

 

 locationServicesEnabled

1     2     if (![CLLocationManager locationServicesEnabled]) {3         NSLog(@"定位服務(wù)不可用!");4         return;5     }6     

 

 

 2. 開啟定位,獲取自己的當(dāng)前位置                            

 [_locationManager startUpdatingLocation];

 

 3. 根據(jù)經(jīng)緯度,知道準(zhǔn)確的地名                              

    reverseGeocodeLocation

 

 1 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 2 { 3     NSLog(@"位置變化: %@", locations[0]); 4      5     // 根據(jù)經(jīng)緯度查找(去蘋果后臺(tái)查找準(zhǔn)確的位置,必須聯(lián)網(wǎng)才能用) 6     [_geocoder reverseGeocodeLocation:locations[0] completionHandler:^(NSArray *placemarks, NSError *error) { 7          8         NSLog(@"%@", placemarks[0]); 9         10     }];11 }

 

 

 

 

 

 4. 根據(jù)定名,獲取到經(jīng)緯度,一般用在導(dǎo)航                        

    geocodeAddressString

1    [_geocoder geocodeAddressString:@"王府井" completionHandler:^(NSArray *placemarks, NSError *error) {2         3         for (CLPlacemark *placemark in placemarks) {4             NSLog(@"aaaa______%@ %lu", placemark, (unsigned long)placemarks.count);5         }6         7     }];

 

                                                             清澈Saup  


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 枣强县| 镇原县| 霞浦县| 象州县| 禹城市| 疏勒县| 纳雍县| 克拉玛依市| 邹平县| 芷江| 南投市| 鹤壁市| 牡丹江市| 高台县| 漳州市| 岱山县| 崇左市| 镇原县| 万州区| 凤阳县| 亚东县| 北票市| 盖州市| 叶城县| 广平县| 棋牌| 罗定市| 东宁县| 临沧市| 昆明市| 阳曲县| 浦江县| 社会| 闻喜县| 道孚县| 六枝特区| 左贡县| 扬中市| 忻城县| 左权县| 新巴尔虎左旗|