#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface mylocation : UIViewController<CLLocationManagerDelegate,MKMapViewDelegate>
@property (retain, nonatomic) IBOutlet MKMapView *m_map;
- (IBAction)return:(id)sender;
@end
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.m_map.showsUserLocation=YES;
CLLocationManager *locationManager = [[CLLocationManager alloc] init];//创建位置管理器
locationManager.delegate=self;//设置代理
locationManager.desiredAccuracy=kCLLocationAccuracyBest;//指定需要的精度级别
locationManager.distanceFilter=1000.0f;//设置距离筛选器
[locationManager startUpdatingLocation];//启动位置管理器
MKCoordinateSpan theSpan;
//地图的范围 越小越精确
theSpan.latitudeDelta=0.05;
theSpan.longitudeDelta=0.05;
MKCoordinateRegion theRegion;
theRegion.center=[[locationManager location] coordinate];
theRegion.span=theSpan;
[self.m_map setRegion:theRegion];
[locationManager release];
}
设置标注
- (IBAction)FindUs:(id)sender {
self.UsMap.hidden = NO;
CLLocationCoordinate2D coord;
coord.latitude = 34.7993;
coord.longitude = 113.6939;
POI *poi = [[POI alloc]initWithCoords:coord];
[UsMap addAnnotation:poi];
MKCoordinateRegion theRegion = { {0.0, 0.0 }, { 0.0, 0.0 } };
theRegion.center=coord;
[UsMap setZoomEnabled:YES];
[UsMap setScrollEnabled:YES];
theRegion.span.longitudeDelta = 0.01f;
theRegion.span.latitudeDelta = 0.01f;
[UsMap setRegion:theRegion animated:YES];
}
@interface POI : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *subtitle;
NSString *title;
}
@property (nonatomic,readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic,retain) NSString *subtitle;
@property (nonatomic,retain) NSString *title;
-(id) initWithCoords:(CLLocationCoordinate2D) coords;
@end
@implementation POI
@synthesize coordinate,subtitle,title;
- (id) initWithCoords:(CLLocationCoordinate2D) coords{
self = [super init];
if (self != nil) {
coordinate = coords;
}
return self;
}
- (void) dealloc
{
[title release];
[subtitle release];
[super dealloc];
}
@end
/获取位置信息
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray* placemarks,NSError *error)
{
if (placemarks.count >0 )
{
CLPlacemark * plmark = [placemarks objectAtIndex:0];
NSString * country = plmark.country;
NSString * city = plmark.locality;
NSLog(@"%@-%@-%@",country,city,plmark.name);
self.m_locationName.text =plmark.name;
}
NSLog(@"%@",placemarks);
}];
//[geocoder release];