【问题标题】:iOS Mapkit - User location shown as 0, 0iOS Mapkit - 用户位置显示为 0, 0
【发布时间】:2012-12-04 05:03:14
【问题描述】:

所以我正在构建一个基于位置的游戏,但在正确初始化用户位置时遇到了问题。 它并不总是发生,但有时位置永远不会从 0,0 改变。

这会导致问题,因为我有一个加载视图(保险库),它会阻止地图显示,直到加载玩家的位置而不是 0,0。因此,如果未设置用户位置,保险库将永远不会打开并显示地图。

还有其他方法可以确保加载用户位置吗? 仅供参考 - 我已经确保他们启用了位置服务并且他们的设备是有能力的。 我在自己的设备上打开和关闭了这个问题,一切都正确启用。

ViewDidLoad:

/*Location Manager*/
locationManager = [[CLLocationManager alloc] init]; 
locationManager.delegate = self; 
[locationManager setDistanceFilter:100.0];//Update location to network when player moves X meters
[locationManager setDesiredAccuracy:kCLLocationAccuracyKilometer];//Nearest 1000 meters (.33 miles)
[locationManager startUpdatingLocation];

/*Map View*/
mapLoaded = false;
currentFilter = 0;
[_mapView setDelegate:self];
[_mapView setShowsUserLocation:YES];
[_mapView setMapType:MKMapTypeSatellite];

位置经理代表:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    //Wait until User Location is initialized (NOT 0,0)
    NSLog(@"Latitude: %f, Longitude: %f",_mapView.userLocation.coordinate.latitude,_mapView.userLocation.coordinate.longitude);
    if(_mapView.userLocation.location.coordinate.latitude!=0.00 && _mapView.userLocation.location.coordinate.longitude!=0.00){

        //Update Player Object
        [player setLatitude:_mapView.userLocation.location.coordinate.latitude];
        [player setLongitude:_mapView.userLocation.location.coordinate.longitude];

        [player updatePlayerLocation];//Update location to network
        if(mapLoaded){//Map already loaded, call refresh
            [self refreshMap];
        }
        else{//First load of map
            [_mapView setCenterCoordinate:_mapView.userLocation.location.coordinate];
            [self populateMap];
            [self openVault];
            mapLoaded = true;//Disable map first load
            //timerMap = [NSTimer scheduledTimerWithTimeInterval:60 target:self selector:@selector(refreshMap) userInfo:nil repeats:YES];//Auto-Refresh of Map
        }
    }
}

【问题讨论】:

    标签: ios mapkit cllocationmanager userlocation


    【解决方案1】:

    您似乎试图从_mapView.userLocation CLLocation 对象中获取位置。

    您应该使用传递给您的方法的locations 数组中的最后一项。

    另见documentationguide with snippets

    【讨论】:

    • 尤里卡!因此,在查看文档后,问题是一些缓存的“最后位置”正在加载,从而导致对委托方法的 3 或 4 次调用。通过使用 Apple 的忽略具有旧时间戳的位置的示例,我只能获取用户的当前位置而忽略缓存的位置。谢谢你治好了我的头痛!
    猜你喜欢
    • 2020-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多