ios系统自带地图基本方法实现

ios系统自带地图基本方法实现,第1张

概述ios系统自带地图基本方法实现

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

////  VIEwController.m//  ios自带地图笔记////  Created by 王木木 on 15/12/3.///* 导入<MapKit/MapKit.h>    <CoreLocation/CoreLocation.h>  两个库 引用地图前  尽量用真机测试  模拟器的话  等程序运行起来之后点击  xcode下方的  位置图片按钮标识  便可 需在info.pList中追加NSLocationWhenInUseUsageDescription或NSLocationAlwaysUsageDescription字段.其中: NSLocationWhenInUseUsageDescription表示应用在前台的时候可以搜到更新的位置信息。 NSLocationAlwaysUsageDescription表示应用在前台和后台(suspend或terminated)都可以获取到更新的位置数据。   */#import "VIEwController.h"#import <MapKit/MapKit.h>#import <CoreLocation/CoreLocation.h>@interface VIEwController ()<MKMapVIEwDelegate,CLLocationManagerDelegate>{    MKMapVIEw *_mapVIEw;    CLLocationManager *locationManager;    CLLocation *location;}@end@implementation VIEwController- (voID)vIEwDIDLoad {    [super vIEwDIDLoad];    // Do any additional setup after loading the vIEw,typically from a nib.    _mapVIEw =[[MKMapVIEw alloc]initWithFrame:self.vIEw.bounds];    _mapVIEw.zoomEnabled = YES;    _mapVIEw.showsUserLocation = YES;    _mapVIEw.scrollEnabled = YES;    _mapVIEw.delegate = self;    [self.vIEw addSubvIEw:_mapVIEw];    if([[UIDevice currentDevice].systemVersion floatValue] > 8.0f)    {        [self getUserLocation];    }     // 长按手势  长按添加大头针    UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(lpgrClick:)];    [_mapVIEw addGestureRecognizer:lpgr];    }//获取当前位置- (voID)getUserLocation{    locationManager = [[CLLocationManager alloc] init];    locationManager.delegate = self;    //kCLLocationAccuracyBest:设备使用电池供电时候最高的精度    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;    locationManager.distanceFilter = 50.0f;    if (([[[UIDevice currentDevice] systemVersion] doubleValue] >= 8.0))    {        [locationManager requestAlwaysAuthorization];    }    //更新位置    [locationManager startUpdatingLocation];}#pragma mark-CLLocationManagerDelegate  位置更新后的回调-(voID)locationManager:(CLLocationManager *)manager dIDUpdateLocations:(NSArray *)locations{    //停止位置更新    [locationManager stopUpdatingLocation];        CLLocation *loc = [locations firstObject];    CLLocationCoordinate2D theCoordinate;    //位置更新后的经纬度    theCoordinate.latitude = loc.coordinate.latitude;    theCoordinate.longitude = loc.coordinate.longitude;    //设定显示范围    MKCoordinateSpan theSpan;    theSpan.latitudeDelta=0.01;    theSpan.longitudeDelta=0.01;    //设置地图显示的中心及范围    MKCoordinateRegion theRegion;    theRegion.center=theCoordinate;    theRegion.span=theSpan;    [_mapVIEw setRegion:theRegion];    location = [locations lastObject];    CLGeocoder *geocoder = [[CLGeocoder alloc] init];       [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *array,NSError *error)     {         CLGeocoder *geocoder = [[CLGeocoder alloc] init];         [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *array,NSError *error) {                          if (array.count > 0)             {                 CLPlacemark *placemark = [array objectAtIndex:0];                 // 将获得的所有信息显示到label上                 NSLog(@"%@",placemark.administrativeArea);                 // 获取城市                 Nsstring *city = placemark.administrativeArea;                 if (!city) {                     // 四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)                     city = placemark.administrativeArea;                 }                 NSLog(@"当前城市:%@",city);                 // 设置地图显示的类型及根据范围进行显示  安放大头针                 MKPointAnnotation *pinAnnotation = [[MKPointAnnotation alloc] init];                 pinAnnotation.coordinate = theCoordinate;                 pinAnnotation.Title = city;                 [_mapVIEw addAnnotation:pinAnnotation];             }             else if (error == nil && [array count] == 0)             {                 NSLog(@"No results were returned.");             }             else if (error != nil)             {                 NSLog(@"An error occurred = %@",error);             }                               }];              }];}// 每次添加大头针都会调用此方法  可以设置大头针的样式- (MKAnnotationVIEw *)mapVIEw:(MKMapVIEw *)mapVIEw vIEwForAnnotation:(ID<MKAnnotation>)annotation{    // 判断大头针位置是否在原点,如果是则不加大头针    if([annotation isKindOfClass:[mapVIEw.userLocation class]])        return nil;    static Nsstring *annotationname = @"annotation";    MKPinAnnotationVIEw *anVIEw = (MKPinAnnotationVIEw *)[mapVIEw dequeueReusableAnnotationVIEwWithIDentifIEr:annotationname];    if(anVIEw == nil)    {        anVIEw = [[MKPinAnnotationVIEw alloc]initWithAnnotation:annotation reuseIDentifIEr:annotationname];    }    anVIEw.animatesDrop = YES;    //    // 显示详细信息    anVIEw.canShowCallout = YES;//    anVIEw.leftCalloutAccessoryVIEw   可以设置左视图//    anVIEw.rightCalloutAccessoryVIEw   可以设置右视图    return anVIEw;}//长按添加大头针事件- (voID)lpgrClick:(UILongPressGestureRecognizer *)lpgr{    // 判断只在长按的起始点下落大头针    if(lpgr.state == UIGestureRecognizerStateBegan)    {        // 首先获取点        CGPoint point = [lpgr locationInVIEw:_mapVIEw];        // 将一个点转化为经纬度坐标        CLLocationCoordinate2D center = [_mapVIEw convertPoint:point toCoordinateFromVIEw:_mapVIEw];        MKPointAnnotation *pinAnnotation = [[MKPointAnnotation alloc] init];        pinAnnotation.coordinate = center;        pinAnnotation.Title = @"长按";        [_mapVIEw addAnnotation:pinAnnotation];    }}//计算两个位置之间的距离- (voID)locationManager:(CLLocationManager *)manager dIDFailWithError:(NSError *)error{    NSLog(@"%@",error);}- (voID)dIDReceiveMemoryWarning {    [super dIDReceiveMemoryWarning];    // dispose of any resources that can be recreated.}@end

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的ios系统自带地图基本方法实现全部内容,希望文章能够帮你解决ios系统自带地图基本方法实现所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1105940.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存