iOS 系統地圖實現及定位

來源:互聯網
上載者:User

標籤:ios


1:添加庫CoreLocation.framework,MApKit.framework;

2:@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) MKMapView *mapView;
@property (nonatomic, strong) CLLocation *checkinLocation;
@property (strong, nonatomic) NSString *currentLatitude; //緯度
@property (strong, nonatomic) NSString *currentLongitude; //經度

- (void)viewDidLoad {
    [super viewDidLoad];
    [email protected]"地圖";
    [self initRightBarButton];
    [self setupLocationManager];
    [self setupMapView];
}
-(void)initRightBarButton{
    UIButton* _btnRight = [UIButton buttonWithType:UIButtonTypeCustom];
    [_btnRight setFrame:CGRectMake(0, 0, 39, 34)];
    UIImage *imgNormal = [UIImage imageNamed:@"gpsbtn_dianji"];
    UIImage *imgSelect = [UIImage imageNamed:@"gpsbtn"];
    [_btnRight setBackgroundImage:imgNormal forState:UIControlStateNormal];
    [_btnRight setBackgroundImage:imgSelect forState:UIControlStateSelected];
    _btnRight.showsTouchWhenHighlighted = YES;
    [_btnRight addTarget:self action:@selector(showUserLocation:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:_btnRight];
    self.navigationItem.rightBarButtonItem = buttonItem;
}
- (void)setupLocationManager{
    _locationManager=[[CLLocationManager alloc]init];
    _locationManager.desiredAccuracy=kCLLocationAccuracyBestForNavigation;
    //每隔多少米定位一次(這裡的設定為任何的移動)
    _locationManager.distanceFilter=kCLDistanceFilterNone;
    //iOS 8.0以後
    if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        [self.locationManager requestWhenInUseAuthorization];
        [self.locationManager startUpdatingLocation];
    }
}
- (void)setupMapView{
    _mapView=[[MKMapView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:_mapView];
    _mapView.delegate=self;
    //使用者位置追蹤(使用者位置追蹤用於標記使用者當前位置,此時會調用定位服務)
    _mapView.userTrackingMode=MKUserTrackingModeFollowWithHeading;
    _mapView.mapType=MKMapTypeStandard;
    [self initLocationData];
}
- (void)initLocationData{
    NSMutableArray *arr=[[NSMutableArray alloc]init];
    for (int i=0; i<1; i++) {
        CLLocationDegrees lat=[self.currentLatitude  doubleValue];
        CLLocationDegrees longi=[self.currentLongitude doubleValue];
        NSString *[email protected]"當前位置";
        BasicMapAnnotation *anno=[[BasicMapAnnotation alloc]initWithLatitude:lat andLongitude:longi];
        anno.title=lacationName;
        anno.index=i;
        [arr addObject:anno];
    }
    [_mapView showAnnotations:arr animated:YES];
}
- (void)showUserLocation:(id)sender{
    MKCoordinateSpan span=MKCoordinateSpanMake(0.01, 0.02);
    MKCoordinateRegion region=MKCoordinateRegionMake(_mapView.userLocation.coordinate, span);
    [_mapView setRegion:region animated:YES];
}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
    NSLog(@"緯度:%f 經度:%f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
    self.currentLatitude  = [NSString stringWithFormat:@"%.4f",userLocation.location.coordinate.latitude];
    self.currentLongitude = [NSString stringWithFormat:@"%.4f",userLocation.location.coordinate.longitude];
    [self initLocationData];
    //設定地圖顯示範圍(如果不進列區域設定會自動顯示地區範圍並指定目前使用者位置為地圖中心點)
    //MKCoordinateSpan span=MKCoordinateSpanMake(0.01, 0.01);
    //MKCoordinateRegion region=MKCoordinateRegionMake(_mapView.region.center, span);
    //[_mapView setRegion:region animated:true];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    if ([annotation isKindOfClass:[BasicMapAnnotation class]]) {
        static NSString *[email protected]"AnnotationKey";
        MKAnnotationView *annotationView=[_mapView dequeueReusableAnnotationViewWithIdentifier:key];
        BasicMapAnnotation *aa=(BasicMapAnnotation *)annotation;
        if (!annotationView) {
            annotationView=[[MKAnnotationView alloc]initWithAnnotation:aa reuseIdentifier:key];
            CGRect frame=annotationView.frame;
            frame.size.width=14;
            frame.size.height=35;
            annotationView.frame=frame;
            annotationView.canShowCallout=YES;
            UIImageView*imageview=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"地表徵圖注"]];
            imageview.frame=frame;
            [annotationView addSubview:imageview];
        }
        return annotationView;
    }
    return nil;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

iOS 系統地圖實現及定位

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.