参考文章 http://blog.csdn.net/tangaowen/article/details/6527901
http://www.cnblogs.com/tangbinblog/archive/2012/07/11/2586715.html
http://www.cocoachina.com/newbie/basic/2011/1014/3367.html
#import#import #import @interface ViewController : UIViewController @property (retain, nonatomic) IBOutlet UITextField *latTxt;@property (retain, nonatomic) IBOutlet UITextField *lonTxg;@property (retain, nonatomic) IBOutlet UITextField *heightTxt;@end
#import "ViewController.h"#import "MapMark.h"@interface ViewController ()//定位管理@property(nonatomic,retain)CLLocationManager *manager;@property(nonatomic,retain)MKMapView *mapview;@end@implementation ViewController- (void)viewDidLoad{ [super viewDidLoad]; self.manager=[[CLLocationManager alloc] init]; _manager.delegate=self; _manager.desiredAccuracy=kCLLocationAccuracyBest; _manager.distanceFilter=1000.0; [_manager release]; self.mapview=[[MKMapView alloc] initWithFrame:CGRectMake(0, 210, 320, 200)]; _mapview.delegate=self; _mapview.mapType=MKMapTypeStandard; _mapview.zoomEnabled=YES; _mapview.showsUserLocation=YES;//调用cclocaiton [self.view addSubview:_mapview]; [_mapview release];}-(void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; [_manager startUpdatingLocation]; }-(void)viewDidDisappear:(BOOL)animated{ [super viewDidDisappear:animated]; [_manager stopUpdatingLocation];}#pragma mark -CLLocationManager delegate-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ NSLog(@"__%@",locations); CLLocation *lc= [locations lastObject]; self.latTxt.text=[NSString stringWithFormat:@"%3.5f",lc.coordinate.latitude]; self.lonTxg.text=[NSString stringWithFormat:@"%3.5f",lc.coordinate.longitude]; self.heightTxt.text=[NSString stringWithFormat:@"%3.5f",lc.altitude]; //设置显示区域 MKCoordinateRegion region=MKCoordinateRegionMake(lc.coordinate, MKCoordinateSpanMake(0.05, 0.005)); [_mapview setRegion:region animated:YES]; MapMark *mark=[[MapMark alloc] initwithCoordinate:lc.coordinate]; mark.title=@"小A"; mark.subtitle=@"20"; // 、、39.938, 116.416 CLLocationCoordinate2D l2d; l2d.latitude=37.78594; l2d.longitude=-122.40717; MapMark *mark1=[[MapMark alloc] initwithCoordinate:l2d]; mark1.title=@"小b"; mark1.subtitle=@"20"; [_mapview addAnnotation:mark];//只是把坐标的资料,放入到集合中。 [_mapview addAnnotation:mark1];}#pragma mark -map delegate//这个代理方法,会依照坐标资料的集合进行render-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation{ NSLog(@"-->vieforannotation-->%@",annotation); //判断是否刚添加的标注 if ([annotation isKindOfClass:[MapMark class]]) { static NSString *bridgeAnnotation=@"annotation"; //从缓存中取值 MKPinAnnotationView *view=(MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:bridgeAnnotation]; if (view==nil) { view=[[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:bridgeAnnotation] autorelease]; view.pinColor=MKPinAnnotationColorPurple; view.animatesDrop=YES; view.canShowCallout=YES; UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect]; btn.frame=CGRectMake(0, 0, 50, 50); [btn setTitle:@"click" forState:UIControlStateNormal]; view.rightCalloutAccessoryView=btn; } view.annotation=annotation;//充缓存中取出来重新设置坐标信息 return view; }return nil; }//点击标记-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{ NSLog(@"-->%@",[view.annotation class]); if ([view.annotation isMemberOfClass:[MapMark class]]) { UIButton *btn=(UIButton *)[view rightCalloutAccessoryView]; NSLog(@"--->%@",btn); }}#define MINIMUM_ZOOM_ARC 0.014 //approximately 1 miles (1 degree of arc ~= 69 miles)#define ANNOTATION_REGION_PAD_FACTOR 1.15#define MAX_DEGREES_ARC 360//size the mapView region to fit its annotations- (void)zoomMapViewToFitAnnotations:(MKMapView *)mapView animated:(BOOL)animated{ NSArray *annotations = mapView.annotations; int count = [mapView.annotations count]; if ( count == 0) { return; } //bail if no annotations //convert NSArray of id into an MKCoordinateRegion that can be used to set the map size //can't use NSArray with MKMapPoint because MKMapPoint is not an id MKMapPoint points[count]; //C array of MKMapPoint struct for( int i=0; i )[annotations objectAtIndex:i] coordinate]; points[i] = MKMapPointForCoordinate(coordinate); } //create MKMapRect from array of MKMapPoint MKMapRect mapRect = [[MKPolygon polygonWithPoints:points count:count] boundingMapRect]; //convert MKCoordinateRegion from MKMapRect MKCoordinateRegion region = MKCoordinateRegionForMapRect(mapRect); //add padding so pins aren't scrunched on the edges region.span.latitudeDelta *= ANNOTATION_REGION_PAD_FACTOR; region.span.longitudeDelta *= ANNOTATION_REGION_PAD_FACTOR; //but padding can't be bigger than the world if( region.span.latitudeDelta > MAX_DEGREES_ARC ) { region.span.latitudeDelta = MAX_DEGREES_ARC; } if( region.span.longitudeDelta > MAX_DEGREES_ARC ){ region.span.longitudeDelta = MAX_DEGREES_ARC; } //and don't zoom in stupid-close on small samples if( region.span.latitudeDelta < MINIMUM_ZOOM_ARC ) { region.span.latitudeDelta = MINIMUM_ZOOM_ARC; } if( region.span.longitudeDelta < MINIMUM_ZOOM_ARC ) { region.span.longitudeDelta = MINIMUM_ZOOM_ARC; } //and if there is a sample of 1 we want the max zoom-in instead of max zoom-out if( count == 1 ) { region.span.latitudeDelta = MINIMUM_ZOOM_ARC; region.span.longitudeDelta = MINIMUM_ZOOM_ARC; } [mapView setRegion:region animated:animated];}- (void)viewWillAppear:(BOOL)animated{ [self zoomMapViewToFitAnnotations:_mapview animated:animated]; //or maybe you would do the call above in the code path that sets the annotations array}- (void)dealloc { [_latTxt release]; [_lonTxg release]; [_heightTxt release]; [super dealloc];}@end
其中