地理位置仅显示全局地图

时间:2011-03-28 18:19:51

标签: iphone geolocation

我尝试完全按照本教程中的说明对我的iPhone应用进行了调整:http://www.youtube.com/watch?v=qNMNRAbIDoU 我的问题是当我建立并去看地图但地理位置服务没有跟踪我的位置,所以我只看到全球地图:( 这是我的代码,希望你能帮我识别问题:

appdelegate.h

    @interface WhereAmIAppDelegate : NSObject <UIApplicationDelegate,CLLocationManagerDelegate> {
    UIWindow *window;
    WhereAmIViewController *viewController;
    CLLocationManager *locationManager;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) CLLocationManager *locationManager; 
@property (nonatomic, retain) IBOutlet WhereAmIViewController *viewController;

appdelegate.m:

@implementation WhereAmIAppDelegate

@synthesize window;
@synthesize viewController;
@synthesize locationManager;

#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch.



    self.locationManager=[[[CLLocationManager alloc] init] autorelease];

    if([CLLocationManager locationServicesEnabled]) {
        self.locationManager.delegate=self;
        self.locationManager.distanceFilter=100;
        [self.locationManager startUpdatingLocation];

    }



    // Add the view controller's view to the window and display.
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];

    return YES;
}

- (void)dealloc {
    [viewController release];
    [window release];
    [super dealloc];
}


#pragma mark CLLocationManagerDelegate Methods


-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation    {

    MKCoordinateSpan span;
    span.latitudeDelta=0.2;
    span.longitudeDelta=0.2;


    MKCoordinateRegion region;
    region.span=span;
    region.center=newLocation.coordinate;
    [viewController.mapView setRegion:region animated:YES];

    viewController.mapView.showsUserLocation=YES;
    viewController.latitude.text=[NSString stringWithFormat:@"%f",newLocation.coordinate.latitude];

    viewController.longitude.text=[NSString stringWithFormat:@"%f",newLocation.coordinate.longitude];
}
@end

MyViewController.h

@interface WhereAmIViewController : UIViewController {

    MKMapView  *mapView;
    UILabel    *latitude;
    UILabel    *longitude;


}

@property (nonatomic,retain)IBOutlet MKMapView *mapView;
@property (nonatomic,retain)IBOutlet UILabel *latitude;
@property (nonatomic,retain)IBOutlet UILabel *longitude;

myViewController.m:

@implementation WhereAmIViewController



@synthesize mapView;
@synthesize latitude;
@synthesize longitude;

- (void)dealloc {


    [mapView release];
    [latitude release];
    [longitude release];
    [super dealloc];
}

@end

2 个答案:

答案 0 :(得分:1)

要检查的两件事:
1)模拟器没有位置,它将始终显示加利福尼亚州Apple总部的纬度/经度 2)这个位置需要一点时间才能得到解决,所以如果你立刻要求位置,你就不会得到一个。

此外,您需要在WhereAmI视图控制器中添加更多代码。通常,您会在viewWillAppear中看到一些内容。视图控制器和地图视图上的Here is a link to a Big Nerd Ranch tutorial。也许它会帮助你。

答案 1 :(得分:1)

这里有几点需要注意:

1)iPhone模拟器没有GPS功能,但它的默认位置是1 Infinite Loop,Cupertino,CA(美国)

2)您的委托方法应为didUpdateFromLocation,而不是didUpdateToLocation