0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Day09 地图定位(网络第07天)

Last updated at Posted at 2015-07-30

#系统地图功能

注意:添加头文件< MapKit/MapKit.h>
//创建地图视图

self.mapView = [[MKMapView alloc]initWithFrame:self.view.bounds];

// 设置坐标经纬度 第一个参数:latitude纬度 第二个参数:longitude经度

CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(30.662221,104.041367);

//设置缩放范围1 经纬度为111公里 0.1为 1公里 指定一个缩放的比例尺 值越小显示区域范围越小

 MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.01);

// 设置代表区域的结构体变量 第一个参数:地图中心点的经纬度坐标 第二个参数:跨度(比例尺)

MKCoordinateRegion region = MKCoordinateRegionMake(coord, span);

   self.mapView.delegate = self;

// 显示你当前的位置(GPS收到的位置)

self.mapView.showsUserLocation = YES;

// 设置地图的显示区域

[self.mapView setRegion:region];


[self.view addSubview:self.mapView];//(需要加到父视图上)

##2.用户位置追踪
//创建一个导航管理器

self.locManager = [[CLLocationManager alloc]init];
self.locManager.delegate = self;//委托(位置变化需要进行回调)
//设置引发位置更新时间的最小距离
self.locManager.distanceFilter = 10.0;//设置刷新距离

//ios8要调用此方法
//需要在info.plist文件中是指一个键值对
//键:NSLocationAlwaysUsageDescription
//值:YES

[self.locManager requestAlwaysAuthorization];//需授权后才能开启定位功能

//启动位置更新 当位置变化时执行回调方法)
//-(void)locationManager:didUpdateLocations:

 [self.locManager startUpdatingLocation];

// 位置信息更新之后的回调方法
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
//从数组中取出新位置的CLLocation对象

CLLocation * location = [locations firstObject];
NSLog(@"经度:%.2f , 纬度:%.2f",location.coordinate.longitude,location.coordinate.latitude);
//加动画效果
 [UIView animateWithDuration:5*0.618 animations:^{
     //调整地图的显示区域
  [self.mapView setRegion:MKCoordinateRegionMake(location.coordinate, MKCoordinateSpanMake(0.1, 0.1))];
 }];

}

//手势长按的方法
-(void)foo:(UILongPressGestureRecognizer *)sender{

 if (sender.state == UIGestureRecognizerStateBegan) {
       
    //由对象获得屏幕坐标
    CGPoint point = [sender locationInView:self.mapView];
    
    //将屏幕坐标转换成地图坐标
    CLLocationCoordinate2D c2d = [self.mapView  convertPoint:point toCoordinateFromView:self.mapView];
    NSLog(@"%f , %f",c2d.latitude,c2d.longitude);
    
    //添加大头针数据(数据模型)
    MKPointAnnotation * annotation = [[MKPointAnnotation alloc]init];
    //设置坐标
    annotation.coordinate = c2d;
    annotation.title = @"标题头";
    annotation.subtitle = @"副标题";
    //关联
    [self.mapView addAnnotation:annotation];
}

}

##3.大头针
//创建一个手势操作识别器

UILongPressGestureRecognizer * rec = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(foo:)];
//添加手势识别器
[self.mapView addGestureRecognizer:rec];

// 向地图添加大头针的回调方法 可以在此处对大头针定制(类似UITableViewCell)
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation{

//从地图视图上获得可复用的大头针视图对象
MKPinAnnotationView * pin = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"PIN"];
//如果没有
if(!pin){
    // 创建大头针试图对象
    pin = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"PIN"];
}
//动画下坠效果
pin.animatesDrop = YES;
//显示气泡效果
pin.canShowCallout = YES;

UIImage * image = [UIImage imageNamed:@"1.png"];
UIImageView * imageView = [[UIImageView alloc]initWithImage:image];
imageView.frame = CGRectMake(0, 0, 40, 40);
//左边定制
pin.leftCalloutAccessoryView =imageView;
UIButton * infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
infoButton.frame = CGRectMake(0, 0, 40, 40);
//右边定制
pin.rightCalloutAccessoryView = infoButton;
return pin;

}

##4.使用系统自带的地图应用
CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
//

CLLocation *loc = [[CLLocation alloc] initWithLatitude:30.66 2221 longitude:104.041367];

//

[geoCoder reverseGeocodeLocation:loc completionHandler:^(NSA rray *placemarks, NSError *error) {

//

CLPlacemark *pMark = [placemarks firstObject];

NSData *data = [NSJSONSerialization dataWithJSONObject:pMark.addressDictionary options:NSJSONWritingPrettyPrinted error:nil];

NSString *result = [[NSString alloc] initWithData:data e ncoding:NSUTF8StringEncoding];

NSLog(@"%@", result);
MKPlacemark *mkMark = [[MKPlacemark alloc] initWithPlace mark:pMark];

NSDictionary *options = @{MKLaunchOptionsMapTypeKey:@(MK MapTypeStandard)};

MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemar k:mkMark];

[mapItem openInMapsWithLaunchOptions:options]; }];
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?