美文网首页iOS地图开发
IOS自带地图MKMapView

IOS自带地图MKMapView

作者: FengxinLi | 来源:发表于2015-10-27 16:22 被阅读1451次

    首先需要导入MapKit.framework。

    如果需要加标注并且显示不同的图片。就需要自己定义类实现MKAnnotation协议。

    在下面方法里面判断是那种类型然后显示不同的图片

    - (MKAnnotationView*)mapView:(MKMapView*)mapView viewForAnnotation:(id)annotation{

    if([annotationisKindOfClass:[YujuShangdianAnnotationclass]]) {

    MKAnnotationView*annotationView =[self.mapViewdequeueReusableAnnotationViewWithIdentifier:@"CustomAnnotation"];

    if(!annotationView) {

    annotationView = [[MKAnnotationViewalloc]initWithAnnotation:annotation

    reuseIdentifier:@"CustomAnnotation"] ;

    annotationView.canShowCallout=NO;

    annotationView.image= [UIImageimageNamed:@"mapyujushangdian"];

    }

    returnannotationView;

    }elseif([annotationisKindOfClass:[YuchuanMaTouAnnotationclass]]) {

    MKAnnotationView*annotationView =[self.mapViewdequeueReusableAnnotationViewWithIdentifier:@"CustomAnnotation"];

    if(!annotationView) {

    annotationView = [[MKAnnotationViewalloc]initWithAnnotation:annotation

    reuseIdentifier:@"CustomAnnotation"] ;

    annotationView.canShowCallout=NO;

    annotationView.image= [UIImageimageNamed:@"mapyuchuanmatou"];

    }

    returnannotationView;

    }elseif([annotationisKindOfClass:[YushichangAnnotationclass]]) {

    MKAnnotationView*annotationView =[self.mapViewdequeueReusableAnnotationViewWithIdentifier:@"CustomAnnotation"];

    if(!annotationView) {

    annotationView = [[MKAnnotationViewalloc]initWithAnnotation:annotation

    reuseIdentifier:@"CustomAnnotation"] ;

    annotationView.canShowCallout=NO;

    annotationView.image= [UIImageimageNamed:@"mapfangzi"];

    }

    returnannotationView;

    }

    returnnil;

    }

    判断一个Annotation是否在当前地图中可见

    MKMapRectvisibleMapRect=self.mapView.visibleMapRect;

    NSSet*visibleAnnotation=[self.mapViewannotationsInMapRect:visibleMapRect];

    if([visibleAnnotationcontainsObject:anno]) {

    }

    将地图缩放到某个合适的位置 使一些Annotation同时可见

    MKPointAnnotation*pointann=[[MKPointAnnotationalloc]init];

    CLLocationCoordinate2Dcoorstart;

    coorstart.latitude=40;

    coorstart.longitude=115;

    pointann.coordinate=coorstart;

    [_mapViewaddAnnotation:pointann];

    MKPointAnnotation*pointann2=[[MKPointAnnotationalloc]init];

    CLLocationCoordinate2Dcoorstart2;

    coorstart2.latitude=40;

    coorstart2.longitude=116;

    pointann2.coordinate=coorstart2;

    [_mapViewaddAnnotation:pointann2];

    [self.mapViewshowAnnotations:@[pointann,pointann2]animated:YES];

    控制MapView动画的时间

    [MKMapViewanimateWithDuration:0.8delay:0.0options:UIViewAnimationOptionCurveEaseInOutanimations:^{

    [self.mapViewsetCenterCoordinate:anno.coordinateanimated:YES];

    } completion:^(BOOLfinished) {

    }];

    相关文章

      网友评论

        本文标题:IOS自带地图MKMapView

        本文链接:https://www.haomeiwen.com/subject/vmmxhttx.html