Rex St John | 雷克斯 聖約翰

  • About Rex St John

Unit Tests

CLLocation Testing Category For Major Cities

Posted on April 22, 2013

Sometimes you need to fake locations within your application and the iOS simulator doesn’t come with built in preselected cities and regions.

I created this category helper to all my to manually set a location in a major city using the lat / lon coordinates. It is easy enough to add more cities by looking up their lat / lon, but this is a good start.


#import 

@interface CLLocation (Testing)

+(CLLocation*)locationInSeattle;
+(CLLocation*)locationInNewYork;
+(CLLocation*)locationInSanFransisco;
+(CLLocation*)locationInBoston;
+(CLLocation*)locationInMiami;
+(CLLocation*)locationInLondon;

@end

#import "CLLocation+Testing.h"

@implementation CLLocation (Testing)

+(CLLocation*)locationInSeattle{
    CLLocationDegrees lat = 47.6097;
    CLLocationDegrees lon = -122.3331;
    CLLocation *someLocation = [[CLLocation alloc] initWithLatitude:lat longitude:lon];
    return  someLocation;
}

+(CLLocation*)locationInNewYork{
    CLLocationDegrees lat = 40.7142;
    CLLocationDegrees lon = -74.0064;
    CLLocation *someLocation = [[CLLocation alloc] initWithLatitude:lat longitude:lon];
    return  someLocation;
}

+(CLLocation*)locationInSanFransisco{
    CLLocationDegrees lat = 37.7750;
    CLLocationDegrees lon = -122.4183;
    CLLocation *someLocation = [[CLLocation alloc] initWithLatitude:lat longitude:lon];
    return  someLocation;
}

+(CLLocation*)locationInBoston{
    CLLocationDegrees lat = 42.3583;
    CLLocationDegrees lon = -71.0603;
    CLLocation *someLocation = [[CLLocation alloc] initWithLatitude:lat longitude:lon];
    return  someLocation;
}

+(CLLocation*)locationInMiami{
    CLLocationDegrees lat = 25.4716;
    CLLocationDegrees lon = -80.1327;
    CLLocation *someLocation = [[CLLocation alloc] initWithLatitude:lat longitude:lon];
    return  someLocation;
}

+(CLLocation*)locationInLondon{
    CLLocationDegrees lat = 51.5171;
    CLLocationDegrees lon = -0.1062;
    CLLocation *someLocation = [[CLLocation alloc] initWithLatitude:lat longitude:lon];
    return  someLocation;
} 

@end

Like this:

Like Loading...
Posted in: iOS | Tagged: Category, CoreLocation, Objective-C, Unit Tests

Copyright © 2019 Rex St John | 雷克斯 聖約翰.

Powered by WordPress and Custom.

%d bloggers like this: