开发者

How to get user's location in a later screen for the MapKit?

I am building a navigation-based app for the iPhone, where I calculate the user's geolocation in the RootViewController (this is the code for my RootViewController.h) class:

#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;

here is the code for my RootViewController.m class:

- (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];

Because I am calculating the user's geolocation in th开发者_JS百科e RootViewController class, I would like to re-use these value when I use the MapKit later on in the application in the MapViewController.m class:

- (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];

}

I would like the map to be centered on the user's location, and choose appropriate span values as well.


There are a lot of ways to do this, the way I've done it is to store the location in NSUserDefaults and access that value later when I'm presenting my map. The benefit of doing it this way is that the user's location persists between runs, so if the next time they open the app they're having trouble getting a GPS signal, you still have a location to display.

This code may not be 100%, i'm writing it from memory:

#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]];

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜