简体   繁体   English

有没有什么方法可以获取当前位置,而无需在我的应用程序中使用CLLocationManager委托

[英]Is There any way to get current location without using the CLLocationManager delegates in my application

I want to provide one method in my library that returns the current location.But I am not able to do that, because location will update and get in CLLocationManager delegate methods.So is there is any way to return back user with location after calling a method in my library. 我想在我的库中提供一种返回当前位置的方法,但是我无法做到这一点,因为位置将更新并获取CLLocationManager委托方法,因此有任何方法可以在调用a之后返回具有位置的用户我的图书馆中的方法。

Thanks in Advance. 提前致谢。

You can add a block parameter to your library method that will be called when it receives a location update. 您可以向您的库方法添加一个block参数,该参数将在收到位置更新时被调用。 That block can then be saved in a property (or into an array if multiple objects could be waiting at the same time). 然后可以将该块保存到一个属性中(如果可以同时等待多个对象,则可以保存到一个数组中)。

ps You really should use CLLocationManager . ps你真的应该使用CLLocationManager Any other method of determining the location will be less accurate / just as (if not more) time consuming. 确定位置的任何其他方法将不太准确/就像(如果不是更多的话)耗时一样。


You can define a block like (depending on how many locations you want to pass): 您可以定义一个类似的块(取决于您要通过的位置数):

typedef void(^LocationUpdateBlock)(CLLocation *location);

Then you add a public interface for the interested classes to register (simple version): 然后,为感兴趣的类添加一个公共接口以注册(简单版本):

property (copy, nonatomic) LocationUpdateBlock locationUpdateBlock;

Then in your delegate method: 然后在您的委托方法中:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    if (self.locationUpdateBlock != nil) {
        self.locationUpdateBlock([locations lastObject]);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM