Wie der Standort des Nutzers in einem späteren Bildschirm für die MapKit bekommen?
-
12-10-2019 - |
Frage
Ich bin der Aufbau eine Navigations-basierte Anwendung für das iPhone, wo ich den Benutzers Geolocation im RootViewController berechnen (dies ist der Code für meinen RootViewController.h ist) Klasse:
#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;
Hier ist der Code für meine RootViewController.m Klasse:
- (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];
Weil ich den Benutzers Geolocation im RootViewController Klasse bin zu berechnen, würde Ich mag diese Werte wieder verwenden, wenn ich die MapKit verwende später in der Anwendung in der MapViewController.m Klasse:
- (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];
}
Ich würde die Karte wie auf dem Standort des Benutzers zentriert werden, und auch entsprechende Spanne Werte wählen.
Lösung
Es gibt viele Möglichkeiten, dies zu tun, so wie ich es getan habe, ist die Lage in NSUserDefaults
und den Zugang dieser Wert später zu speichern, wenn ich meine Karte präsentiere. Der Vorteil der es auf diese Weise tun, ist, dass die Lage verharrt des Benutzers zwischen den Läufen, also, wenn sie das nächste Mal die App öffnen sie Probleme haben, bekommen ein GPS-Signal, können Sie immer noch einen Ort Anzeige haben.
Dieser Code kann nicht 100% sein, Ich schreibe es aus dem Speicher:
#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]];
}