Iphone MKMap针颜色
我是Xcode新手,基本上我有一个Iphone MKMap针颜色,iphone,xcode,annotations,mkmapview,Iphone,Xcode,Annotations,Mkmapview,我是Xcode新手,基本上我有一个MKMapView,我想为我的每个管脚添加单独的颜色。我目前已经用注释标记了管脚 LocationAnnotation.h: @interface LocationAnnotation : NSObject <MKAnnotation> @property (nonatomic, assign) CLLocationCoordinate2D coordinate; @property (nonatomic, copy) NSString *titl
MKMapView
,我想为我的每个管脚添加单独的颜色。我目前已经用注释标记了管脚
LocationAnnotation.h:
@interface LocationAnnotation : NSObject <MKAnnotation>
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@end
我的每个坐标在
MainMapViewController.h:
#define TheKingBill_LATITUDE 50.431379
#define TheKingBill_LONGITUDE -3.685495
//Annotation
NSMutableArray * locations = [[NSMutableArray alloc] init];
CLLocationCoordinate2D location;
LocationAnnotation * myAnn;
//The King Bill Annotation
myAnn = [[LocationAnnotation alloc] init];
location.latitude = TheKingBill_LATITUDE;
location.longitude = TheKingBill_LONGITUDE;
myAnn.coordinate = location;
myAnn.title = @"The King Bill";
myAnn.subtitle = @"Another Pub In Town";
[locations addObject:myAnn];
//The Seven Stars Annotations
myAnn = [[LocationAnnotation alloc] init];
location.latitude = TheSevenStars_LATITUDE;
location.longitude = TheSevenStars_LONGITUDE;
myAnn.coordinate = location;
myAnn.title = @"The Royal Seven Stars Hotel";
myAnn.subtitle = @"Hotel In Town";
[locations addObject:myAnn];
[self.MainMapView addAnnotations:locations];
有什么想法我需要添加,使每个针的颜色不同
谢谢
好了,现在我的另一个地图链接到这个LocationAnnotation文件时出现了问题。我还想添加彩色别针到这个。如果新代码与我以前的mapviewcontroller相同,这将是理想的。这是代码 位置注释.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface LocationAnnotation : NSObject <MKAnnotation>
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property int idNumber;
- (id)initWithTitle:(NSString *)ttl andSubtitle:(NSString *)subttl andCoordinate:(CLLocationCoordinate2D)c2d andID:(int)idN;
@end
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface LocationAnnotation : NSObject <MKAnnotation>
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property int idNumber;
- (id)initWithTitle:(NSString *)ttl andSubtitle:(NSString *)subttl andCoordinate:(CLLocationCoordinate2D)c2d andID:(int)idN;
@end
MapViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface MapViewController : UIViewController
@property (weak, nonatomic) IBOutlet MKMapView *ClientMapView;
@end
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface MainMapViewController : UIViewController <MKMapViewDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *mainMapView;
@end
好的,这里是您的代码和修改: 位置注释.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface LocationAnnotation : NSObject <MKAnnotation>
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property int idNumber;
- (id)initWithTitle:(NSString *)ttl andSubtitle:(NSString *)subttl andCoordinate:(CLLocationCoordinate2D)c2d andID:(int)idN;
@end
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface LocationAnnotation : NSObject <MKAnnotation>
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property int idNumber;
- (id)initWithTitle:(NSString *)ttl andSubtitle:(NSString *)subttl andCoordinate:(CLLocationCoordinate2D)c2d andID:(int)idN;
@end
MainMapViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface MapViewController : UIViewController
@property (weak, nonatomic) IBOutlet MKMapView *ClientMapView;
@end
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface MainMapViewController : UIViewController <MKMapViewDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *mainMapView;
@end
#导入
#进口
@接口MainMapViewController:UIViewController
@属性(弱、非原子)IBMOutlet MKMapView*mainMapView;
@结束
mainmappviewcontroller.m
#import "MapViewController.h"
#import "LocationAnnotation.h"
@interface MapViewController ()
@end
//The Dartmouth Inn Coordinates
#define DARTMOUTH_INN_LATITUDE 50.430036;
#define DARTMOUTH_INN_LONGITUDE -3.683873;
//Span
#define THE_SPAN 0.004f;
@implementation MapViewController
@synthesize ClientMapView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//Create the region
MKCoordinateRegion myRegion;
//Centre
CLLocationCoordinate2D centre;
centre.latitude = DARTMOUTH_INN_LATITUDE;
centre.longitude = DARTMOUTH_INN_LONGITUDE;
//Span
MKCoordinateSpan span;
span.latitudeDelta = THE_SPAN;
span.longitudeDelta = THE_SPAN;
myRegion.center = centre;
myRegion.span = span;
//Set The Map View
[ClientMapView setRegion:myRegion animated:YES];
//Annotation
//1. Create A Coordinate for use with annotation
CLLocationCoordinate2D dartLocation;
dartLocation.latitude = DARTMOUTH_INN_LATITUDE;
dartLocation.longitude = DARTMOUTH_INN_LONGITUDE;
LocationAnnotation * myAnnotation = [LocationAnnotation alloc];
myAnnotation.coordinate = dartLocation;
myAnnotation.title = @"The Dartmouth Inn";
[self.ClientMapView addAnnotation:myAnnotation];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import "MainMapViewController.h"
#import "LocationAnnotation.h"
@interface MainMapViewController ()
@end
//Totnes Main Centre Coordinates
#define Totnes_LATITUDE 50.433741
#define Totnes_LONGITUDE -3.685797
//The Dartmouth Inn Coordinates
#define DARTMOUTH_INN_LATITUDE 50.430036;
#define DARTMOUTH_INN_LONGITUDE -3.683873;
//Pub Offers Co-Ordinates
#define TheKingBill_LATITUDE 50.431379
#define TheKingBill_LONGITUDE -3.685495
#define TheSevenStars_LATITUDE 50.431045
#define TheSevenStars_LONGITUDE -3.682945
#define TheLordNelson_LATITUDE 50.430931
#define TheLordNelson_LONGITUDE -3.683644
//Span
#define THE_SPAN 0.01f;
@implementation MainMapViewController
@synthesize mainMapView;
- (void)viewDidLoad
{
[super viewDidLoad];
//Set Delegate
mainMapView.delegate = self;
//Create the region
MKCoordinateRegion myRegion;
//Centre
CLLocationCoordinate2D centre;
centre.latitude = Totnes_LATITUDE;
centre.longitude = Totnes_LONGITUDE;
//Span
MKCoordinateSpan span;
span.latitudeDelta = THE_SPAN;
span.longitudeDelta = THE_SPAN;
myRegion.center = centre;
myRegion.span = span;
//Set The Map View
[mainMapView setRegion:myRegion animated:YES];
//Annotation
NSMutableArray * locations = [[NSMutableArray alloc] init];
LocationAnnotation * myAnn;
//The King Bill Annotation
myAnn = [[LocationAnnotation alloc] initWithTitle:@"The King Bill"
andSubtitle:@"Another Pub In Town"
andCoordinate:CLLocationCoordinate2DMake(TheKingBill_LATITUDE, TheKingBill_LONGITUDE)
andID:1];
[locations addObject:myAnn];
//The Seven Stars Annotations
myAnn = [[LocationAnnotation alloc] initWithTitle:@"The Royal Seven Stars Hotel"
andSubtitle:@"Hotel In Town"
andCoordinate:CLLocationCoordinate2DMake(TheSevenStars_LATITUDE, TheSevenStars_LONGITUDE)
andID:2];
[locations addObject:myAnn];
//The Lord Nelson Annotations
myAnn = [[LocationAnnotation alloc] initWithTitle:@"The Lord Nelson"
andSubtitle:@"Great Pub In Centre of Town"
andCoordinate:CLLocationCoordinate2DMake(TheLordNelson_LATITUDE, TheLordNelson_LONGITUDE)
andID:3];
[locations addObject:myAnn];
[self.mainMapView addAnnotations:locations];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - MKMapViewDelegate
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation {
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
MKPinAnnotationView *annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pin"];
int annId = ((LocationAnnotation *)annotation).idNumber;
annView.pinColor = (annId == 1) ? MKPinAnnotationColorPurple
: (annId == 2) ? MKPinAnnotationColorGreen
: MKPinAnnotationColorRed;
annView.canShowCallout = YES;
return annView;
}
#导入“MainMapViewController.h”
#导入“LocationAnnotation.h”
@接口MainMapViewController()
@结束
//托特内斯主中心坐标
#定义Totnes_纬度50.433741
#定义Totnes_经度-3.685797
//达特茅斯坐标酒店
#定义达特茅斯酒店纬度50.430036;
#定义达特茅斯酒店经度-3.683873;
//酒吧提供座标
#定义主比尔_纬度50.431379
#定义主经度-3.685495
#定义七星纬度50.431045
#定义七颗星的经度-3.682945
#定义纬度50.430931
#定义经度-3.683644
//跨度
#定义_跨度0.01f;
@实现MainMapViewController
@综合主地图视图;
-(无效)viewDidLoad
{
[超级视图下载];
//集合委托
mainMapView.delegate=self;
//创建区域
MK协调区域myRegion;
//中心
位置协调中心;
中心纬度=托特内斯纬度;
中心经度=总经度;
//跨度
Mk坐标跨度;
span.latitudeDelta=_span;
span.longitudeDelta=_span;
myRegion.center=中心;
myRegion.span=span;
//设置地图视图
[mainMapView setRegion:myRegion动画:是];
//注释
NSMUTABLEARRY*位置=[[NSMUTABLEARRY alloc]init];
地点注释*myAnn;
//国王法案注释
myAnn=[[LocationAnnotation alloc]initWithTitle:@“国王法案”
副标题:@“镇上的另一家酒吧”
AND坐标:CLLOCATION Coordinated2 DMAKE(主比尔纬度,主比尔经度)
andID:1];
[对象:myAnn];
//七星批注
myAnn=[[LocationAnnotation alloc]initWithTitle:@“皇家七星级酒店”
副标题:@“城市酒店”
AND坐标:CLLOCATION坐标2 DMAKE(七颗星的纬度,七颗星的经度)
andID:2];
[对象:myAnn];
//纳尔逊勋爵
myAnn=[[LocationAnnotation alloc]initWithTitle:@“纳尔逊勋爵”
副标题:@“市中心的大酒吧”
AND坐标:CLLOCATION坐标2DMAKE(洛德尼尔森纬度,洛德尼尔森经度)
andID:3];
[对象:myAnn];
[self.mainMapView addAnnotations:位置];
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
#pragma标记-MKMapViewDelegate
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释{
if([annotation isKindOfClass:[MKUserLocation类]])
返回零;
MKPinAnnotationView*annView=[[MKPinAnnotationView alloc]initWithAnnotation:annotation重用标识符:@“pin”];
int annId=((LocationAnnotation*)annotation.idNumber;
annView.pinColor=(annId==1)?MKPinAnnotationColorPurple
:(annId==2)?MKPinAnnotationColorGreen
:MKPinAnnotationColorRed;
annView.canShowCallout=是;
返回视图;
}
好的,这里是经过修改的代码:
位置注释.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface LocationAnnotation : NSObject <MKAnnotation>
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property int idNumber;
- (id)initWithTitle:(NSString *)ttl andSubtitle:(NSString *)subttl andCoordinate:(CLLocationCoordinate2D)c2d andID:(int)idN;
@end
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface LocationAnnotation : NSObject <MKAnnotation>
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property int idNumber;
- (id)initWithTitle:(NSString *)ttl andSubtitle:(NSString *)subttl andCoordinate:(CLLocationCoordinate2D)c2d andID:(int)idN;
@end
MainMapViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface MapViewController : UIViewController
@property (weak, nonatomic) IBOutlet MKMapView *ClientMapView;
@end
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface MainMapViewController : UIViewController <MKMapViewDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *mainMapView;
@end
#导入
#进口
@接口MainMapViewController:UIViewController
@属性(弱、非原子)IBMOutlet MKMapView*mainMapView;
@结束
mainmappviewcontroller.m
#import "MapViewController.h"
#import "LocationAnnotation.h"
@interface MapViewController ()
@end
//The Dartmouth Inn Coordinates
#define DARTMOUTH_INN_LATITUDE 50.430036;
#define DARTMOUTH_INN_LONGITUDE -3.683873;
//Span
#define THE_SPAN 0.004f;
@implementation MapViewController
@synthesize ClientMapView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//Create the region
MKCoordinateRegion myRegion;
//Centre
CLLocationCoordinate2D centre;
centre.latitude = DARTMOUTH_INN_LATITUDE;
centre.longitude = DARTMOUTH_INN_LONGITUDE;
//Span
MKCoordinateSpan span;
span.latitudeDelta = THE_SPAN;
span.longitudeDelta = THE_SPAN;
myRegion.center = centre;
myRegion.span = span;
//Set The Map View
[ClientMapView setRegion:myRegion animated:YES];
//Annotation
//1. Create A Coordinate for use with annotation
CLLocationCoordinate2D dartLocation;
dartLocation.latitude = DARTMOUTH_INN_LATITUDE;
dartLocation.longitude = DARTMOUTH_INN_LONGITUDE;
LocationAnnotation * myAnnotation = [LocationAnnotation alloc];
myAnnotation.coordinate = dartLocation;
myAnnotation.title = @"The Dartmouth Inn";
[self.ClientMapView addAnnotation:myAnnotation];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import "MainMapViewController.h"
#import "LocationAnnotation.h"
@interface MainMapViewController ()
@end
//Totnes Main Centre Coordinates
#define Totnes_LATITUDE 50.433741
#define Totnes_LONGITUDE -3.685797
//The Dartmouth Inn Coordinates
#define DARTMOUTH_INN_LATITUDE 50.430036;
#define DARTMOUTH_INN_LONGITUDE -3.683873;
//Pub Offers Co-Ordinates
#define TheKingBill_LATITUDE 50.431379
#define TheKingBill_LONGITUDE -3.685495
#define TheSevenStars_LATITUDE 50.431045
#define TheSevenStars_LONGITUDE -3.682945
#define TheLordNelson_LATITUDE 50.430931
#define TheLordNelson_LONGITUDE -3.683644
//Span
#define THE_SPAN 0.01f;
@implementation MainMapViewController
@synthesize mainMapView;
- (void)viewDidLoad
{
[super viewDidLoad];
//Set Delegate
mainMapView.delegate = self;
//Create the region
MKCoordinateRegion myRegion;
//Centre
CLLocationCoordinate2D centre;
centre.latitude = Totnes_LATITUDE;
centre.longitude = Totnes_LONGITUDE;
//Span
MKCoordinateSpan span;
span.latitudeDelta = THE_SPAN;
span.longitudeDelta = THE_SPAN;
myRegion.center = centre;
myRegion.span = span;
//Set The Map View
[mainMapView setRegion:myRegion animated:YES];
//Annotation
NSMutableArray * locations = [[NSMutableArray alloc] init];
LocationAnnotation * myAnn;
//The King Bill Annotation
myAnn = [[LocationAnnotation alloc] initWithTitle:@"The King Bill"
andSubtitle:@"Another Pub In Town"
andCoordinate:CLLocationCoordinate2DMake(TheKingBill_LATITUDE, TheKingBill_LONGITUDE)
andID:1];
[locations addObject:myAnn];
//The Seven Stars Annotations
myAnn = [[LocationAnnotation alloc] initWithTitle:@"The Royal Seven Stars Hotel"
andSubtitle:@"Hotel In Town"
andCoordinate:CLLocationCoordinate2DMake(TheSevenStars_LATITUDE, TheSevenStars_LONGITUDE)
andID:2];
[locations addObject:myAnn];
//The Lord Nelson Annotations
myAnn = [[LocationAnnotation alloc] initWithTitle:@"The Lord Nelson"
andSubtitle:@"Great Pub In Centre of Town"
andCoordinate:CLLocationCoordinate2DMake(TheLordNelson_LATITUDE, TheLordNelson_LONGITUDE)
andID:3];
[locations addObject:myAnn];
[self.mainMapView addAnnotations:locations];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - MKMapViewDelegate
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation {
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
MKPinAnnotationView *annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pin"];
int annId = ((LocationAnnotation *)annotation).idNumber;
annView.pinColor = (annId == 1) ? MKPinAnnotationColorPurple
: (annId == 2) ? MKPinAnnotationColorGreen
: MKPinAnnotationColorRed;
annView.canShowCallout = YES;
return annView;
}
#导入“MainMapViewController.h”
#导入“LocationAnnotation.h”
@接口MainMapViewController()
@结束
//托特内斯主中心坐标
#定义Totnes_纬度50.433741
#定义Totnes_经度-3.685797
//达特茅斯坐标酒店
#定义达特茅斯酒店纬度50.430036;
#定义达特茅斯酒店经度-3.683873;
//酒吧提供座标
#定义主比尔_纬度50.431379
#定义主经度-3.685495
#定义七星纬度50.431045
#定义七颗星的经度-3.682945
#定义纬度50.430931
#定义经度-3.683644
//跨度
#定义_跨度0.01f;
@实现MainMapViewController
@综合主地图视图;
-(无效)viewDidLoad
{
[超级视图下载];
//集合委托
mainMapView.delegate=self;
//创建区域
MK协调区域myRegion;
//中心
位置协调中心;
中心纬度=托特内斯纬度;
中心经度=总经度;
//跨度
Mk坐标跨度;
span.latitudeDelta=_span;
span.longitudeDelta=_span;
myRegion.center=中心;
myRegion.span=span;
//设置地图视图
[mainMapView setRegion:myRegion动画:是];
//注释
NSMUTABLEARRY*位置=[[NSMUTABLEARRY alloc]init];
位置