示例代码:
main.m
// // main.m // ControlDemo // // Created by watsy0007 on 12-6-3. // Copyright (c) 2012年 __MyCompanyName__. All rights reserved. // #import <UIKit/UIKit.h> #import <MapKit/MapKit.h> #import <CoreLocation/CoreLocation.h> #define MAPVIEW_SEGMENTCONTROL_TAG_TYPE 1 #define MAPVIEW_SEGMENTCONTROL_TAG_USERTRACK 2 @interface ViewController : UIViewController <MKMapViewDelegate> { MKMapView *_mapView_; } @end @implementation ViewController - (void) dealloc { [_mapView_ release]; [super dealloc]; } //随机生成覆盖位置 - (NSArray *) randomOverlay:(NSInteger) nMax { NSMutableArray *overlayArray = [NSMutableArray arrayWithCapacity:nMax]; for (int i = 0; i < nMax; i++) { double dRandlatitude = (double)(rand() % 10000) / 1000000; double dRandlongitude = (double)(rand() % 10000) / 1000000; BOOL bAdd = rand() % 2; if (bAdd) { dRandlatitude = -dRandlatitude; } bAdd = rand() % 2; if (bAdd) { dRandlongitude = -dRandlongitude; } MKCircle *mCircle = [MKCircle circleWithCenterCoordinate:CLLocationCoordinate2DMake(22.551368 + dRandlatitude, 113.882654 + dRandlongitude) radius:100.0]; [overlayArray addObject:mCircle]; } return overlayArray; } - (void) loadView { [super loadView]; self.view.backgroundColor = [UIColor whiteColor]; self.title = @"MapKit Demo"; _mapView_ = [[MKMapView alloc] initWithFrame:self.view.bounds]; _mapView_.delegate = self; //显示用户坐标 _mapView_.showsUserLocation = YES; //设置地区 MKCoordinateRegion currentRegion = MKCoordinateRegionMakeWithDistance(CLLocationCoordinate2DMake(22.551368,113.882654), 0.0002, 0.0002); // _mapView_.region = currentRegion; [_mapView_ setRegion:currentRegion animated:YES]; MKCoordinateRegion fitRegin = [_mapView_ regionThatFits:currentRegion]; [_mapView_ setRegion:fitRegin animated:YES]; [_mapView_ addOverlays:[self randomOverlay:10]]; [self.view addSubview:_mapView_]; } - (void) viewWillUnload { [_mapView_ release]; [super viewWillUnload]; } - (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { return YES; } - (void) viewDidLoad { [super viewDidLoad]; } #pragma mark - #pragma mark MKMapViewDelegate delegate //完成更新用户坐标 - (void) mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation { MKMapPoint pUser = MKMapPointForCoordinate(userLocation.coordinate); //动态计算要现实的矩形 NSArray *array = [mapView overlays]; MKMapRect visiableRect = [mapView visibleMapRect]; if (pUser.x < visiableRect.origin.x) { visiableRect.origin.x = pUser.x; visiableRect.size.width += (visiableRect.origin.x - pUser.x); } for (int i = 0; i < [array count]; i++) { MKCircle *circleOverlay = [array objectAtIndex:i]; MKMapPoint play = MKMapPointForCoordinate(circleOverlay.coordinate); if (play.x < visiableRect.origin.x) { visiableRect.origin.x = play.x; visiableRect.size.width += (visiableRect.origin.x - play.x); } else if (play.x > (visiableRect.origin.x + visiableRect.size.width) ) { visiableRect.size.width = visiableRect.size.width + (play.x - (visiableRect.origin.x + visiableRect.size.width)); } if (play.y < visiableRect.origin.y) { visiableRect.origin.y = play.y; visiableRect.size.height = visiableRect.size.height + (visiableRect.origin.y - play.y); } else if (play.y > (visiableRect.origin.y + visiableRect.size.height)) { visiableRect.size.height = visiableRect.size.height + (play.y - (visiableRect.origin.y + visiableRect.size.height)); } } [mapView setVisibleMapRect:visiableRect animated:YES]; } - (MKOverlayView *) mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay { //返回覆盖的视图 MKCircleView *circleView = (MKCircleView *)[mapView viewForOverlay:overlay]; if (circleView == nil) { circleView = [[[MKCircleView alloc] initWithOverlay:overlay] autorelease]; circleView.strokeColor = [UIColor redColor]; circleView.lineWidth = 2.0; circleView.fillColor = [UIColor colorWithRed:202.0 / 255.0 green:202.0 / 255.0 blue:202.0 / 255.0 alpha:1.0]; } return circleView; } @end //----------------------------------------------------------------------------------------------------- #pragma mark - #pragma mark AppDelegate @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @property (strong, nonatomic) UIViewController *viewController; @end @implementation AppDelegate @synthesize window = _window; @synthesize viewController = _viewController; - (void) dealloc { [_window release]; [_viewController release]; [super dealloc]; } - (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { srand(time(NULL)); self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; self.viewController = [[ViewController alloc] init]; UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController:self.viewController]; self.window.rootViewController = controller; [controller release]; [self.window makeKeyAndVisible]; return YES; } @end int main(int argc, char *argv[]) { @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); } }