本文实现功能是在地图上选取某点,获取该点的经纬度,然后我们就可以根据经纬度检索该地区相关信息了;
效果图:
蓝色点表示根据传入经纬度所定位出来的位置,红色大头针表示在地图上选取某点时插上大头针,标注该点经纬度信息;
代码部分:
视图初始化
- (void)viewDidLoad { [super viewDidLoad]; // 用一个经纬度初始化 CLLocation *location = [[CLLocation alloc] initWithLatitude:32.969 longitude:112.549]; // 设置缩放级别大小 MKCoordinateSpan span; span.latitudeDelta = 0.1; span.longitudeDelta = 0.1; // 设置显示范围 MKCoordinateRegion region; region.center = location.coordinate; region.span = span; mapView = [[MKMapView alloc] initWithFrame:self.view.bounds]; // 显示定位位置 mapView.showsUserLocation = YES; // 地图显示类型 mapView.mapType = MKMapTypeStandard; [self.view addSubview:mapView]; [mapView setRegion:region animated:NO]; [location release]; [mapView release]; // 定义一个iOS拍击手势 UITapGestureRecognizer *gestureTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapPress:)]; [mapView addGestureRecognizer:gestureTap]; }iOS上手指拍击事件
-(void)tapPress:(UIGestureRecognizer *)sender { NSLog(@"%@",NSStringFromSelector(_cmd)); CGPoint touchPoint = [sender locationInView:mapView]; CLLocationCoordinate2D touchLocation = [mapView convertPoint:touchPoint toCoordinateFromView:mapView]; NSLog(@"latitude--->%f",touchLocation.latitude); NSLog(@"longitude-->%F",touchLocation.longitude); [self creatAnnotation:touchLocation]; }创建大头针
-(void)creatAnnotation:(CLLocationCoordinate2D )locationCoordinate { MKPointAnnotation *pointAnnotation=Nil; pointAnnotation = [[[MKPointAnnotation alloc] init] autorelease]; pointAnnotation.coordinate = locationCoordinate; pointAnnotation.title = @"当前经纬度:"; pointAnnotation.subtitle = [NSString stringWithFormat:@"%f %f",locationCoordinate.latitude,locationCoordinate.longitude]; /*********自动调整当前地图最适位置************ MKCoordinateRegion region; region.center = locationCoordinate; region.span.latitudeDelta = 0.3; region.span.longitudeDelta = 0.3; [mapView setRegion:region animated:YES]; *****************************************/ [mapView addAnnotation:pointAnnotation]; }
源代码: