我需要显示带有标签的UiView On MKAnnotation我能够显示[参考MKAnnotationView创建的像这样]但我通过调用locationsUpdateAgain方法再次刷新应用程序,
启动App MKAnnotationView工作正常只创建一个BUT当我再次调用[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];
}
- (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){
}];
#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;
}
请参阅截图
答案 0 :(得分:0)
问题:
当我调用[locationManager startUpdatingLocation]时,它会创建两个MKAnnotationViews,即使我删除了所有这样的注释
[_mapView removeAnnotations:(_mapView.annotations)];
解:
所以我添加了代码以在DidUpdateLocations委托方法
中添加UserAnnotation之前再次删除现有的userAnnotationfor (id <MKAnnotation> myAnnot in [_mapView annotations])
{
if ([myAnnot isKindOfClass:[UserAnnotation class]])
{
[_mapView removeAnnotation:myAnnot];
}
}
DidUpdateLocations委托方法:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
[locationManager stopUpdatingLocation];
userCurrentLocation = locations.lastObject;
CLLocationCoordinate2D currentLocationCoordinate = [userCurrentLocation coordinate];
for (id <MKAnnotation> myAnnot in [_mapView annotations])
{
if ([myAnnot isKindOfClass:[UserAnnotation class]])
{
[_mapView removeAnnotation:myAnnot];
}
}
UserAnnotation *userAnnot = [UserAnnotation new];
userAnnot.coordinate = currentLocationCoordinate;
[self.mapView addAnnotation: userAnnot];
}