xcode 如何向地图视图添加多个颜色图钉。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13136934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add more than one color pin to a mapview.
提问by Will
I have a map that has 3 categories of points of interest. At the moment I have got them to display however they are all the same color. How can I get them to display a color depending to what category they are in?
我有一张地图,其中包含 3 类兴趣点。目前我已经让它们显示,但它们都是相同的颜色。我怎样才能让他们根据他们所在的类别显示颜色?
I have had a search around and couldn't find anything that I could understand to apply to my code.
我四处搜索,但找不到任何我能理解的适用于我的代码的内容。
Here is my current code for annotating the pins:
这是我当前用于注释引脚的代码:
-(void)pins:(NSString *)name lat:(NSString *)lat lon:(NSString *)lon poiID:(NSString *)poiID
{
//cast string to float
CGFloat Lat = (CGFloat)[lat floatValue];
CGFloat Lon = (CGFloat)[lon floatValue];
//Set coordinates of pin
CLLocationCoordinate2D coordinate;
coordinate.latitude = Lat;
coordinate.longitude = Lon;
//Create Pin
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
//set details
[annotation setCoordinate:coordinate];
[annotation setTitle:name];
[annotation setAccessibilityLabel:poiID]; //set the label eq to the id so we know which poi page to go to
// Add pin to map
[self.showMapView addAnnotation:annotation];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
//create annotation
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"pinView"];
if (!pinView) {
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pinView"];
pinView.pinColor = MKPinAnnotationColorRed;
pinView.animatesDrop = FALSE;
pinView.canShowCallout = YES;
//details button
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pinView.rightCalloutAccessoryView = rightButton;
} else {
pinView.annotation = annotation;
}
return pinView;
}
Here is the rest of my Codeincase it is needed.
这是我的代码的其余部分,以防万一。
Thanks for any help.
谢谢你的帮助。
回答by AdamM
Simple enough to do, just add an if statement to check the title or subtitle. When I was using maps, my annotation class had a subtitle where it would say what the place was, i.e. hotel, airport etc. I would just add an if statement to check, and then set what the pin point was and set up what colour I wanted the pin to be
做起来很简单,只需添加一个 if 语句来检查标题或副标题。当我使用地图时,我的注释类有一个副标题,它会说明地点是什么,即酒店、机场等。我只需添加一个 if 语句来检查,然后设置针点是什么并设置什么颜色我希望别针是
if([annotation.subtitle isEqualToString:@"Hotel"])
{
pinView.animatesDrop=YES;
pinView.pinColor=MKPinAnnotationColorPurple;
}else if([annotation.subtitle isEqualToString:@"Airport"])
{
pinView.animatesDrop=YES;
pinView.pinColor=MKPinAnnotationColorRed;
}
or if you want it to be an image, just use
或者如果你希望它是一个图像,只需使用
pinView.image = [UIImage imageNamed:@"airportMarker.png"];
EDIT: UPDATED CODE
编辑:更新的代码
Create a custom annotation class like this
创建一个像这样的自定义注释类
@interface MyAnnotation : NSObject<MKAnnotation>
{
CLLocationCoordinate2D coordinate;
NSString* title;
NSString* subtitle;
NSInteger categoryID;
}
@property NSInteger categoryID;
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString* title;
@property (nonatomic, copy) NSString* subtitle;
@end
Then when you are adding the objects to screen, give each one an ID, i.e
然后当你将对象添加到屏幕时,给每个对象一个 ID,即
CLLocationCoordinate2D hotelCoordinates1;
hotelCoordinates1.latitude = 35.38355;
hotelCoordinates1.longitude = 1.11004;
MyAnnotation* exampleHotel=[[MyAnnotation alloc] init];
exampleHotel.coordinate=hotelCoordinates1;
exampleHotel.title=@"Example Hotel";
exampleHotel.subtitle=@"Hotel";
exampleHotel.categoryID = 1;
Then inside this method
然后在这个方法里面
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
//create annotation
just do this
就这样做
if([(MyAnnotation*)annotation categoryID] == 1)
{
}
You need to ensure you cast the annotation to whatever the name of the class you created. And thats it. Hope that helps!!
您需要确保将注释转换为您创建的类的任何名称。就是这样。希望有帮助!!
回答by Paras Joshi
- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString *AnnotationViewID = @"annotationViewID";
MKAnnotationView *annotationView = (MKAnnotationView *)[MymapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if (annotationView == nil)
{
annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
}
if (annotation == MymapView.userLocation)
return nil;
/////i give just an example with category
if(category == 1){
annotationView.image = [UIImage imageNamed:@"Category1.png"];
}
else if(category == 2){
annotationView.image = [UIImage imageNamed:@"Category2.png"];
}
annotationView.annotation = annotation;
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annotationView.rightCalloutAccessoryView.tag = 101;
annotationView.canShowCallout = YES;
return annotationView;
}
回答by edzio27
You have to override mkAnnotation class and define specyfic field where you declare categories id. When you create Annotation on map set the id, and on the delegate viewForAnnotation check the id annotation, and check which color you have to use for pin for current id.
您必须覆盖 mkAnnotation 类并定义您声明类别 ID 的特定字段。当您在地图上创建注释时设置 id,并在委托 viewForAnnotation 检查 id 注释,并检查您必须使用哪种颜色作为当前 id 的 pin。
You can also see this post to see how to ovveride MKAnnotation class: MKAnnotation
您还可以查看此帖子以了解如何覆盖 MKAnnotation 类:MKAnnotation