IOS 之 Core Loaction 定位服务与地图

本文将深入探讨iOS开发的关键技术,包括Swift编程语言、Xcode开发环境、UIKit和Foundation框架,以及如何构建高性能的iOS应用。涵盖从基础知识到高级实践的全面教程。

   使用CLLoactionManager类之前得加入 CoreLocation框架

然后加入头文件 

#import<CoreLocation/CoreLocation.h>

#import<CoreLocation/CLLocationManagerDelegate>


接着定义变量

CLLocationManager* location;

_location = [[CLLocationManageralloc]init];

_location.delegate =self;  

_location.desiredAccuracy =kCLLocationAccuracyBest; //设置精确度到M  精确越大越耗电

_location.distanceFilter = 1000.0f; //多少距离侦测一次

[_locationstartUpdatingLocation]; //开始侦测

[_location stopUpdatingLocation]; //停止侦测


//委托方法  

//更新方法
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation* lo = [locations lastObject];
    _pointX.text = [NSString stringWithFormat:@"%g", lo.coordinate.longitude];
    _pointY.text = [NSString stringWithFormat:@"%g", lo.coordinate.latitude];
    
    CLGeocoder *geocoder = [CLGeocoder new];
    [geocoder reverseGeocodeLocation:lo completionHandler:^(NSArray *placemarks, NSError *error) {
        if ( [placemarks count] > 0 ) {
            CLPlacemark* placemark = placemarks[0];
            NSDictionary *dictAddress = placemark.addressDictionary;
            NSString *strAddress = [dictAddress objectForKey:(NSString*)kABPersonAddressStateKey];
            self.address.text = strAddress;
            NSLog(@"地址: %@", strAddress);
        }
    }];
}

//失败时调用的方法

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error


CLLocation* lo; 属性

lo.coordinate.longitude  //精度

lo.coordinate.latitude  //纬度

lo.horizontalAccuracy  //中心圆半径.相当于地图上你看到的自己所在的一个圆圈  值越大位置越不确定 为负时就不能依赖坐标值了.

lo.altitude  //海平面上下多少米

lo.verticalAccuracy  //海拨, 为负不确定海拨

ClLocationDistance distance = [lo distanceFromLocation: CLLocation* lo]; //计算两个之间的距离


//地理信息反编码,就是把看不懂的坐标数字转换为地点的相关描述,包括国家,市,街道,很准确

//使用这个类取地址信息的时候还需要引入

#import <AddressBook/AddressBook.h>

CLGeocoder *geocoder = [CLGeocodernew]; //反编码类

CLPlacemark* placemark = placemarks[0]; //封装着地点相关信息

placemark.addressDictionary //地址信息的字典,包含一些键值对,在地址簿框架中

placemark.ISOcountryCode    //ISO国家代号

placemark.postalCode        //邮政编码

placemark.administrativeArea  //行政区域信息

placemark.subAdministrativeArea  //行政区域附加信息

placemark.locality               //指定城市信息

placemark.subLocality            //指定城市信息附加信息

placemark.thoroughfare           //指定街道级别信息

placemark.subThoroughfare        //指定街道级别的附加信息


//通过地址信息查询

geocodeAddressString:completionHandler:


//这个网址可以是地图上的一些资料也可以生成GPX文件

http://mygeoposition.com


//关于地图相关的IOS6自带的地图

核心视图类是MKMapView,委托协议是MKMapViewDelegate 使用时需要导入MapKit框架. 

#import <MapKit/MapKit.h>

MKMapView *mapView;

mapView.mapType

有以下几个类型

MKMapTypeStandard 标准地图

MKMapTypeStatellite 卫星地图

MKMapTypeHybrid  混合地图

mapView.showUserLocation 允许跟踪显示用户位置信息

setUserTrackingMode:MKUserTrackingModeFloow:animated: 设置跟踪模式 还得实现委托方法

MKUserTrackingModeNone 没有用户跟踪模式

MKUserTrackingModeFollow 可以跟踪用户的位置变化

MKUserTrackingModeFollowWithHeading 可以跟踪用户的位置和方向变化

//设置跟踪用户
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    _mapView.centerCoordinate = userLocation.location.coordinate;
}

//添加地图标注

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation* lo = [locations lastObject];
    _pointX.text = [NSString stringWithFormat:@"%g", lo.coordinate.longitude];
    _pointY.text = [NSString stringWithFormat:@"%g", lo.coordinate.latitude];
    
    CLGeocoder *geocoder = [CLGeocoder new];
    [geocoder reverseGeocodeLocation:lo completionHandler:^(NSArray *placemarks, NSError *error) {
        if ( [placemarks count] > 0 ) {
            [_mapView removeAnnotations:_mapView.annotations];
            CLPlacemark* placemark = placemarks[0];
            //调整地图位置和缩放比例
            MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(placemark.location.coordinate, 10000, 10000);
            [_mapView setRegion:viewRegion animated:YES];
            LocationInfo *locationInfo = [LocationInfo new];
            locationInfo.stree = placemark.thoroughfare;
            locationInfo.coordinate = placemark.location.coordinate;
            //这个动作会触发下边委托方法
            [_mapView addAnnotation:locationInfo];
        }
    }];
}
//被上边的add动作调用最后一个参数就是上边的locationInfo,这个必须自己定义而且还得实现MKAnnotation协议

//地图类的委托方法.  用于显示地图上的标注
-(MKAnnotationView *)mapView:(MKMapView *)themapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:@"PIN_ANNOTATION"];
    if ( annotationView == nil ) {
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"PIN_ANNOTATION"];
    }
    
    annotationView.pinColor = MKPinAnnotationColorPurple; //大头针的颜色
    annotationView.animatesDrop = YES;   //设置标注视图时是否以动画效果
    annotationView.canShowCallout = YES;   //显示气泡提示信息
    
    return annotationView;
}

//使用程序外的苹果地图

需要使用MKPlacemark和MKMapItem这两个类

- (IBAction)query:(id)sender {
    CLLocationCoordinate2D ll2d = _sPoint.coordinate; //_sPoint是CLLoation类
    NSDictionary *address = _place.addressDictionary; //_place是 CLPlaceMark类
    MKPlacemark *place1 = [[MKPlacemark alloc] initWithCoordinate:ll2d addressDictionary:address];
    MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:place1];
    [mapItem openInMapsWithLaunchOptions:nil];
}

//调用谷歌WEB地图

- (IBAction)query:(id)sender {
    NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%f,%f", _sPoint.coordinate.latitude, _sPoint.coordinate.longitude];
    NSURL *url = [NSURL URLWithString:urlString];
    [[UIApplication sharedApplication] openURL:url];
}


越学越深.努力吧










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值