【发布时间】:2011-10-15 05:03:06
【问题描述】:
假设我有下面的代码在视图上添加地图,在位置上添加 pin 的最简单方法是什么。有人可以建议将代码 sn-p 添加到我的代码中吗?
谢谢
- (void)viewDidLoad
{
MKMapView *map = [[MKMapView alloc] initWithFrame:CGRectMake(2, 2, 300, 300)];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = 22.278355;
region.center.longitude = 114.181713;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[map setRegion:region animated:YES];
[map regionThatFits:region];
[self.view addSubview:map];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
更新:
xxx.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface ShopDetail : UIViewController <MKMapViewDelegate> {
}
@end
xxx.m
- (void)viewDidLoad
{
MKMapView *map = [[MKMapView alloc] initWithFrame:CGRectMake(2, 2, 300, 300)];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = 22.278355; //40.105085;
region.center.longitude = 114.181713; //-83.005237;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[map setRegion:region animated:YES];
[map regionThatFits:region];
map.delegate = self;
[self.view addSubview:map];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
MKPinAnnotationView *pav = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
NSLog(@"called viewForAnnotation");
pav.pinColor = MKPinAnnotationColorGreen;
return pav;
}
*NSLog(@"调用viewForAnnotation");尚未执行。
【问题讨论】:
-
更新似乎对我不起作用,即使我只是复制了您的代码。您确定代理正在触发注解吗?
-
你可以在这里找到方法:stackoverflow.com/questions/5182082/…Cheers!
标签: objective-c