文档库 最新最全的文档下载
当前位置:文档库 › DC_GPS

DC_GPS

DC_GPS
DC_GPS

#pragma mark - == GPS == -

#import

#import

#define REFRESH_INTERVAL 10

#define LOCATION_WITH_MAP

@interface GPS : NSObject { NSInteger getCityNext;

BOOL requestFail;

BOOL requestFinish;

NSDate * refreshTime;

CLLocationManager * location;

NSMutableData * receivedData;

CLLocationCoordinate2D coordinate;

NSString * fullAddress;

NSString * streetNumber;

NSString * route;

NSString * city;

NSString * province;

NSString * postalCode;

NSString * country;

#ifdef LOCATION_WITH_MAP

MKMapView * jexMapView;

#endif

}

@property BOOL requestFinish;

@property BOOL requestFail;

@property CLLocationCoordinate2D coordinate;

@property (nonatomic, retain) NSString * fullAddress;

@property (nonatomic, retain) NSString * streetNumber;

@property (nonatomic, retain) NSString * route;

@property (nonatomic, retain) NSString * city;

@property (nonatomic, retain) NSString * province;

@property (nonatomic, retain) NSString * postalCode;

@property (nonatomic, retain) NSString * country;

DECLARE_SINGLETON_FOR_CLASS(GPS)

+ (BOOL)isEnabel;

- (void)refresh:(BOOL)getCity;

- (void)requestForCity;

- (void)ignoreProvinceName;

- (NSString *)provinceAndCity;

@end

@implementation GPS

@synthesize requestFinish, requestFail;

@synthesize coordinate, fullAddress, streetNumber, route, city, province, postalCode, country;

SYNTHESIZE_SINGLETON_FOR_CLASS(GPS);

+ (BOOL)isEnabel {

return [CLLocationManager locationServicesEnabled];

}

- (id)init {

if ((self = [super init])) {

location = [[CLLocationManager alloc] init];

location.delegate = self;

location.distanceFilter = 100;

location.desiredAccuracy = kCLLocationAccuracyBestForNavigation;

getCityNext = 0;

requestFinish = NO;

requestFail = NO;

refreshTime = [[NSDate alloc] initWithTimeIntervalSinceNow:0];

#ifdef LOCATION_WITH_MAP

jexMapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 480, 320)];

jexMapView.mapType = MKMapTypeStandard;

jexMapView.showsUserLocation = YES;

jexMapView.delegate = self;

#endif

}

return self;

}

- (void)dealloc {

RELEASE(refreshTime);

RELEASE(fullAddress);

RELEASE(streetNumber);

RELEASE(route);

RELEASE(city);

RELEASE(province);

RELEASE(postalCode);

RELEASE(country);

#ifdef LOCATION_WITH_MAP

RELEASE(jexMapView);

#endif

[super dealloc];

}

- (void)refresh:(BOOL)getCity {

if (location && ([refreshTime timeIntervalSinceNow] <= 0

|| (FLOAT_EQUAL(https://www.wendangku.net/doc/ae6284448.html,titude, 0) &&

FLOAT_EQUAL(coordinate.longitude, 0)))) {

getCityNext = (getCity ? 1 : 0);

requestFinish = NO;

requestFail = NO;

[location startUpdatingLocation];

}

}

- (void)requestForCity {

#ifdef GPS_LOCAL

NSString * urlString = [NSString stringWithFormat:@"http://

https://www.wendangku.net/doc/ae6284448.html,/maps/api/geocode/json?latlng=%f,%f&sensor=true",

https://www.wendangku.net/doc/ae6284448.html,titude, coordinate.longitude];

NSURL * requestURL = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

NSURLRequest * geocodingRequest=[NSURLRequest requestWithURL:requestURL cachePolicy:NSURLRequestUseProtocolCachePolicy

timeoutInterval:60.0];

// NOTE: create connection and start downloading data

NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest:geocodingRequest delegate:self];

if (connection) {

receivedData = [[NSMutableData data] retain];

getCityNext = 2;

} else {

CCLOG(@"Request for city fail");

}

#else

getCityNext = 2;

requestFinish = YES;

#endif

}

/*!

@function

@abstract忽略“省”,“市”

@return void

*/

- (void)ignoreProvinceName {

if ([province hasSuffix:@"省"] || [province hasSuffix:@"市"]) {

province = [[province substringWithRange:NSMakeRange(0, [province length] - 1)] copy];

}

}

- (NSString *)provinceAndCity {

return [NSString stringWithFormat:@"%@ %@", province, city];

}

#pragma mark -

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation: (CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { CCLOG(@"GPS Location: {%f, %f}", https://www.wendangku.net/doc/ae6284448.html,titude, newLocation.coordinate.longitude);

#ifdef LOCATION_WITH_MAP

jexMapView.showsUserLocation = YES;

#else

coordinate = newLocation.coordinate;

if ([CURRENT_DEVICE isSimulator]) {

https://www.wendangku.net/doc/ae6284448.html,titude = 23.129163;

coordinate.longitude = 113.264435;

}

if (getCityNext > 0) {

if (getCityNext == 1) {

[self requestForCity];

}

} else {

requestFinish = YES;

}

[location stopUpdatingLocation];

RELEASE(refreshTime);

refreshTime = [[NSDate alloc] initWithTimeIntervalSinceNow:REFRESH_INTERVAL]; #endif

}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{

CCLOG(@"Failed to find location!");

requestFail = YES;

requestFinish = YES;

}

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation

*)userLocation {

CCLOG(@"Map Location: {%f, %f}", https://www.wendangku.net/doc/ae6284448.html,titude, userLocation.location.coordinate.longitude);

coordinate = userLocation.location.coordinate;

if ([CURRENT_DEVICE isSimulator]) {

https://www.wendangku.net/doc/ae6284448.html,titude = 23.129163;

coordinate.longitude = 113.264435;

}

if (getCityNext > 0) {

if (getCityNext == 1) {

[self requestForCity];

}

} else {

requestFinish = YES;

}

[location stopUpdatingLocation];

RELEASE(refreshTime);

refreshTime = [[NSDate alloc] initWithTimeIntervalSinceNow:REFRESH_INTERVAL];

jexMapView.showsUserLocation = NO;

}

- (void)mapView:(MKMapView *)mapView didFailToLocateUserWithError:(NSError

*)error {

CCLOG(@"Failed to locate user!");

requestFail = YES;

requestFinish = YES;

jexMapView.showsUserLocation = NO;

}

- (void)connection:(NSURLConnection *)connection didReceiveResponse: (NSURLResponse *)response {

[receivedData setLength:0];

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [receivedData appendData:data];

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError

*)error {

CCLOG(@"Connection failed! Error - %@ %@",

[error localizedDescription], [[error userInfo]

objectForKey:NSURLErrorFailingURLStringErrorKey]);

RELEASE(connection);

RELEASE(receivedData);

requestFail = YES;

requestFinish = YES;

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSDictionary * resultDict = [JSON_READ deserializeAsDictionary:receivedData error:nil];

NSString * status = [resultDict valueForKey:@"status"];

if ([status isEqualToString:@"OK"]) {

// NOTE: get first element as array

NSArray * firstResultAddress = [[[resultDict objectForKey:@"results"] objectAtIndex:0] objectForKey:@"address_components"];

streetNumber = [Jex addressComponent:@"street_number"

inAddressArray:firstResultAddress ofType:@"long_name"];

route = [Jex addressComponent:@"route"inAddressArray:firstResultAddress ofType:@"long_name"];

city = [Jex addressComponent:@"locality"

inAddressArray:firstResultAddress ofType:@"long_name"];

province = [Jex addressComponent:@"administrative_area_level_1" inAddressArray:firstResultAddress ofType:@"short_name"];

postalCode = [Jex addressComponent:@"postal_code"

inAddressArray:firstResultAddress ofType:@"short_name"];

country = [Jex addressComponent:@"country"

inAddressArray:firstResultAddress ofType:@"long_name"];

} else {

CCLOG(@"Connection failed: %@", status);

requestFail = YES;

}

requestFinish = YES;

}

@end

相关文档