Mapkit中的自定义引脚

时间:2013-07-27 13:57:21

标签: iphone ios objective-c mkmapview mkannotation

我主要是一名JAVA程序员,但为iPhone开了一个公交车跟踪器项目,所以我对此不太满意。请原谅任何noobish错误。下面的代码就是当我得到销钉工作时的情况。我尝试显示图像所做的任何更改都已删除。

#import "UICBus_FirstViewController.h"

@interface UICBus_FirstViewController ()

@end

@implementation UICBus_FirstViewController
@synthesize timer;

- (void)viewDidLoad
{
    [super viewDidLoad];

    CLLocationCoordinate2D zoomLocation;
    zoomLocation.latitude = 41.869271;
    zoomLocation.longitude= -87.666436;
    // 2
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.85*METERS_PER_MILE, 0.85*METERS_PER_MILE);
    // 3
    MKCoordinateRegion adjustedRegion = [__mapView regionThatFits:viewRegion];
    // 4
    [__mapView setRegion:adjustedRegion animated:YES];

    CGRect frame = self.view.bounds;
    frame.size.height = frame.size.height - 40;

    // Do any additional setup after loading the view, typically from a nib.
    [self initRoutes];
    timer = [NSTimer scheduledTimerWithTimeInterval: 5 target:self selector:@selector(updateLocations) userInfo:nil repeats: YES];
}

- (void)viewWillAppear:(BOOL)animated {
}

- (void)updateLocations {
    [self updateBuses];
    [self._mapView removeAnnotations:_Anns];
    _Anns = [[NSMutableArray alloc] init];
    for (int i = 0; i < _Buses.count; i++){
        UICBus_Bus *Temp = _Buses[i];
        CLLocationCoordinate2D ctrpoint;
        ctrpoint.latitude = [Temp.getLat floatValue];
        ctrpoint.longitude = [Temp.getLon floatValue];
        MKAnnotation *addAnnotation = [[MKAnnotation alloc] initWithTitle:Temp.getMac andCoordinate:ctrpoint];
        [_Anns addObject:addAnnotation];
    }
    [self._mapView addAnnotations:_Anns];
}


// ********************** Still need to add code to determine if bus is active or not **********************

- (void)updateBuses {
    NSURL *url = [NSURL URLWithString:@"http://bus.uic.edu/api/latest"];
    NSData *content = [NSData dataWithContentsOfURL:url];
    NSError *JSONe;
    _BusesInput = [NSJSONSerialization JSONObjectWithData:content options:NSJSONReadingMutableContainers error:&JSONe];
    _Buses = [[NSMutableArray alloc] init];
    UICBus_Bus *Temp;

    if(_BusesInput.count < 1){
        NSLog(@"No Buses Found/Empty JSON");
    }else{
        for (NSString* key in _BusesInput){
            Temp = [[UICBus_Bus alloc] init:[_BusesInput objectForKey:key]];
            [_Buses addObject:Temp];
        }
    }


}

- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views{
    MKAnnotationView *annotationView = [views objectAtIndex:0];
    id <MKAnnotation> mp = [annotationView annotation];
    [mv selectAnnotation:mp animated:YES];

}

- (void)viewDidUnload {
    [self set_mapView:nil];
    [super viewDidUnload];
}
@end

那么,我可以将此代码显示为自定义引脚,还是需要重新编写?怎么样?

1 个答案:

答案 0 :(得分:0)

在显示和选择自定义图像时,您可以直接使用didAddAnnotationViews方法和didSelectAnnotation。  我发现此链接可以帮助您最好地使用How do I add custom pins to the iPhone MapKit? 希望这会有所帮助。