第四篇 iOS MKMapView的总结

首先初始化

    _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 {

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值