我需要在 MKAnnotation 上显示带有标签的 UiView 我能够显示[请参考 MKAnnotationView created Like this] 但我通过调用 locationsUpdateAgain 方法再次刷新应用程序,
应用程序 MKAnnotationView 的启动工作正常只创建一个但是当我再次调用 [locationManager startUpdatingLocation] 时,MKAnnotationView 创建 2 个视图显示,如 SECOND(2nd) 屏幕截图
请告诉我如何摆脱这个............
再次更新位置:
-(void)locationsUpdateAgain{
if(locationManager==nil)
{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
//locationManager.pausesLocationUpdatesAutomatically=NO;
NSString *version = [[UIDevice currentDevice] systemVersion];
if([version floatValue] > 7.0f)
{
if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
{
[locationManager requestWhenInUseAuthorization];
}
}
}
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
MKAnnotations 像这样添加
///USER ANNOTATION
Annotation*UserLocationAnnotion = [[Annotation alloc]init];
UserLocationAnnotion.coordinate = currentLocationCoordinate;
//point2.locationType = @"user_location";
[self.mapView addAnnotation:UserLocationAnnotion];
///CARTS ANNOTATIONS
for (int i = 0; i<[cartsNearbyArray count]; i++) {
VendorsObjects *vendor=[cartsNearbyArray objectAtIndex:i];
CartAnnotation * cartAnn = [[CartAnnotation alloc]init];
cartAnn.coordinate = vendorCoord;
[self.mapView addAnnotation:cartAnn];
}
移动注释:当我移动 mapview userLocationAnnotation[MKAnnotation] 移动但创建了两个 MKAnnotationViews
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
NSLog(@"region changed");
NSLog(@"%f %f",mapView.centerCoordinate.latitude,mapView.centerCoordinate.longitude);
CLLocationCoordinate2D cord = mapView.centerCoordinate;
[UIView animateWithDuration:1.0f
animations:^(void){
//[point2 setCoordinate:cord];
UserLocationAnnotion.coordinate = cord;
}
completion:^(BOOL finished){
}];
MKAnnotationView 类创建如下:
#import <MapKit/MapKit.h>
@interface CustomMapAnnotationView : MKAnnotationView
#import "CustomMapAnnotationView.h"
@implementation CustomMapAnnotationView
MKAnnotationView 委托方法:
-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
static NSString *reuseId = @"CustomMapPin";
CustomMapAnnotationView *userAnnotationView = nil;
userAnnotationView = (CustomMapAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
Annotation *userAnnotation = (Annotation *)annotation;//user location annotation
CartAnnotation *cartAnnotation = (CartAnnotation *)annotation;//carts surrounding annotation
if ([cartAnnotation isKindOfClass:[CartAnnotation class]])
{
MKAnnotationView *cartAnnotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];
[cartAnnotationView setImage:[UIImage imageNamed:@"vegcart.png"]];
cartAnnotationView.annotation = cartAnnotation;
return cartAnnotationView;
}
else if([userAnnotation isKindOfClass:[Annotation class]])
{
if (userAnnotationView == nil )
userAnnotationView = [[CustomMapAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];
[userAnnotationView setImage:[UIImage imageNamed:@"location_marker.png"]];
userAnnotationView.centerOffset = CGPointMake(0, -userAnnotationView.frame.size.height/2 );
UILabel* category = [[UILabel alloc] initWithFrame:CGRectMake(10, 15, 200, 20)];
category.text = @"for cart arrival";
customView = [[UIView alloc]init];
container = [[UIView alloc] init] ;
customView.frame = CGRectMake(-30, -45, 140, 45);
customView.layer.masksToBounds = YES;
customView.layer.cornerRadius = 27;
customView.backgroundColor = [UIColor colorWithRed:1.0f green:0.41f blue:0.003f alpha:1.0f];
[customView addSubview:category];
container.frame =CGRectMake(-75, -45, 45, 45);
travelTimeLabel.frame = CGRectMake(2, 0, 45, 45) ;
travelTimeLabel.font = [UIFont boldSystemFontOfSize:12];
[container addSubview:travelTimeLabel];
[container setBackgroundColor:[UIColor colorWithRed:1.0f green:0.41f blue:0.003f alpha:1.0f]];
container.layer.cornerRadius = 22.5;
[userAnnotationView addSubview:customView];
[userAnnotationView addSubview:container];
return userAnnotationView;
}
else
return nil;
}
请参考截图