iPhone 定位到当前位置

在地图上显示自己当前所在的位置。

//

//  UserLoactionTestViewController.m

//  UserLoactionTest

//

//  Created by mir on 11-8-14.

//  Copyright __MyCompanyName__ 2011. All rights reserved.

//

#import "UserLoactionTestViewController.h"

@implementation UserLoactionTestViewController

- (void)loadView {

[super loadView];

mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 10, 320, 400)];

[mapView setDelegate:self];

[mapView setMapType:MKMapTypeStandard];

[self.view addSubview:mapView];

[mapView release];

mapView.showsUserLocation=YES;

    CLLocationManager *locationManager = [[CLLocationManager alloc] init];//创建位置管理器 

    locationManager.delegate=self;//设置代理 

    locationManager.desiredAccuracy=kCLLocationAccuracyBest;//指定需要的精度级别 

//locationManager.distanceFilter=1000.0f;//设置距离筛选器 

    [locationManager startUpdatingLocation];//启动位置管理器 

    MKCoordinateSpan theSpan; 

    //地图的范围 越小越精确 

    theSpan.latitudeDelta= 0.05f; 

    theSpan.longitudeDelta=0.05f; 

    MKCoordinateRegion theRegion; 

CLLocationCoordinate2D cr  = locationManager.location.coordinate;

    theRegion.center = cr; //[[locationManager location] coordinate]; 

    theRegion.span = theSpan; 

    [mapView setRegion:theRegion]; 

//NSLog(@"%f  %f",mapView.userLocation.location.coordinate.latitude,

//  mapView.userLocation.location.coordinate.longitude);

}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

//NSLog(@"dfdfdfd");

mapView.region = MKCoordinateRegionMake(newLocation.coordinate, MKCoordinateSpanMake(0.005f, 0.005f));

[manager stopUpdatingHeading];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

}

- (void)viewDidUnload {

}

- (void)dealloc {

    [super dealloc];

}

@end


猜你喜欢

转载自374016526.iteye.com/blog/1156926
今日推荐