首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

舆图应用的简单示例

2012-07-18 
地图应用的简单示例首先,需要将CoreLocation.framework和MapKit.framework加入到项目中。?头文件:?#import

地图应用的简单示例

首先,需要将CoreLocation.framework和MapKit.framework加入到项目中。

?

头文件:

?

#import <UIKit/UIKit.h>#import <CoreLocation/CoreLocation.h>#import <MapKit/MapKit.h>@interface LocationViewController : UIViewController <CLLocationManagerDelegate> {CLLocationManager *locationManager;CLLocation *myLocation;IBOutlet UIButton *getLocationBtn;IBOutlet UILabel *latitudeLabel;IBOutlet UILabel *longitudeLabel;IBOutlet MKMapView *mapView;        }@property (nonatomic, retain) CLLocationManager *locationManager;@property (nonatomic, retain) CLLocation *myLocation;@property (nonatomic, retain) UIButton *getLocationBtn;@property (nonatomic, retain) UILabel *latitudeLabel;@property (nonatomic, retain) UILabel *longitudeLabel;@property (nonatomic, retain) MKMapView *mapView;- (IBAction) getLocationBtnClicked:(id) sender;- (void) initMapView;- (void) initLocation;@end

?

实现文件:

?

#import "LocationViewController.h"@implementation LocationViewController@synthesize locationManager;@synthesize myLocation;@synthesize getLocationBtn;@synthesize latitudeLabel;@synthesize longitudeLabel;@synthesize mapView;- (void)viewDidLoad {[self initMapView];[self initLocation];    [super viewDidLoad];}//初始化地图显示参数- (void) initMapView {//设定显示中心经纬度CLLocationCoordinate2D theCoordinate;theCoordinate.latitude = 38.148926;theCoordinate.longitude = -120.715542;//设定显示范围MKCoordinateSpan theSpan;theSpan.latitudeDelta = 0.1;theSpan.longitudeDelta = 0.1;//设置地图显示的中心及范围MKCoordinateRegion theRegion;theRegion.center = theCoordinate;theRegion.span = theSpan;//设置地图显示的类型及根据范围进行显示[self.mapView setMapType:MKMapTypeStandard];[mapView setRegion:theRegion];[mapView regionThatFits:theRegion];//定位后在地图上用蓝色点显示用户的位置mapView.showsUserLocation = YES;}//初始化定位相关参数- (void) initLocation {self.locationManager = [[CLLocationManager alloc] init];//设置代理locationManager.delegate = self;//设置需要的定位精度locationManager.desiredAccuracy = kCLLocationAccuracyBest;latitudeLabel.text = @"";longitudeLabel.text = @"";}- (IBAction) getLocationBtnClicked:(id) sender {latitudeLabel.text = @"";longitudeLabel.text = @"";//启动定位[locationManager startUpdatingLocation];}- (void)dealloc {[locationManager release];[myLocation release];[getLocationBtn release];[latitudeLabel release];[longitudeLabel release];[mapView release];    [super dealloc];}//代理方法#pragma mark -#pragma mark CLLocationManagerDelegate Methods//确定当前位置和位置更新了时调用这个方法- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation    fromLocation:(CLLocation *)oldLocation{//获得定位点的纬度信息NSString *latitudeString = [[NSString alloc] initWithFormat:@"%g", newLocation.coordinate.latitude];latitudeLabel.text = latitudeString;[latitudeString release];//获得定位点的经度信息NSString *longitudeString = [[NSString alloc] initWithFormat:@"%g", newLocation.coordinate.longitude];longitudeLabel.text = longitudeString;[longitudeString release];//停止定位[locationManager stopUpdatingLocation];//把地图中心移动到定位到的这个点[mapView setCenterCoordinate:newLocation.coordinate animated:YES];//重新设置地图显示的区域MKCoordinateSpan newSpan;newSpan.latitudeDelta = 0.05;newSpan.longitudeDelta = 0.05;MKCoordinateRegion newRegion;newRegion.center = newLocation.coordinate;newRegion.span = newSpan;[mapView setRegion:newRegion animated:YES];}//位置查询遇到错误时调用这个方法- (void)locationManager:(CLLocationManager *)manager    didFailWithError:(NSError *)error{NSString *errorType = (error.code == kCLErrorDenied) ?     @"Access Denied" : @"Unknown Error";    UIAlertView *alert = [[UIAlertView alloc]                           initWithTitle:@"Error getting Location"                           message:errorType                           delegate:nil                           cancelButtonTitle:@"OK"                           otherButtonTitles:nil];    [alert show];    [alert release];}@end

热点排行