0

我主要是一名 JAVA 程序员,但承担了 iPhone 的总线跟踪器项目,所以我对此还不是很满意。请原谅任何愚蠢的错误。以下代码是我得到引脚工作时的情况。我为尝试显示图像所做的任何更改都已被删除。

#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

那么,我可以获得此代码以显示自定义图钉还是需要返工?如何?

4

1 回答 1

0

您可以在显示和选择自定义图像时直接使用 didAddAnnotationViews 方法和 didSelectAnnotation。我发现此链接可以最好地帮助您如何将自定义图钉添加到 iPhone MapKit? 希望这可以帮助。

于 2013-07-27T14:14:13.013 回答