MapKitの後の画面でユーザーの位置を取得するにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/4729039

質問

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

}

マップをユーザーの場所に集中させ、適切なスパン値も選択したいと思います。

役に立ちましたか?

解決

これを行う方法はたくさんあります。私がやった方法は、場所を保存することです 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]];

}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top