2012-12-04 43 views
0

因此,我正在构建基于位置的游戏,并且在获取用户位置以正确初始化时遇到问题。 它并不总是发生,但有时位置永远不会从0,0变化。iOS Mapkit - 用户位置显示为0,0

这会导致一个问题,因为我有一个加载视图(Vault),阻止地图显示直到玩家位置加载,而不是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 
     } 
    } 
} 

回答

0

你似乎在试图让从_mapView.userLocation CLLocation对象的位置。

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

documentation看到,guide with snippets

+0

尤里卡!因此,在审阅文档之后,问题是一些缓存的“最后位置”被加载,从而导致3或4次调用委托方法。通过使用苹果公司的无视具有旧时间戳的位置的示例,我只能获取用户的当前位置并忽略缓存的位置。感谢您治疗我的头痛! – JimmyJammed

+0

不客气!很高兴我能帮上忙。 –