MKMapView指定坐標添加大頭針


#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface UserLocationViewController : UIViewController<MKMapViewDelegate>{

    MKMapView *mapView;
    UIButton *btn;
}
@property(nonatomic,retain) MKMapView *mapView;
@property(nonatomic,retain) UIButton *btn;
-( void)btnPressed:( id)sender;
@end

 

     

      - ( void )viewDidLoad {
     mapView=[[MKMapView alloc]initWithFrame:CGRectMake( 10100300100)];
     mapView.scrollEnabled=NO;
     [mapView setDelegate:self];
      // 設置地圖中心
     CLLocationCoordinate2D coordinate;
     coordinate.latitude =  23.134844f;
     coordinate.longitude =  113.317290f;
     MKPointAnnotation *ann = [[MKPointAnnotation alloc] init];
     ann.coordinate = coordinate;
     [ann setTitle: @" 天河城 "];
     [ann setSubtitle: @" 購物好去處 "];
      // 觸發viewForAnnotation
     [mapView addAnnotation:ann];
       // 添加多個
     
// [mapView addAnnotations]
    
     
     
     
     
// 設置顯示范圍
     MKCoordinateRegion region;
     region.span.latitudeDelta =  0.001;
     region.span.longitudeDelta =  0.001;
     region.center = coordinate;
      //  設置顯示位置(動畫)
     [mapView setRegion:region animated:YES];
      //  設置地圖顯示的類型及根據范圍進行顯示
     [mapView regionThatFits:region];
    

     [self.view addSubview:mapView];
     
     btn=[[UIButton alloc]initWithFrame:CGRectMake( 10100300100)];
     [btn setTag: 1];
     [btn setBackgroundColor:[UIColor clearColor]];
     [btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
     [self.view addSubview:btn];
 
     [super viewDidLoad];
 }
- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:( id <MKAnnotation>)annotation
{
    MKPinAnnotationView *pinView = nil;
    
     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;
     return pinView;
}
-( void)btnPressed:( id)sender
{
    [sender setHidden:YES];
    [self.mapView setFrame:CGRectMake( 00320480)];
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM