在MkMapView中在指定位置添加大头针

在MkMapView中添加自己当前位置的大头针的基础上实现指定位置添加大头针,实现的效果如下: 添加相关代码: 添加DisplayMap : #import Foundation/Foundation.h #import MapKit/MKAnnotation.h @interface DisplayM

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

在MkMapView中在指定位置添加大头针_第1张图片

添加相关代码:

添加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;
    }

你可能感兴趣的:(在MkMapView中在指定位置添加大头针)