問題描述
我想在地圖中的注釋中添加自定義圖像.我已經制作了以下自定義 MapAnnotationView:
I want to add a custom image to my annotations in the map. And i have made the following custom MapAnnotationView:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@class POI;
@interface MapAnnotation : MKAnnotationView <MKAnnotation >
@property (nonatomic) CGFloat lat;
@property (nonatomic) CGFloat lon;
@property (nonatomic) CGFloat altitude;
@property (nonatomic, copy) NSString * title;
@property (nonatomic, copy) NSString * subtitle;
@property (nonatomic,retain) NSString *source;
@property (nonatomic,retain) UIImage *image;
@end
@implementation MapAnnotation
@synthesize coordinate;
@synthesize lat=_lat,lon=_lon,altitude= _altitude;
@synthesize subtitle= _subtitle, title= _title, source=_source, image =_img;
- (CLLocationCoordinate2D)coordinate;{
CLLocationCoordinate2D position;
if (_lat != 0.0 && _lon != 0.0) {
position.latitude = _lat;
position.longitude = _lon;
}else {
position.latitude=0.0;
position.longitude=0.0;
}
return position;
}
@end
-(void) mapDataToMapAnnotations{
NSMutableArray *toRemove = [NSMutableArray arrayWithCapacity:10];
for (id annotation in _map.annotations)
if (annotation != _map.userLocation)
[toRemove addObject:annotation];
[_map removeAnnotations:toRemove];
[_data removeAllObjects];
[_data addObjectsFromArray:[UDdelegate naturArray]];
if(_data != nil){
MapAnnotation * tmpPlace;
//for(NSDictionary * poi in _data){
for(POI* poi in _data){
tmpPlace = [[MapAnnotation alloc]init];
tmpPlace.title = [poi title];
tmpPlace.lat = [poi lat];
tmpPlace.lon = [poi lon];
tmpPlace.subtitle = [poi dist];
tmpPlace.image = [poi poiIcon];
[self.map addAnnotation:tmpPlace];
[_map setNeedsLayout];
}
}
}
問題是引腳是標準的 redPin.... 我確定圖標不為空,已經檢查過了.
The problem is that the pins is the standard redPin.... I am sure that the icons isn't null, have checked for that.
謝謝
推薦答案
您必須為 MapKit 委托方法 mapView:viewForAnnotation:
提供自定義視圖.
You have to serve the MapKit delegate method mapView:viewForAnnotation:
with a custom view.
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString *annotationViewReuseIdentifier = @"annotationViewReuseIdentifier";
MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationViewReuseIdentifier];
if (annotationView == nil)
{
annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationViewReuseIdentifier] autorelease];
}
annotationView.image = [UIImage imageNamed:@"pin_image.png"];
annotationView.annotation = annotation;
return annotationView;
}
要封裝更多內容,您應該像以前一樣創建一個自定義注釋視圖,并將上面的委托方法與您的類一起提供.
To encapsulate more you should create a custom annotation view like you did and serve the delegate method above with your class.
我建議您重命名 MapAnnotation
類,因為它會造成混淆.iOS 中還有注釋,它們是這些注釋視圖的數據持有者.為了解決這個問題,我更喜歡編寫繼承類的類型,在這種情況下,MKAnnotationView
在自定義類的末尾.例如 CustomPinAnnotationView
.
I advise you to rename the MapAnnotation
class because it is confusing. There are also Annotations in iOS which are the data holders for those annotation views. To solve this I would prefer to write the type of the inherited class, in this case MKAnnotationView
at the end of your custom class. For example CustomPinAnnotationView
.
這篇關于IOS:將圖像添加到自定義 MKAnnotationview的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!