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

- (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];
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值