iOS 地图

1.在程序info中添加设置并导入Libraries(CoreLocation.framework)

NSLocationWhenInUseUsageDescription     打开定位服务提高服务质量(随便写)

导入Libraries(CoreLocation.framework)

2.主页面打开地图定位服务

#import <CoreLocation/CoreLocation.h>

<CLLocationManagerDelegate>

@property (nonatomic) CLLocationManager * locationManager;

//判断是否打开定位服务

if (![CLLocationManager locationServicesEnabled]) {

        NSLog(@"Location Service Not Enabled");

        return;

}

self.locationManager = [CLLocationManager new];

if ([CLLocationManager authorizationStatus]==kCLAuthorizationStatusNotDetermined) {

        [self.locationManager requestWhenInUseAuthorization];

}

self.locationManager.delegate = self;

[self.locationManager startUpdatingLocation];


#pragma mark - CLLocationManagerDelegate

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations

{

CLLocation * location = locations[0];

    NSLog(@"%@", location);

     [manager stopUpdatingLocation];

}

3.创建MapViewController

#import <MapKit/MapKit.h>

<MKMapViewDelegate>

@property (weak, nonatomic) IBOutlet MKMapView *mapView;

@property (nonatomic) CLPlacemark * placemark;

@property (nonatomic) BOOL updateUserLocation;


if (self.userData.city.length<2)

{

return;

}

self.mapView.delegate = self;

CLGeocoder * geoCoder = [CLGeocoder new];

[geoCoder geocodeAddressString:self.userData.city completionHandler:^(NSArray *placemarks, NSError *error) {

        self.placemark = placemarks[0];

        MKPointAnnotation * ann = [MKPointAnnotation new];

        ann.coordinate = self.placemark.location.coordinate;

        ann.title = self.userData.name;

        ann.subtitle = self.userData.city;

        [self.mapView addAnnotation:ann];

        self.mapView.showsUserLocation = YES;

}];


#pragma mark - MKMapViewDelegate

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation

{

    if (!self.updateUserLocation) {

        MKCoordinateSpan span = MKCoordinateSpanMake(fabs(userLocation.coordinate.latitude-self.placemark.location.coordinate.latitude)+1, fabs(userLocation.coordinate.longitude-self.placemark.location.coordinate.longitude)+1);

        

        CLLocationCoordinate2D center = CLLocationCoordinate2DMake((userLocation.coordinate.latitude+self.placemark.location.coordinate.latitude)/2, (userLocation.coordinate.longitude+self.placemark.location.coordinate.longitude)/2);

        

        MKCoordinateRegion region = MKCoordinateRegionMake(center, span);

        [self.mapView setRegion:region animated:YES];

        

        self.updateUserLocation = YES;

    }

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值