让模拟器也支持GPS定位(模拟实现)

来源:互联网 发布:java orm框架比较 编辑:程序博客网 时间:2024/05/18 00:05
iOS上的GPS定位一般需要真机才能看到效果,但在开发的过程中,一般都在模拟器上调试。那怎么办呢?我们可以使用Object-C的策略,给模拟器指定一个经纬度,这样,定位就可以在模拟器上实现了。RealTool为你实现一个简单的demo。

// 模拟器 宏定义
#ifdef TARGET_IPHONE_SIMULATOR
@interface CLLocationManager (Simulator)
@end

@implementation CLLocationManager (Simulator)

-(void)startUpdatingLocation
{
    float latitude = 32.061;
    float longitude = 118.79125;   
    CLLocation *setLocation= [[[CLLocation alloc] initWithLatitude:latitude longitude:longitude] autorelease];
    [self.delegate locationManager:self didUpdateToLocation:setLocation
                      fromLocation:setLocation];
}
@end
#endif // TARGET_IPHONE_SIMULATOR

这样,在调用startUpdatingLocation的时候,就会自己调用返回经纬度的函数了。