iOS- MKMapKit - 如何在用户位置删除注释?

时间:2012-08-12 00:23:32

标签: ios annotations mkmapview

我的应用上有用户位置,但如何在当前用户位置删除注释?我是否必须获得长期& lat的用户位置和删除注释的方式?或者我该怎么做?

2 个答案:

答案 0 :(得分:2)

  1. 首先导入必要的框架(CoreLocation和MapKit)。
  2. 然后创建Objective-C NSObject类Annotation
  3. 设置其.h:

    #import <Foundation/Foundation.h>
    
    #import <CoreLocation/CoreLocation.h>
    
    #import <MapKit/MapKit.h>
    
    @interface Annotation : NSObject <MKAnnotation> 
    
    @property (nonatomic) CLLocationCoordinate2D coordinate;
    @property (nonatomic, copy) NSString *title;
    @property (nonatomic, copy) NSString *subtitle;
    
    @end
    
  4. 设置其.m:

    #import "Annotation.h"
    
    @implementation Annotation
    @synthesize coordinate, title, subtitle;
    
    @end
    
    1. 设置viewDidLoad

         if ([CLLocationManager locationServicesEnabled]) {
      
         locationManager = [[CLLocationManager alloc] init];
      
         [locationManager setDelegate:self];
      
         [locationManager setDesiredAccuracy: kCLLocationAccuracyBestForNavigation];
      
         [locationManager startUpdatingLocation];
      
         }
      
        self.mapView.delegate = self; 
      
    2. 设置didUpdateToLocation

         // IMPORT ANNOTATION
      
         - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
      
      
         [locationManager stopUpdatingLocation];
      
         double miles = 3.10686;
         double scalingFactor = ABS((cos(2 * M_PI * newLocation.coordinate.latitude / 360.0)));
      
         MKCoordinateSpan span;
      
         span.latitudeDelta = miles/69.0;
         span.longitudeDelta = miles/(scalingFactor * 69.0);
      
         MKCoordinateRegion region;
         region.span = span;
         region.center = newLocation.coordinate;
      
         [self.mapView setRegion:region animated:YES];
      
         Annotation *annot = [[Annotation alloc] init];
         annot.coordinate = newLocation.coordinate;
      
         [self.mapView addAnnotation:annot];
      
           }
      

答案 1 :(得分:0)

最简单的方法是在showsUserLocation上将YES设置为MKMapView并实施

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
    MKCoordinateRegion region = MKCoordinateRegionMake(userLocation.location.coordinate, MKCoordinateSpanMake(0.01, 0.01));
    [mapView setRegion:region animated:NO];
}
在您的MKMapViewDelegate

,以便在找到用户的位置后将地图视图移动到该位置。

这将在地图视图中的地图视图中显示一个蓝点,就像地图应用程序一样。