Question

I have created annotations on a mapview however i would like my user marker to return a flashing blue dot instead of a green pin. I cannot seem to figure out how to change the original user marker with the flashing blue dot. Here is my code.

#import "ViewController.h"
#import "DetailController.h"
#import "Annotation.h"

#import "City.h"

@interface ViewController (){
MKLocalSearch *localSearch;
MKLocalSearchResponse *results;
}
@property (nonatomic, strong) IBOutlet DetailController *detailViewController;


@end
#define getDatalURL @"http://www.club-hop.com/apptest.php"

@implementation ViewController

@synthesize mapView,jsonArray,citiesArray;
- (void)viewDidLoad
{
[super viewDidLoad];
[self retrieveData];
self.detailViewController = [[DetailController alloc] init];
[self.searchDisplayController setDelegate:self];
[self.ibSearchBar setDelegate:self];
//Zoom the map to current location.
[self.mapView setShowsUserLocation:YES];
[self.mapView setUserInteractionEnabled:YES];
[self.mapView setUserTrackingMode:MKUserTrackingModeFollow];

City * cityObject;

// load external page into UIWebView
NSMutableArray * locations= [[NSMutableArray alloc]init];
CLLocationCoordinate2D location;
Annotation * myAnn;

for(int u=0; u<citiesArray.count;u++){
    cityObject=[citiesArray objectAtIndex:u];

    myAnn=[[Annotation alloc]init];

    myAnn.city=cityObject;     // Store the city object on the annotation

    NSNumber *aLat= cityObject.Latitude;
    NSNumber *aLon= cityObject.Longitude;

    double lat = [aLat doubleValue];
    double lon = [aLon doubleValue];

    location.latitude= lat;
    location.longitude=lon;
    myAnn.coordinate = location;
    myAnn.title=cityObject.clubName;
    myAnn.subtitle=cityObject.cityName;

    [locations addObject:myAnn];}

[self.mapView addAnnotations:locations];


}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}



//class methods
-(void) retrieveData{
NSURL * url= [NSURL URLWithString:getDatalURL];
NSData * data= [NSData dataWithContentsOfURL:url];
jsonArray= [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

//setup cities array
citiesArray=[[NSMutableArray alloc]init];

for(int i=0; i<jsonArray.count;i++){
    NSString * cID= [[jsonArray objectAtIndex:i] objectForKey:@"id"];
    NSString * cName= [[jsonArray objectAtIndex:i] objectForKey:@"cityName"];
    NSString * cCountry= [[jsonArray objectAtIndex:i] objectForKey:@"cityCountry"];
    NSString * cLine= [[jsonArray objectAtIndex:i] objectForKey:@"clubLine"];
    NSString * pri=[[jsonArray objectAtIndex:i] objectForKey:@"price"];
    NSString * promo=[[jsonArray objectAtIndex:i] objectForKey:@"promo"];
    NSString * clName= [[jsonArray objectAtIndex:i] objectForKey:@"clubName"];
    NSNumber * cLatitude= [[jsonArray objectAtIndex:i] objectForKey:@"Latitude"];
    NSNumber * cLongitude= [[jsonArray objectAtIndex:i] objectForKey:@"Longitude"];

    [citiesArray addObject:[[City alloc]initWithCityName:cName andCityCountry:cCountry andClubName:clName andClubLine:cLine andPrice:pri andPromo:promo andLatitude:cLatitude   andLongitude:cLongitude andCityId:cID]];

}

}


- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view         calloutAccessoryControlTapped:(UIControl *)control

{   

UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
                                                     bundle:nil];
self.detailViewController = [storyboard instantiateViewControllerWithIdentifier:@"Page2"];

Annotation *myAnnotation=(Annotation *)view.annotation;


self.detailViewController.city=myAnnotation.city;

[self.navigationController pushViewController:self.detailViewController animated:YES];


}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id   <MKAnnotation>)annotation
{
MKAnnotationView *pin=nil;
if ([annotation isKindOfClass:[Annotation class]])
{
    pin=(MKAnnotationView *)[mapView   dequeueReusableAnnotationViewWithIdentifier:@"myAnnotation"];
    if (pin == nil)
    {
        pin=[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
        pin.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        pin.image=[UIImage imageNamed:@"mappin.png"];
        pin.centerOffset=CGPointMake(0.0, pin.image.size.height/-2);
        pin.canShowCallout=YES;
    }
 }
 return pin;
}

@end
Was it helpful?

Solution

You can achieve this through your viewForAnnotation method in your MKMapViewDelegate. If this method returns nil then the map view will use the default annotation view. You can test the annotation to see if it is your subclass and then return a view or nil as appropriate. Something like -

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKAnnotationView *pin=nil;
    if ([annotation isKindOfClass:[Annotation class]])
    {
        pin=(MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"myAnnotation"];
        if (pin == nil)
        {
            pin=[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
            pin.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            pin.image=[UIImage imageNamed:@"mappin.png"];
            pin.centerOffset=CGPointMake(0.0, pin.image.size.height/-2);
            pin.canShowCallout=YES;   
        }
    }
    return pin;
} 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top