2013-07-27 52 views
0

我主要是一位JAVA程序員,但是擔任了iPhone的總線跟蹤器項目,所以我對此還不是很滿意。請原諒任何noobish錯誤。下面的代碼是當我得到引腳工作時的情況。我嘗試顯示圖像的任何更改都已被刪除。Mapkit中的自定義引腳

#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 

那麼,我可以得到這個代碼來顯示一個自定義引腳,或者我需要返工嗎?如何?

+0

您查看過的解決方案是否實現了viewForAnnotation委託方法並設置視圖的圖像屬性?如果你嘗試過,並且它不起作用,請顯示該代碼並解釋發生或未發生的事情。 – Anna

+0

一個無關的觀點:看起來你已經爲你的註解對象創建了一個名爲'MKAnnotation'的自定義類(代碼爲'MKAnnotation * addAnnotation ...')。 ['MKAnnotation'也是MapKit註解協議的名稱](http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKAnnotation_Protocol/Reference/Reference.html)。我不建議爲您的自定義類使用相同的名稱。 – Anna

回答

0

您可以直接使用didAddAnnotationViews方法和didSelectAnnotation在顯示和選擇自定義圖像時使用resp。 我覺得這個鏈接可以幫助您更好地使用How do I add custom pins to the iPhone MapKit? 希望這會有所幫助。