0

私は RootViewController (これは私の RootViewController.h のコードです) クラスでユーザーの地理位置情報を計算する iPhone 用のナビゲーション ベースのアプリを構築しています。

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

}

マップがユーザーの位置を中心にして、適切なスパン値も選択するようにしたいと思います。

4

1 に答える 1

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

}
于 2011-01-18T21:38:29.683 に答える