iOS 5返回错误的用户位置,而iOS 4则没有

时间:2011-12-10 10:17:16

标签: ios5

我有3台iPad:

iPad 3G / WiFi iOS 5.0.1

iPad 2 3G / WiFi iOS 4.3.5

iPad 2 WiFi iOS 5.0.1

在所有3台iPad上运行相同的代码导致我的位置在运行iOS 5.0.1的iPad上报告为“纽约市”,而运行iOS 4.3.5的iPad则返回正确的位置。

可能是什么原因?

谢谢!

已添加代码:

//  AppDelegate.h
//  Created by Michael Superczynski on 8/27/11.
//  Copyright 2011 HyperNova Software. All rights reserved.

#import "Locator.h"
#import <UIKit/UIKit.h>

@interface AppDelegate : NSObject <UIApplicationDelegate>

{
    CLLocationManager *locationManager;
    Locator *locator;
}

@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) Locator *locator;

@end

//  AppDelegate.m
//  Created by Michael Superczynski on 8/27/11.
//  Copyright 2011 HyperNova Software. All rights reserved.

#import "AppDelegate.h"
#import "Locator.h"
#import <MapKit/MapKit.h>

@implementation AppDelegate

@synthesize locationManager;
@synthesize locator;
. . .

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{   
    Locator *aLocator = [[Locator alloc]init];
    locator = aLocator;
CLLocationManager *aLocationManager = [[CLLocationManager alloc] init];
locationManager = aLocationManager;
locationManager.delegate = locator;
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
locationManager.distanceFilter = 100;
locationManager.purpose = @"This will show your location on the map";
[locator startStandardUpdates];
. . .
}

//
//  Locator.h
//
//  Created by Michael Superczynski on 8/30/11.
//  Copyright 2011 HyperNova Software. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface Locator : NSObject <CLLocationManagerDelegate, MKReverseGeocoderDelegate>
{
    NSString *currentLocation;
    CLLocation *currentCLLocation;
    CLLocationManager *locationManager;
}

@property (nonatomic, strong) NSString *currentLocation;
@property (nonatomic, strong) CLLocation *currentCLLocation;
@property (nonatomic, strong) CLLocationManager *locationManager;

-(void)reverseGeocodeCurrentLocation:(CLLocation *)location;
-(void)startStandardUpdates;

@end

//
//  Locator.m
//
//  Created by Michael Superczynski on 8/30/11.
//  Copyright 2011 HyperNova Software. All rights reserved.
//

#import "AppDelegate.h"
#import <CoreLocation/CoreLocation.h>
#import "Locator.h"

@implementation Locator

@synthesize currentLocation;
@synthesize currentCLLocation;
@synthesize locationManager;

-(id)init
{
    self = [super init];
    if (self) 
    {
        // Initialization code here.
    }
    return self;
}

-(void)startStandardUpdates
{
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [CLLocationManager locationServicesEnabled];
    currentCLLocation = [[CLLocation alloc]init];
    [locationManager startUpdatingLocation];
    float latitude;
    float longitude; 
    if (![CLLocationManager locationServicesEnabled])
    {
        latitude = kDefaultLocationLatitudeA;
        longitude = kDefaultLocationLongitudeA; 
    }
    else
    {
        latitude = (float)locationManager.location.coordinate.latitude;
        longitude = (float)locationManager.location.coordinate.longitude;
    }
    CLLocation *location = [[CLLocation alloc]initWithLatitude:latitude longitude:longitude];
    [self reverseGeocodeCurrentLocation:location];
}

// Delegate method from the CLLocationManagerDelegate protocol.
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
       fromLocation:(CLLocation *)oldLocation
{
    NSDate* eventDate = newLocation.timestamp;
    currentCLLocation = newLocation;
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
    newLocation.coordinate.latitude,
    newLocation.coordinate.longitude] argument:nil]; 
    // If it's a relatively recent event, turn off updates to save power
    if ((abs((int)(howRecent))) < 30.0)
        [self reverseGeocodeCurrentLocation:newLocation];
}

-(void)reverseGeocodeCurrentLocation:(CLLocation *)location
{
    MKReverseGeocoder *reverseGeocoder = [[MKReverseGeocoder alloc]initWithCoordinate:location.coordinate];
    reverseGeocoder.delegate = self;
    [reverseGeocoder start];    
}

-(void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark    
{
    self.currentLocation = [NSString stringWithFormat:@"%@, %@", placemark.administrativeArea, placemark.country];
}

-(void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error 
{
    if ([error code] != kCLErrorLocationUnknown) 
        [locationManager stopUpdatingLocation];
    float latitude = kDefaultLocationLatitudeA;
    float longitude = kDefaultLocationLongitudeA; 
    CLLocation *cLLocation = [[CLLocation alloc]initWithLatitude:latitude longitude:longitude];
}

-(void)reverseGeocoder:(MKReverseGeocoder *)geocoder monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error 
{
    if ([error code] != kCLErrorLocationUnknown) 
        [locationManager stopUpdatingLocation];
}
@end

0 个答案:

没有答案