简体   繁体   中英

Custom Pin in Mapkit

I am primarily a JAVA programmer but took on a bus tracker project for the iPhone, so I am not very comfortable with this yet. Please forgive any noobish mistakes. The following code is how it was when I got pins to work. Any changes I made to attempt showing images has been removed.

#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

So, can I get this code to display a custom pin or do I need to rework it? and how?

You can directly work with didAddAnnotationViews method and didSelectAnnotation possibly while displaying and selecting custom image resp. I found this link to help you best with How do I add custom pins to the iPhone MapKit? Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM