1

I need to show the UiView with label On MKAnnotation I am able to show[refer MKAnnotationView created Like this] but I am refreshing the App again by calling locationsUpdateAgain Method,

Starting of App MKAnnotationView works fine creates only one BUT When I call again [locationManager startUpdatingLocation] the MKAnnotationView creates 2 Views shows like in SECOND(2nd) screenshot

Please tell me how to get rid of this...........

locationsUpdateAgain:

-(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 added like this

  ///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];

}

Moving Annotation:when i move mapview userLocationAnnotation[MKAnnotation] Moves but two MKAnnotationViews were created

- (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 Class created Like this:

 #import <MapKit/MapKit.h>

@interface CustomMapAnnotationView : MKAnnotationView

#import "CustomMapAnnotationView.h"

 @implementation CustomMapAnnotationView
MKAnnotationView Delegate Method:
 -(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;

}

Please refer screenshot

enter image description here enter image description here

Sanju
  • 1,148
  • 11
  • 26

1 Answers1

0

Heyy I solved it ......

Problem:

When I call [locationManager startUpdatingLocation] it creating two MKAnnotationViews even though i removed all annotations like this

 [_mapView removeAnnotations:(_mapView.annotations)];
Solution:

So I added code to remove existing userAnnotation again before adding UserAnnotation in DidUpdateLocations delegate Method

for (id <MKAnnotation>  myAnnot in [_mapView annotations])
{
if ([myAnnot isKindOfClass:[UserAnnotation class]])
{
    [_mapView removeAnnotation:myAnnot];
}
} 

DidUpdateLocations delegate Method:

- (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];

}
Sanju
  • 1,148
  • 11
  • 26