MKMapView确立当前位置并圈定范围

来源:互联网 发布:东华软件 减持 编辑:程序博客网 时间:2024/06/04 18:46
- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view from its nib.        CGRect mapRect = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);    //创建地图视图,初始化参数    m_mapview = [[MKMapView alloc] initWithFrame:mapRect];    //设置地图的代理    m_mapview.delegate = self;    [self.view addSubview:m_mapview];        //地图的类型:MKMapTypeStandard 显示街道和道路 MKMapTypeSatellite 显示卫星 MKMapTypeHybrid 显示混合地图    [m_mapview setMapType:MKMapTypeStandard];        //显示用户当前的坐标,打开地图有相应的提示    m_mapview.showsUserLocation=YES;        [[NSNotificationCenter defaultCenter] addObserver: self                                             selector:@selector(prepareSetMapPoint)                                                 name:@"NotifyLocataionDidUpdate"                                               object:nil];        UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]                                    initWithTitle:@"获取本机当前的位置"                                    style:UIBarButtonItemStylePlain                                    target:self                                    action:@selector(currentLocation:)];        self.navigationItem.rightBarButtonItem = rightButton;        [self currentLocation:nil];        }-(IBAction)currentLocation:(id)sender{            FMYAppDelegate *appDelegate = (FMYAppDelegate*)[[UIApplication sharedApplication] delegate];            [[NSNotificationCenter defaultCenter] addObserver: self                                             selector:@selector(prepareSetMapPoint)                                                 name:@"NotifyLocataionDidUpdate"                                               object:nil];        [appDelegate updateLocationInfor];        }-(void)prepareSetMapPoint{        [[NSNotificationCenter defaultCenter] removeObserver:self                                                    name:@"NotifyLocataionDidUpdate"                                                  object:nil];        FMYAppDelegate *appDelegate = (FMYAppDelegate*)[[UIApplication sharedApplication] delegate];        [appDelegate stopUpdateLocationInfo];        //经纬坐标    CLLocationCoordinate2D theCoordinate;        theCoordinate.latitude = appDelegate.fixedlatitude;    theCoordinate.longitude = appDelegate.fixedLongitude;        //定义显示的范围    MKCoordinateSpan theSpan;    theSpan.latitudeDelta=0.1;    theSpan.longitudeDelta=0.1;        //定义一个区域(用定义的经纬度和范围来大小来定义)    MKCoordinateRegion theRegion;    theRegion.center=theCoordinate;    theRegion.span=theSpan;        //在地图上显示此区域    [m_mapview setRegion:theRegion animated:YES];    //    [self SetMapPoint:theCoordinate];    }-(void)SetMapPoint:(CLLocationCoordinate2D)myLocation{        FMYMapPoint* m_poi = [[FMYMapPoint alloc]initWithCoords:myLocation];        [m_mapview addAnnotation:m_poi];        MKCoordinateRegion theRegion = { {0.0, 0.0 }, { 0.0, 0.0 } };    theRegion.center=myLocation;    [m_mapview setZoomEnabled:YES];    [m_mapview setScrollEnabled:YES];    theRegion.span.longitudeDelta = 0.01f;    theRegion.span.latitudeDelta = 0.01f;    [m_mapview setRegion:theRegion animated:YES];}