溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MapView的應(yīng)用

發(fā)布時間:2020-08-22 09:14:39 來源:網(wǎng)絡(luò) 閱讀:347 作者:jna_114 欄目:開發(fā)技術(shù)

 MapView在8.0之后,用法有了一些新的用法(代理方法),在此之前需要導(dǎo)入

#import <MapKit/MapKit.h>


//創(chuàng)建位置服務(wù)對象

    locationManager = [[CLLocationManager alloc] init];

    

    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    

    //設(shè)置定位代理

    locationManager.delegate = self;

    

    //如果實在iOS8.0之后,我們需要添加以下操作

    //1.調(diào)用方法--requestWhenInUseAuthorization 或者 requestAlwaysAuthorization

    //2.

    

    if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {

        [locationManager requestWhenInUseAuthorization];

    }

    

    //開始定位

    [locationManager startUpdatingLocation];

    

    

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


#pragma mark -CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

{

    NSLog(@"定位成功");

    //停止定位

    [locationManager stopUpdatingLocation];

    

    CLLocationCoordinate2D coordinate = newLocation.coordinate;

    

    NSLog(@"位置:緯度:%.2f----經(jīng)度:%.2f", coordinate.latitude, coordinate.longitude);

    

}


- (void)locationManager:(CLLocationManager *)manager

     didUpdateLocations:(NSArray *)locations

{

    NSLog(@"新方法定位成功");

    //停止定位

    [locationManager stopUpdatingLocation];

    

    CLLocation *location = [locations lastObject];

    

    CLLocationCoordinate2D coordinate = location.coordinate;


    NSLog(@"位置:緯度:%.2f----經(jīng)度:%.2f", coordinate.latitude, coordinate.longitude);

    

    //iOS5.0之前使用位置反編碼

    MKReverseGeocoder *mkReverse = [[MKReverseGeocoder alloc] initWithCoordinate:coordinate];

    mkReverse.delegate = self;

    //開始反編碼

    [mkReverse start];

    

    

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];

    [geocoder reverseGeocodeLocation:location

                   completionHandler:^(NSArray *placemarks, NSError *error) {

                       

                       NSLog(@"---------CLGeocoder---------------");

                       

                       

                       for (CLPlacemark *place in placemarks) {

                           NSLog(@"name,%@",place.name);                       // 位置名

                           NSLog(@"thoroughfare,%@",place.thoroughfare);       // 街道

                           NSLog(@"subThoroughfare,%@",place.subThoroughfare); // 子街道

                           NSLog(@"locality,%@",place.locality);               //

                           NSLog(@"subLocality,%@",place.subLocality);         // 區(qū)

                           NSLog(@"country,%@",place.country);                 // 國家

                       }

                   }

     ]; // CLGeocoder反編碼

    

    

}


#pragma mark -MKReverseGeocoderDelegate

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)place

{

    

    NSLog(@"-----------MKReverseGeocoderDelegate----------");

    NSLog(@"name,%@",place.name);                       // 位置名

    NSLog(@"thoroughfare,%@",place.thoroughfare);       // 街道

    NSLog(@"subThoroughfare,%@",place.subThoroughfare); // 子街道

    NSLog(@"locality,%@",place.locality);               //

    NSLog(@"subLocality,%@",place.subLocality);         // 區(qū)

    NSLog(@"country,%@",place.country);                 // 國家

}


- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error

{

    NSLog(@"error is %@", error);

}


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI