iPhone开发之在MkMapView中在指定位置添加大头针

来源:互联网 发布:js模拟select下拉菜单 编辑:程序博客网 时间:2024/04/28 19:01

在MkMapView中添加自己当前位置的大头针的基础上实现指定位置添加大头针,实现的效果如下:

1.jpg

添加相关代码:

添加DisplayMap :

    #import <Foundation/Foundation.h>
    #import <MapKit/MKAnnotation.h>
    @interface DisplayMap : NSObject
    <MKAnnotation>{
        CLLocationCoordinate2D coordinate;
        NSString *title;
        NSString *subtitle;
    }
    @property (nonatomic, assign) CLLocationCoordinate2D coordinate;
    @property (nonatomic, copy) NSString *title;
    @property (nonatomic, copy) NSString *subtitle;
    @end


    #import "DisplayMap.h"
    @implementation DisplayMap
    @synthesize coordinate,title,subtitle;
    -(void)dealloc{
        [title release];
        [super dealloc];
    }
    @end

修改collert中的代码:

    - (void)viewDidLoad {
        [super viewDidLoad];
        //mapView.showsUserLocation=YES;
        self.mapView.delegate=self;
        CLLocationManager *locationManager = [[CLLocationManager alloc] init];//创建位置管理器
        locationManager.delegate=self;//设置代理
        locationManager.desiredAccuracy=kCLLocationAccuracyBest;//指定需要的精度级别
        locationManager.distanceFilter=1000.0f;//设置距离筛选器
        [locationManager startUpdatingLocation];//启动位置管理器

        MKCoordinateRegion theRegion = { {0.0, 0.0 }, { 0.0, 0.0 } };
        theRegion.center=[[locationManager location] coordinate];
        [locationManager release];
        [mapView setZoomEnabled:YES];
        [mapView setScrollEnabled:YES];
        theRegion.span.longitudeDelta = 0.01f;
        theRegion.span.latitudeDelta = 0.01f;
        [mapView setRegion:theRegion animated:YES];
        DisplayMap *ann = [[DisplayMap alloc] init];
        ann.title = @"欧陆经典";
        ann.subtitle = @"vsp";
        //地点名字
        ann.coordinate = theRegion.center;
        [mapView addAnnotation:ann];
    }
    - (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
    {
        MKPinAnnotationView *pinView = nil;
        if(annotation != mapView.userLocation)
        {
            static NSString *defaultPinID = @"com.invasivecode.pin";
            pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
            if ( pinView == nil ) pinView = [[[MKPinAnnotationView alloc]
                                              initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
            pinView.pinColor = MKPinAnnotationColorRed;
            pinView.canShowCallout = YES;
            pinView.animatesDrop = YES;
        }
        else {
            [mapView.userLocation setTitle:@"欧陆经典"];
        [mapView.userLocation setSubtitle:@"vsp"];
        }
        return pinView;
    }


http://www.mobiletrain.org/lecture/doc/iphone/2011-09/696.html

0 0
原创粉丝点击