-(MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation {
}
MKAnnotationView
方法用于在地图中显示图标。如何使用objective-c?{/ p>在-didUpdateToLocation
方法中调用MKAnnotationView方法
答案 0 :(得分:1)
我不确定这是否是你想要的,但是:
这是我为注释编写的课程:
.h文件:
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface MyLocation : NSObject <MKAnnotation> {
NSString *_name;
NSString *_address;
CLLocationCoordinate2D _coordinate;
}
@property (copy) NSString *name;
@property (copy) NSString *address;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (id)initWithName:(NSString*)name address:(NSString*)address coordinate:(CLLocationCoordinate2D)coordinate;
@end
和.m文件:
#import "MyLocation.h"
@implementation MyLocation
@synthesize name = _name;
@synthesize address = _address;
@synthesize coordinate = _coordinate;
- (id)initWithName:(NSString*)name address:(NSString*)address coordinate:(CLLocationCoordinate2D)coordinate {
if ((self = [super init])) {
_name = [name copy];
_address = [address copy];
_coordinate = coordinate;
}
return self;
}
- (NSString *)title {
if ([_name isKindOfClass:[NSNull class]])
return @"Unknown charge";
else
return _name;
}
- (NSString *)subtitle {
return _address;
}
@end
在视图控制器中我调用位置管理器,如:
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = 10.0; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
然后在位置管理器的委托方法中,我正在设置我的地图:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
[locationManager stopUpdatingLocation];
//CLLocationCoordinate2D newCoord = { newLocation.coordinate.latitude, newLocation.coordinate.longitude };
CLLocationCoordinate2D centerPoint = { newLocation.coordinate.latitude, newLocation.coordinate.longitude };
MKCoordinateSpan coordinateSpan = MKCoordinateSpanMake(0.01, 0.01);
MKCoordinateRegion coordinateRegion = MKCoordinateRegionMake(centerPoint, coordinateSpan);
["your-map-name" setRegion:coordinateRegion];
["your-map-name" regionThatFits:coordinateRegion];
CLLocationCoordinate2D cordPoint = { latitude_value, longitude_value };
MyLocation *annotation = [[MyLocation alloc] initWithName:@"title of the pin" address:@"some sub string" coordinate:cordPoint] ;
["your-map-name" addAnnotation:annotation]; }
这就是我解决我的注释和地图问题的方法..
我希望这会有所帮助..