2015-05-06 42 views
0

我遇到了将位置发送给其他用户的问题。我使用Parse.com作为我的后端,我使用此代码来获得一个位置:位置管理器精度不是很准确

-(void)sendLocation{ 



    if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]){ 

     [locationManager requestWhenInUseAuthorization]; 

    } 
    [locationManager startUpdatingLocation]; 



} 


- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    [locationManager stopUpdatingLocation]; //kill it NOW or we have duplicates 

    if(!self.haveLocation) { 

     self.haveLocation=YES; 

     NSLog(@"didUpdateToLocation: %@", newLocation); 

     self.currentLocation = newLocation; 
     self.currentLocationGeoPoint= [PFGeoPoint geoPointWithLocation:self.currentLocation]; 

} 

如果我从当前位置给自己发送消息,然后打开该邮件进行查看,通过比较当前的位置圈随着销子的下降,我可以看到他们不在同一个地方,相隔很远。

我有BOOL var haveLocation以确保它只刷新一次。我需要更多次刷新吗?任何关于如何使这更准确的指针将非常感激!由于

我尝试下面的苹果例子LocateMe:

我现在有:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 

    NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow]; 

    if (locationAge > 5.0) return; 

    // test that the horizontal accuracy does not indicate an invalid measurement 
    if (newLocation.horizontalAccuracy < 0) return; 

    // test the measurement to see if it is more accurate than the previous measurement 
    if (self.bestEffortAtLocation == nil || self.bestEffortAtLocation.horizontalAccuracy > newLocation.horizontalAccuracy) { 
     // store the location as the "best effort" 
     self.bestEffortAtLocation = newLocation; 


     if (newLocation.horizontalAccuracy <= locationManager.desiredAccuracy) { 

      [locationManager stopUpdatingLocation]; 

      //this code after getting the location 
      NSLog(@"didUpdateToLocation: %@", newLocation); 
} 

} 

}

然而,当我点击发送位置它只运行永远不会停止更新。我也设置了locationManager.desiredAccuracy = kCLLocationAccuracyBest;

+0

有你试过不杀死更新,只是记录结果,看看它们是否有所不同? Geoloc可能会非常棘手,就像Rob说的那样,经过几次尝试后,它有可能获得准确性。 –

+0

您还可以将精度精度设置为“最佳” –

+0

我使用了这个locationManager.desiredAccuracy = kCLLocationAccuracyBest; – Kex

回答

2

位置管理器可能会向您发送多个更新,因为它可以提高准确性。这是一项功能,旨在尽可能快地为您提供信息,同时最终为您提供最佳信息(至少达到您要求的准确度)。您正在积极避免这些更新,因此您可能会收到最不准确的信息。

您应该检查horizontalAccuracy以确定它是否足够准确供您使用。

请注意,您无法将horizontalAccuracykCLLocationAccuracyBest进行比较。 “最好”是一个常数-1。您需要将其与您需要的值(以米为单位)进行比较,如果需要花费太长时间(如果系统可能无法为您提供任何准确的值),可能会设置一个超时值。

+0

我只是看着horizo​​ntalAccuracy,并找到了一个苹果公司称为LocateMe的例子。我上面编辑了我的问题。现在的问题是,该位置永远不会停止更新。 – Kex

+0

正确。 'kCLLocationAccuracyBest'是-1。你不会得到好或更好的准确性。 –

+0

刚刚看到你的编辑,所以我可以做如果(newLocation.horizo​​ntalAccuracy <= 10)[locationManager stopUpdatingLocation];那会给半径10米? – Kex