簡體   English   中英

是否可以通過使用iOS中的Mapbox SDK在離線狀態下找到用戶當前位置

[英]Is it possible to find user current location in offline by using mapbox SDK in ios

我正在離線地圖上工作。 我正在使用Mapbox SDK。
但是我無法跟蹤用戶的當前位置。
我寫了code like below :

  (void)viewDidLoad {

    [super viewDidLoad];
    RMMBTilesSource *offlineSource = [[RMMBTilesSource alloc] initWithTileSetResource:@"control-room-0.2.0" ofType:@"mbtiles"];

    RMMapView *mapView = [[RMMapView alloc] initWithFrame:self.view.bounds andTilesource:offlineSource];
    mapView.zoom = 2;
    [self.view addSubview:mapView];


    mapView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

    mapView.adjustTilesForRetinaDisplay = YES; // these tiles aren't designed specifically for retina, so make them legible

    [mapView setUserTrackingMode:RMUserTrackingModeFollow animated:true];
    mapView.draggingEnabled = YES;
    mapView.showsUserLocation = YES;
    mapView.userInteractionEnabled = YES;

    [self.view addSubview:mapView];
}

提前致謝。 請幫助我

實際上您可以做到,但是您必須打開wifi和3g網絡。 但是,如果showsUserLocation不適合您,則可以使用CLLocationManager進行操作。 希望我的代碼可以為您提供幫助。

- (IBAction)findMe:(id)sender {
    if (!_trackingActive) {
        NSLog(@"Tracking mode activated");
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        _locationManager.delegate = self;
        [_locationManager startUpdatingLocation];
        _startLocation = nil;
        _trackingActive = true;
        self.mapView.showsUserLocation = YES;

        if ([UIView instancesRespondToSelector:@selector(setTintColor:)])
            self.mapView.tintColor = [UIColor colorWithRed:0.200 green:1.000 blue:0.200 alpha:1];

        self.mapView.userLocation.title = @"Hola";
        self.mapView.userLocation.badgeIcon = [UIImage imageNamed:@"info"];
        self.mapView.userLocation.annotationIcon = [UIImage imageNamed:@"info"];

    }else{
        NSLog(@"Tracking mode deactivated");
        [_locationManager stopUpdatingLocation];
        _trackingActive = false;
        self.mapView.showsUserLocation = NO;
    }
}

-(void)locationManager:(CLLocationManager *)manager
   didUpdateToLocation:(CLLocation *)newLocation
          fromLocation:(CLLocation *)oldLocation
{
    NSString *currentLatitude = [[NSString alloc]
                                 initWithFormat:@"%+.6f",
                                 newLocation.coordinate.latitude];
    _latitude.text = currentLatitude;

    NSString *currentLongitude = [[NSString alloc]
                                  initWithFormat:@"%+.6f",
                                  newLocation.coordinate.longitude];
    _longitude.text = currentLongitude;

    NSString *currentHorizontalAccuracy =
    [[NSString alloc]
     initWithFormat:@"%+.6f",
     newLocation.horizontalAccuracy];
    _horizontalAccuracy.text = currentHorizontalAccuracy;

    NSString *currentAltitude = [[NSString alloc]
                                 initWithFormat:@"%+.6f",
                                 newLocation.altitude];
    _altitude.text = currentAltitude;

    NSString *currentVerticalAccuracy =
    [[NSString alloc]
     initWithFormat:@"%+.6f",
     newLocation.verticalAccuracy];
    _verticalAccuracy.text = currentVerticalAccuracy;

    if (_startLocation == nil)
        _startLocation = newLocation;

    CLLocationDistance distanceBetween = [newLocation
                                          distanceFromLocation:_startLocation];

    NSString *tripString = [[NSString alloc]
                            initWithFormat:@"%f",
                            distanceBetween];
    _distance.text = tripString;
    self.mapView.centerCoordinate = CLLocationCoordinate2DMake(currentLatitude.doubleValue, currentLongitude.doubleValue);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM