我的应用程序上有用户位置,但是如何在当前用户位置上放置注释?我是否必须获取用户位置的经纬度并以这种方式删除注释?或者我该怎么做?
问问题
354 次
2 回答
2
- 首先导入必要的框架(CoreLocation 和 MapKit)。
- 然后创建Objective-C NSObject 类Annotation
设置它的.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
设置它的.m:
#import "Annotation.h" @implementation Annotation @synthesize coordinate, title, subtitle; @end
设置
viewDidLoad
if ([CLLocationManager locationServicesEnabled]) { locationManager = [[CLLocationManager alloc] init]; [locationManager setDelegate:self]; [locationManager setDesiredAccuracy: kCLLocationAccuracyBestForNavigation]; [locationManager startUpdatingLocation]; } self.mapView.delegate = self;
设置
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]; }
于 2012-08-12T01:07:08.950 回答
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
找到用户的位置后,让地图视图移动到该位置。
这将在用户位置的地图视图上显示一个蓝点,就像地图应用程序一样。
于 2012-08-12T02:51:37.497 回答