11.4 地图大头针 重用标示符

来源:互联网 发布:易推客营销软件下载 编辑:程序博客网 时间:2024/05/29 11:30

  if(_mapView==nil)

    {

        _mapView=[[MKMapView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];

        [self.view addSubview:_mapView];

        

    }

    _mapView.mapType=MKMapTypeStandard;

    _mapView.showsUserLocation=NO;

    _mapView.userLocation.title=@"我在这里郑州火车站 ";

    _mapView.userLocation.subtitle=@"我要回家  ";

    _mapView.userTrackingMode=MKUserTrackingModeFollowWithHeading;//更新方向;

    _mapView.delegate=self;


    //设置地图显示区域

    //坐标范围

    CLLocationCoordinate2D center={34.7485365,113.6623235};

    MKCoordinateSpan span={0.02,0.03};

    

    MKCoordinateRegion region={center,span};

    

    [_mapView setRegion:region  animated:YES];

    //添加标注 在地图上添加标注 其实是添加得标注对象 而不是View   MKPointAnnotation继承MKShape  继承NSObject

    MKPointAnnotation *point=[[MKPointAnnotation alloc]init];

    //火车站经纬度

    CLLocationCoordinate2D coordinate={34.7,113.7};

    

    point.title=@"我要去这里";

    point.subtitle=@"和你一起";

    //标注得位置

    point.coordinate=coordinate;

    [_mapView addAnnotation:point];

    [point release];


    

    // Do any additional setup after loading the view from its nib.

}

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation

{

    //定义一个重用标示符

    static  NSString *annotationString=@"annotation";

//根据重用标示符 从重用队列中找到一个可以重用得标示试图

    MKAnnotationView *pinAnnotationView=[mapView dequeueReusableAnnotationViewWithIdentifier:annotationString];

    

    if(pinAnnotationView==nil)

    {

        //创建一个大头针

        pinAnnotationView=[[[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annotationString]autorelease];

    }

    if (annotation==mapView.userLocation)

    {


        //设置大头针得颜色

        //pinAnnotationView.tintColor=;

        //pinAnnotationView.animatesDrop=YES;//下落有动画

        pinAnnotationView.canShowCallout=YES;//可以显示详情

        return pinAnnotationView;

    }

  else

  {

    //设置标注试图得图片

      

      pinAnnotationView.canShowCallout=YES;//可以显示详情


    pinAnnotationView.image=[UIImage imageNamed:@"arrest.png"];

  }

    return pinAnnotationView;

}


0 0