来自:
首先,加入地图包
接口代码:
#import#import @interface View30 : UIViewController { MKMapView *map;}@end
实现代码:
- (void)viewDidLoad { [super viewDidLoad];//创建位置管理器CLLocationManager *locationManager = [[CLLocationManager alloc] init];//设置代理locationManager.delegate=self;//指定需要的精度级别locationManager.desiredAccuracy=kCLLocationAccuracyBest;//设置距离筛选器locationManager.distanceFilter=500.0f;//启动位置管理器[locationManager startUpdatingLocation]; MKCoordinateRegion theRegion = { { 0.0, 0.0 }, { 0.0, 0.0 } };theRegion.center=[[locationManager location] coordinate];[locationManager release]; //设置地图map=[[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];map.delegate=self;//是否显示用户的位置信息map.showsUserLocation=YES;//向上滚动 [map setZoomEnabled:YES];//横向滚动[map setScrollEnabled:YES];//设置地图范围 越小越精确theRegion.span.longitudeDelta = 0.05f;theRegion.span.latitudeDelta = 0.05f;[map setRegion:theRegion animated:YES];[self.view addSubview:map]; }- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id)annotation{ //注释指针MKPinAnnotationView *pinView = nil; static NSString *defaultPinID = @"mylocation";pinView = (MKPinAnnotationView *)[map dequeueReusableAnnotationViewWithIdentifier:defaultPinID];if ( pinView == nil ) {pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];}pinView.pinColor = MKPinAnnotationColorRed;pinView.canShowCallout = YES;pinView.animatesDrop = YES;[map.userLocation setTitle:@"我的位置"];[map.userLocation setSubtitle:@"小标题"];return pinView;}