第四篇 iOS MKMapView的总结

来源:互联网 发布:淘宝被店家限制购买 编辑:程序博客网 时间:2024/05/29 05:10

首先初始化

    _rootMapView = [[MKMapView alloc]initWithFrame:CGRectMake(x,x,x,x)];

    _rootMapView.delegate =  self;
    _rootMapView.mapType = MKMapTypeStandard;
    _rootMapView.showsUserLocation = YES; //设置定位


定位

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
    mapView.showsUserLocation = NO;
    my = MKMapPointForCoordinate(userLocation.location.coordinate);
    _rootMapView.region = MKCoordinateRegionMakeWithDistance(userLocation.location.coordinate, 10000, 10000);// 设置显示的跨度
}

当地图的显示范围发生变化的方法

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{

    double meter = MKMapPointsPerMeterAtLatitude(mapView.centerCoordinate.latitude);//每个像素的跨度是多少米

}

在地图添加MKPointAnnotation

    for (int i = 0; i<[_annArray count]; i++) {
        PointAnnotation *ann = [[[PointAnnotation alloc]initWithDiction:[_annArray objectAtIndex:i]]autorelease];
        [arrayPoint addObject:ann];
        [_rootMapView addAnnotation:ann];
    }
//继承MKPointAnnotation的PointAnnotation
//
//@interface PointAnnotation : MKPointAnnotation
//
//@property (nonatomic, copy) NSString *title;
//@property (nonatomic, copy) NSString *subtitle;
//@property (nonatomic, assign) NSString *locationType;
//@property (nonatomic, retain) NSString *userName;
//
//@property (nonatomic, retain) NSString *header;
//
//- (id)initWithLocation:(CLLocationCoordinate2D)coord userName:(NSString *)newUser headerUrl:(NSString *)url;
//-(id)initWithDiction:(NSDictionary *)dic;
//
//@implementation PointAnnotation
//@synthesize title,subtitle,locationType,userName,header;
//
//- (id)initWithLocation:(CLLocationCoordinate2D)coord userName:(NSString *)newUser headerUrl:(NSString *)url{
//    self = [super init];
//    if (self) {
//        self.coordinate = coord;
//        userName = newUser;
//        header = url;
//    }
//    return self;
//}
//-(id)initWithDiction:(NSDictionary *)dic{
//    NSString *str = [dic objectForKey:@"Location"];
//    NSRange range = [str rangeOfString:@","];
//    NSString *first = [str substringToIndex:range.location];
//    NSString *secend = [str substringFromIndex:range.location+1];
//    double f = [first doubleValue];
//    double s = [secend doubleValue];
//    MKMapPoint point = {f,s};
//    self.coordinate = MKCoordinateForMapPoint(point);
//    userName = [dic objectForKey:@"Name"];
//    header = [dic objectForKey:@"HeadUrl"];
//    return self;
//}

设置自定义大头针的View

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;
    if ([annotation isKindOfClass:[PointAnnotation class]]){
        PointAnnotation *anno = (PointAnnotation *)annotation;

        //CustomAnnotationView 继承 MKAnnotationView;
        CustomAnnotationView *pinView = [[[CustomAnnotationView alloc]initWithAnnotation:anno reuseIdentifier:@"PointAnnotation"]autorelease];
        pinView.annotation = annotation;
        [pinView createView];
        pinView.canShowCallout = NO;//是否有callout View
        return pinView;
    }
    return nil;
}

点击AnnotationView的事件

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {

}


0 0