ios - 获取当前位置而不是缓存 ios
<p><p>我搜索了很多但没有得到任何满意的答案,我有一个场景,我根据用户的当前位置显示卖家列表。我第一次得到一个位置,然后当我尝试获取位置时运行我的应用程序时,我正在获取缓存位置数据。我确实在每 24 小时后尝试了一段时间,但仍然获得了我当前位置也被更改的缓存位置。以下是我用作引用的代码。请指教。</p>
<p>头文件中定义的属性</p>
<pre><code>@property (nonatomic,retain) CLLocationManager *locationManager;
- (void)startSingleLocationRequest
{
self.locationManager = [ init];
self.locationManager.delegate = self;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
;
;
}
#pragma mark --didUpdateLocations
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
self.latitudeValue =locations.lastObject.coordinate.latitude;
self.longitudeValue =locations.lastObject.coordinate.longitude;
// location set for simulation to UK
if(self.latitudeValue != 51.509979 && self.longitudeValue != -0.133700){
;
;
abc *slv =;
slv.receivedLatitudeValue = self.latitudeValue;
slv.receivedLongitudeValue = self.longitudeValue;
;
}
else{}
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>你可以这样做,</p>
<pre><code>-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
for(int i=0;i<locations.count;i++){
CLLocation * newLocation = ;
CLLocationCoordinate2D theLocation = newLocation.coordinate;
CLLocationAccuracy theAccuracy = newLocation.horizontalAccuracy;
NSTimeInterval locationAge = -;
if (locationAge > 5.0)
{
continue;
}
// do your all stuff here and then
break;
}
}
</code></pre>
<p>如果您的位置年龄超过 5 秒,您的代码将不会执行。您可以决定位置年龄是 10 秒还是 20 秒!</p>
<p><strong>更新:</strong> </p>
<p>只需替换您的属性(property)声明</p>
<pre><code> @property (nonatomic,retain) CLLocationManager *locationManager;
</code></pre>
<p>与</p>
<pre><code> @property (nonatomic,strong) CLLocationManager *locationManager;
</code></pre>
<p><strong>或</strong> </p>
<p>只需将其声明为实例变量,</p>
<pre><code> CLLocationManager *locationManager;
</code></pre>
<p>并从它的每个实例中删除 self!</p>
<p>第二件事,</p>
<p>你应该先做,</p>
<pre><code>;
</code></pre>
<p>然后开始更新位置,</p>
<pre><code>;
</code></pre></p>
<p style="font-size: 20px;">关于ios - 获取当前位置而不是缓存 ios,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/47261066/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/47261066/
</a>
</p>
页:
[1]