将用户注释更改回mapview中的默认蓝点

时间:2014-04-08 03:00:37

标签: ios objective-c annotations mapkit

我在mapview上创建了注释但是我希望我的用户标记返回一个闪烁的蓝点而不是绿色引脚。我似乎无法弄清楚如何用闪烁的蓝点更改原始用户标记。这是我的代码。

#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

1 个答案:

答案 0 :(得分:0)

您可以通过viewForAnnotation中的MKMapViewDelegate方法实现此目的。如果此方法返回nil,则地图视图将使用默认注释视图。您可以测试注释以查看它是否是您的子类,然后根据需要返回视图或nil。像 -

这样的东西
- (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;
}