2012-10-31 18 views
1

我想在MKMapview中添加一個UIImage。如何在iPhone中的MKMapView中添加UIImage?

即:一個特定的位置,我想添加一個名爲「apple.jpg」的圖像。

我不知道該怎麼做。

因爲我已經添加了一個可拖動的引腳作爲註釋。但我不知道我是否可以添加多個圖像。

+1

我猜你是問如何做一個自定義的MKAnnotationView,是這樣嗎? – Kassem

+0

不,我想簡單地添加一個圖像而不是註釋。即我想在給定的位置添加蘋果的圖片。 – Hemang

+1

MapKit中的任何地理定位事物都是一個Annotation,它的可視化表示形式是MKAnnotationView – whiteagle

回答

5

對於添加圖像的MKMapView的部分請參見本教程...

i.ndigo mkmapview

和添加圖片作爲引腳上的MapView然後使用該婁代碼...

MKAnnotationView *annotationView = [[[MKAnnotationView alloc] init]; 

annotationView.image = [UIImage imageNamed:@"apple.png"]; 
annotationView.annotation = annotation; 
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
annotationView.rightCalloutAccessoryView.tag = 101; 

    annotationView.canShowCallout = YES; 
[yourMapView addAnnotation:annotationView]; 
+0

我試過這段代碼。但一旦我捏/放大或縮小圖像保持不變。我想讓圖像成爲地圖的一部分。 – Hemang

+0

表示您不想將圖像視爲pinview而是作爲MapView中的位置? –

+0

是的。 100%正確的Paras。我沒有得到任何代碼或幫助,甚至沒有API來做到這一點。 – Hemang

2

as @Kassem Bagher提到,您需要創建自定義MKAnnotationView,查看任何教程(example

+1

我希望這個例子能夠正常工作,因爲我已經在附加示例中找到了一些相關的代碼。 – Hemang

1

您的圖片必須爲b e png格式請參閱下面的代碼。

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation 
{ 

MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"pinView"]; 
if (!pinView) { 
    pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pinView"] autorelease]; 
    pinView.image = [UIImage imageNamed:@"apple.png"]; 
    pinView.canShowCallout = YES; 
} 
} 
+0

我試過這段代碼。但一旦我捏/放大或縮小圖像保持不變。我想讓圖像成爲地圖的一部分。 – Hemang

+0

我無法得到你。你使用哪種圖像?你在地圖上捏/放大? –

+0

我想添加一張圖片作爲地圖的一部分。與位置相同。 – Hemang

1

這是工作代碼,我已經與下面的代碼沒有練琴圖像png格式,.jpg或.gif的任何甲做,你可以用它

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
      MKPinAnnotationView *pinAnnotation = nil; 
      NSString *defaultPinID = @"myPin"; 
      pinAnnotation = (MKPinAnnotationView *)[mapview dequeueReusableAnnotationViewWithIdentifier:defaultPinID]; 
          if (pinAnnotation == nil) 
           pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease]; 

      pinAnnotation.image = [UIImage imageNamed:@"marker_postoffice.png"]; 
      pinAnnotation.annotation = annotation; 
      pinAnnotation.canShowCallout = YES; 
      UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 



      [infoButton addTarget:self action:@selector(showDetails)forControlEvents:UIControlEventTouchUpInside]; 
      pinAnnotation.rightCalloutAccessoryView = infoButton; 





     // now you can set diff image by [annotation title] you can set diff image with if else condition like below code 


     if([[annotation title] isEqualToString:objAppDelegate.OfficePinTitle]) 
     { 

         pinAnnotation.image = [UIImage imageNamed:@"marker_postoffice.png"]; 
         pinAnnotation.annotation = annotation; 
         pinAnnotation.canShowCallout = YES; 
         UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 

         infoButton addTarget:self 
                 action:@selector(showDetails)forControlEvents:UIControlEventTouchUpInside]; 

             pinAnnotation.rightCalloutAccessoryView = infoButton; 

     } 
     else 
     { 

         pinAnnotation.image = [UIImage imageNamed:@"marker_red_postoffice.png"]; 
         pinAnnotation.canShowCallout = YES; 
         UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 

         [infoButton addTarget:self action:@selector(showDetails)forControlEvents:UIControlEventTouchUpInside]; 

         pinAnnotation.rightCalloutAccessoryView = infoButton; 
     } 


} 
相關問題