【问题标题】:Map reverting to user's location, not allowing 'browse' iOS地图恢复到用户的位置,不允许“浏览”iOS
【发布时间】:2013-05-12 07:01:48
【问题描述】:

应用程序的地图屏幕显示用户的当前位置。我想让用户“浏览”地图(滚动并探索其他区域),我有一个按钮,可以让用户返回地图上的当前位置 但是 我是发生的情况是该应用程序不允许用户“浏览”地图并保留他们正在查看的视图,而是直接跳回用户的当前位置。

这里有一些代码:

-(void) setupLocation {
        locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self;
        //    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
        locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
        [locationManager startUpdatingLocation];
    }

    - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
    {
        NSLog(@"didFailWithError: %@", error);
    }

        - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
    {
        //    [self.mapView setCenterCoordinate:self.mapView.userLocation.coordinate animated:YES];

        tempLat = newLocation.coordinate.latitude;
        tempLon = newLocation.coordinate.longitude;
        CLLocationCoordinate2D currentLocation = CLLocationCoordinate2DMake(tempLat, tempLon);
        MKCoordinateRegion viewRegionLocation = MKCoordinateRegionMakeWithDistance(currentLocation, 100, 100);
        [self.mapView setRegion:viewRegionLocation animated:YES];

        locationNew = [[CLLocation alloc] initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];
        locationOld = [[CLLocation alloc] initWithLatitude:oldLocation.coordinate.latitude longitude:oldLocation.coordinate.longitude];

    }

我也有:

- (IBAction)stopUpdating:(id)sender {
    [locationManager stopUpdatingLocation];
}

还有:

- (IBAction)findMe:(id)sender {
    [locationManager startUpdatingLocation];
}

任何关于为什么地图不断跳回用户当前位置的想法?

非常感谢!

【问题讨论】:

    标签: ios mapkit core-location


    【解决方案1】:

    因为,在您的 - (void)locationManager: didUpdateToLocation: fromLocation: 方法中

    您每次都将地图视图的区域设置为更新的位置。

    删除这部分将解决您的问题。

      MKCoordinateRegion viewRegionLocation = MKCoordinateRegionMakeWithDistance(currentLocation, 100, 100);
      [self.mapView setRegion:viewRegionLocation animated:YES];
    

    编辑 1:

    在您的 viewDidLoad 方法中添加这一行,

    self.mapView.showsUserLocation = YES;
    

    像这样更改“查找我”按钮按下方法,

    -(IBAction) findMeButtonPressed:(id)sender;{
        MKCoordinateRegion viewRegionLocation = MKCoordinateRegionMakeWithDistance([self.locationManager location].coordinate, 100, 100);
        [self.mapView setRegion:viewRegionLocation animated:YES];
    }
    

    【讨论】:

    • 感谢您的回答。这确实有效,但这样做我不会在用户的位置获得特写。我想要做的基本上是当应用程序启动时,让用户看到他们当前的位置(100*100 区域),允许他们漫游,当点击“找到我”按钮时,返回他们当前的位置。有没有办法将初始位置显示为特写并且仍然允许漫游?
    • 我不知道为什么,但现在它根本没有显示位置。我现在要在真机上试用它,而不是在模拟器上。
    • Things 链接位置更新,最好在真机上试用 :)
    • 在设备上试过了,还是不行。它不会在发布时显示我的位置,现在我在 findMe 上遇到访问级别问题 - 不允许 self.locationManager - ??
    • 谢谢!!你很有帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-07
    • 1970-01-01
    • 2015-06-25
    • 2012-04-17
    • 1970-01-01
    相关资源
    最近更新 更多