是否可以使用ios中的mapbox SDK在离线状态下查找用户当前位置

时间:2015-01-13 06:57:08

标签: ios objective-c mapbox

我正在离线地图上工作。我正在使用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];
}

提前致谢。请帮帮我

1 个答案:

答案 0 :(得分:0)

实际上你可以做到,但你必须打开你的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);
}