如何在MapKit的后续屏幕中获取用户的位置?

时间:2011-01-18 21:09:48

标签: iphone objective-c mapkit cllocation

我正在为iPhone构建一个基于导航的应用程序,我在RootViewController中计算用户的地理位置(这是我的RootViewController.h的代码)类:

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import "SecondViewController.h"
#import <sqlite3.h>

@interface RootViewController : UITableViewController <CLLocationManagerDelegate>{


    SecondViewController *secondViewController;
    NSUInteger numUpdates;

    CLLocationManager *lm;
    CLLocation *firstLocation;
    CLLocation *secondLocation;

}

@property (nonatomic, retain) SecondViewController *secondViewController;
@property (nonatomic, retain) NSMutableArray *restaurants;
@property (nonatomic, retain) NSArray *sortedRestaurants;

这是我的RootViewController.m类的代码:

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

CLLocationCoordinate2D location;

NSString *lat = [[NSString alloc] initWithFormat:@"%g", newLocation.coordinate.latitude];
double lat1 = newLocation.coordinate.latitude;
location.latitude = newLocation.coordinate.latitude; //setting the latitude property of the location variable

NSString *lng = [[NSString alloc] initWithFormat:@"%g", newLocation.coordinate.longitude];
double lon1 = newLocation.coordinate.longitude;
location.longitude = newLocation.coordinate.longitude; //setting the longitude property of the location variable

MapViewController *mController = [[MapViewController alloc] initWithNibName:@"MapViewController" bundle:[NSBundle mainBundle]];
self.mapViewController = mController;
[mController release];

self.mapViewController.userCoord = [[[CLLocation alloc] initWithLatitude:location.latitude longitude:location.longitude] autorelease];
//in the above line I get the error, "incompatible type for argument 1 of 'setUserCoord' "      
[lm stopUpdatingLocation];

因为我在RootViewController类中计算用户的地理位置,所以当我稍后在MapViewController.m类的应用程序中使用MapKit时,我想重新使用这些值:

- (void)viewDidLoad {

    [super viewDidLoad];
MKCoordinateRegion region;
MKCoordinateSpan span;
    region.center = userCoord; 
/* userCoord is declared as an object of type CLLocationCoordinate2D
   in MapViewController.h, and is declared as a property, and then
   synthesized in the .m class.  Even though I am trying to center
   the map on the user's coordinates, the map is neither centering,
   nor zooming to my settings. */

    span.latitudeDelta = 0.2;
span.longitudeDelta = 0.2;
region.span = span;

[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
mapView.showsUserLocation = YES;
[mapView setRegion:region animated:YES];

    RestaurantAnnotation *rAnnotation = [[RestaurantAnnotation alloc] init];
rAnnotation.title = restaurantObj.name;  
rAnnotation.subTitle = restaurantObj.address;

    CLLocationCoordinate2D newCoord = {restaurantObj.latitude, restaurantObj.longitude};
rAnnotation.coordinate = newCoord;
[mapView addAnnotation:rAnnotation];

}

我希望地图以用户的位置为中心,并选择合适的跨度值。

1 个答案:

答案 0 :(得分:1)

有很多方法可以做到这一点,我这样做的方法是将位置存储在NSUserDefaults中,稍后在我展示地图时访问该值。这样做的好处是用户的位置在运行之间持续存在,因此如果他们下次打开应用程序时他们无法获得GPS信号,您仍然可以显示位置。

此代码可能不是100%,我是从内存中写的:

#define latKey @"latKey"
#define lonKey @"lonKey"
#define locationKey @"locationKey"

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

    NSNumber *lat = [NSNumber numberWithDouble:newLocation.coordinate.latitude];
    NSNumber *lon = [NSNumber numberWithDouble:newLocation.coordinate.longitude];

    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:lat, latKey, lon, lonKey, nil];

    [[NSUserDefaults standardUserDefaults] addObjectForKey:locationKey];

}

- (void)viewDidLoad {

    NSDictionary *dict = [[NSUserDefaults standardUserDefaults] dictForKey:locationKey];
    CLLocation *location = [[CLLocation alloc] initWithLatitude:[[dict objectForKey:lat] doubleValue] longitude:[[dict objectForKey:lon] doubleValue]];

}