2011-08-22 62 views
16

如何在地圖中顯示圖像而不是針腳。到目前爲止,我只能添加引腳。 .m的示例代碼將非常有幫助,因爲我還是iOS編程的新手。iOS MapKit自定義針腳

+1

一點點搜索走一段很長的路要走:) http://stackoverflow.com/questions/4579397/iphone-mapkit-adding-custom-image-and-pins-to-annotations也:HTTP:/ /stackoverflow.com/questions/7124028/change-pin-design –

回答

53
#pragma mark - 
#pragma mark MKMapView delegate 
- (MKAnnotationView *)mapView:(MKMapView *)mapview viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    if ([annotation isKindOfClass:[MKUserLocation class]]) 
     return nil; 
    static NSString* AnnotationIdentifier = @"AnnotationIdentifier"; 
    MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier]; 
    if(annotationView) 
     return annotationView; 
    else 
    { 
     MKAnnotationView *annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation 
                     reuseIdentifier:AnnotationIdentifier] autorelease]; 
     annotationView.canShowCallout = YES; 
     annotationView.image = [UIImage imageNamed:@"someImage.png"]; 
     UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
     [rightButton addTarget:self action:@selector(writeSomething:) forControlEvents:UIControlEventTouchUpInside]; 
     [rightButton setTitle:annotation.title forState:UIControlStateNormal]; 
     annotationView.rightCalloutAccessoryView = rightButton; 
     annotationView.canShowCallout = YES; 
     annotationView.draggable = YES; 
     return annotationView; 
    } 
    return nil; 
} 

編輯:

我可以向大家解釋這MKAnnotationView,但我想你會發現蘋果提供比任何其他來源一個更好的解釋文件。檢查鏈接中的概述部分。

https://developer.apple.com/documentation/mapkit/mkannotationview

+0

它的工作原理。謝謝。能否請您簡要說明傳遞的參數以及註釋標識符是什麼?與.net的標籤屬性類似嗎? – Bahamut

+0

在我的需求中,我不是使用「someImage.png」,而是使用UIView並使用[annotationView addSubView:myView]。它的工作正常。但不要在它的水龍頭上調用「didSelectAnnotationView」。哪裏不對了? – Satyam

+0

可能需要禁用該視圖的用戶交互。 – Robin

6

轉到組織者的Xcode,然後去到文檔和搜索weatherMap它顯示地圖包括註釋圖像的例子。

0
#pragma mark - 
#pragma mark MKMapView delegate 
-(void)addAllPinsOnMapView 
{ 



MKCoordinateRegion region = mapViewOffer.region; 
region.center = CLLocationCoordinate2DMake(12.9752297537231, 80.2313079833984); 
region.span.longitudeDelta= 0.1f; 
region.span.latitudeDelta= 0.1f; 
[mapViewOffer setRegion:region animated:YES]; 






mapViewOffer.delegate=self; 
arrMapPin=[[NSMutableArray alloc] init]; 
NSArray *name=[[NSArray alloc]initWithObjects: 
       @"Title1", 
       @"Title2", 
       @"Title3", nil]; 

NSMutableArray *arrCoordinateStr = [[NSMutableArray alloc] initWithCapacity:name.count]; 
[arrCoordinateStr addObject:@"12.970760345459,80.2190093994141"]; 
[arrCoordinateStr addObject:@"12.9752297537231,80.2313079833984"]; 
[arrCoordinateStr addObject:@"12.9788103103638,80.2412414550781"]; 

for(int i = 0; i < name.count; i++) 
{ 
    NSArray *components = [[arrCoordinateStr objectAtIndex:i] componentsSeparatedByString:@","]; 
    double latitude = [components[0] doubleValue]; 
    double longitude = [components[1] doubleValue]; 

    MKPointAnnotation *mapPin = [[MKPointAnnotation alloc] init]; 
    CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude); 
    mapPin.title = [name objectAtIndex:i]; 
    mapPin.coordinate = coordinate; 
    [mapViewOffer addAnnotation:mapPin]; 
} 
} 
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView: 
(MKAnnotationView *)view 
{ 
NSLog(@"%@",view.annotation.title); 
NSLog(@"%f",view.annotation.coordinate.latitude); 
NSLog(@"%f",view.annotation.coordinate.longitude); 

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(calloutTapped:)]; 
[view addGestureRecognizer:tapGesture]; 

} 
-(void)calloutTapped:(UITapGestureRecognizer *) sender 
{ 
NSLog(@"Callout was tapped"); 

MKAnnotationView *view = (MKAnnotationView*)sender.view; 
id <MKAnnotation> annotation = [view annotation]; 
if ([annotation isKindOfClass:[MKPointAnnotation class]]) 
{ 
    //[self performSegueWithIdentifier:@"annotationDetailSegue" sender:annotation]; 
    OfferDetailsViewController *objOfferDetailsViewController = [[OfferDetailsViewController alloc]init]; 
    [self.navigationController pushViewController:objOfferDetailsViewController animated:YES]; 
} 
} 
- (MKAnnotationView *)mapView:(MKMapView *)theMapView 
viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
MKAnnotationView *pinView = nil; 
static NSString *defaultPinID = @"annotationViewID"; 
pinView = (MKAnnotationView *)[mapViewOffer dequeueReusableAnnotationViewWithIdentifier:defaultPinID]; 
if (pinView == nil){ 
    pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID]; 
} 

pinView.canShowCallout = YES; 
pinView.image = [UIImage imageNamed:@"placeholder"]; 


UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
[infoButton addTarget:self action:@selector(infoButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
pinView.rightCalloutAccessoryView = infoButton; 

return pinView; 
} 
+0

在發佈長代碼片段之前添加一些說明。 –