iOS开发中最基本的位置功能实现示例

2020-01-14 16:25:30王冬梅

    }else{  
         
        //  当前设备位置的标注视图,返回nil,当前位置会创建一个默认的标注视图  
        return nil;  
    }  
      
}  
  
- (void)didClickAnnotationViewRightButtonAction:(UIButton *)button{  
  
    NSLog(@"%d %s",__LINE__,__FUNCTION__);  
}  
  
//  更新当前位置调用  
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{  
  
    NSLog(@"%d %s",__LINE__,__FUNCTION__);  
}  
  
//  选中标注视图  
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{  
      
    NSLog(@"%d %s",__LINE__,__FUNCTION__);  
}  
  
//  地图的现实区域改变了调用  
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{  
  
    NSLog(@"%d %s",__LINE__,__FUNCTION__);  
}  
  
- (void)didReceiveMemoryWarning  
{  
    [super didReceiveMemoryWarning];  
    // Dispose of any resources that can be recreated.  
}  
  
@end 

 

自定义MKAnnotationView

复制代码
#import <Foundation/Foundation.h>  
#import <MapKit/MapKit.h>  
//  引入MKAnnotation协议,切记不能忘记!!!!!!!!!  
@interface HMTAnnotation : NSObject<MKAnnotation>  
  
@property (nonatomic,readonly) CLLocationCoordinate2D coordinate;  //  坐标  
@property (nonatomic,copy) NSString *title;     //  位置名称  
@property (nonatomic,copy) NSString *subtitle;  //  位置子信息(可选)  
  
- (id)initWithCGLocation:(CLLocationCoordinate2D) coordinate;  
  
@end  
  
#import "HMTAnnotation.h"  
  
@implementation HMTAnnotation