0

だから私はiOS開発に不慣れで、現在のGPS座標でラベルを更新しようとしています。コンパイルに問題はありませんが、座標が0.00000, 0.00000.

私の .h ファイルのコードは次のとおりです。

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>


@interface ViewController : UIViewController{
    IBOutlet CLLocationManager *locationManager;
    IBOutlet UILabel *location;
}

//@property (nonatomic, retain) IBOutlet CLLocationManager *locationManager;
//@property (weak, nonatomic) IBOutlet UILabel *location;
@end

私の .m ファイルのコードは次のとおりです。

@implementation ViewController

- (void) updateLabel
{
    NSObject *latitude = [NSString stringWithFormat:@"%f", locationManager.location.coordinate.latitude];
    NSObject *longitude = [NSString stringWithFormat:@"%f", locationManager.location.coordinate.longitude];

    location.text = [NSString stringWithFormat: @"%@,%@", latitude, longitude];
}

- (void)viewDidLoad
{

    locationManager = [[CLLocationManager alloc] init];
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
    [self updateLabel];
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
4

4 に答える 4

1

ViewDidLoadで、このように一度試してください:

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = (id)self;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
    [locationManager startUpdatingLocation];
    [self updateLabel];

このデリゲート メソッドを使用しないと、値が 0 になります。

 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
   //No need to do any code...
   // NSLog(@"Got location %f,%f", newLocation.coordinate.latitude,   newLocation.coordinate.longitude);

 }

ラベル メソッドの更新中:

- (void) updateLabel
{
   //Getting Current Latitude and longitude..

    CLLocation *location = [locationManager location];
    float longitude=location.coordinate.longitude;
    float latitude=location.coordinate.latitude;
    NSLog(@"latitude,longitudes are >> %f,%f",latitude,longitude);
    locationlabel.text =  [NSString stringWithFormat:@"%f,%f",longitude,latitude];


  } 
于 2013-03-29T05:39:50.433 に答える
1

それを修正しました:

私はデリゲート メソッドを実装しておらず、[locationManager startUpdatingLocation] も実装していませんでした。今、私はよく知っています。

.h ファイル:

@interface MapViewController : UIViewController <CLLocationManagerDelegate>
@property (nonatomic, retain) IBOutlet CLLocationManager *locationManager;
@property (strong, nonatomic) IBOutlet UILabel *location;
@end

.m ファイル:

- (void) updateCurrentLabel
{
    NSObject *latitude = [NSString stringWithFormat:@"%f", locationManager.location.coordinate.latitude];
    NSObject *longitude = [NSString stringWithFormat:@"%f", locationManager.location.coordinate.longitude];

    self.location.text = [NSString stringWithFormat: @"Current Location: %@,%@", latitude, longitude];
}

- (void)viewDidLoad
{
    [self getCurrentLocation];
    [super viewDidLoad];
}
-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    [self updateCurrentLabel];
}

-(void) getCurrentLocation
{
    self.locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    [locationManager startUpdatingLocation];
}

私がどれほど初心者だったかを指摘してくれてありがとう。理解した。みんなありがとう!

于 2013-03-28T21:58:56.333 に答える
0

location をシングルトンとして使用し、値をデフォルトの user に保存するのは魅力的です。私は若いプログラマーで、すべてをコーディングしようとしています。私はこれを次のように使用します (このコードはまだリファクタリングする必要があり、alertUserWithTitle: はユーザーに警告する NYMessageToUser のクラス メソッドです):

//##Header file:
@interface NYLocationManager : NSObject<CLLocationManagerDelegate>
{
    CLLocationManager *locationManager;
    float lonngitude;
    float latitude;
    float altitude;
}
@property(nonatomic,retain)CLLocationManager *locationManager;
@property(nonatomic,readwrite)float longitude;
@property(nonatomic,readwrite)float latitude;
@property(nonatomic,readwrite)float altitude;

+(NYLocationManager *) getInstance;

-(void)startUpdatingLocation;
-(void)stopUpdatingLocation;
-(double)getDistanceFromUserLocationToCordinatesLatitude:(float)lat Longitude:(float)lon;

@end




//### implementation file: 

@implementation NYLocationManager

@synthesize locationManager;
@synthesize latitude;
@synthesize longitude;
@synthesize altitude;

+ (id)getInstance
{
    static NYLocationManager *Instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Instance = [[self alloc] init];
    });
    [Instance startUpdatingLocation];
    return Instance;
}

- (id)init
{
    if (self = [super init])
    {

        latitude    =0.0;
        longitude   =0.0;
        altitude    =0.0;
        if([[NSUserDefaults standardUserDefaults] objectForKey:@"locationLongitude"] != nil)
        {
            NSUserDefaults *savedLocation=[NSUserDefaults standardUserDefaults];
            latitude    =[[savedLocation objectForKey:@"locationLatitude"] floatValue];
            longitude   =[[savedLocation objectForKey:@"locationLongitude"] floatValue];
            altitude    =[[savedLocation objectForKey:@"locationAltitude"] floatValue];
        }
        locationManager = [[CLLocationManager alloc] init];
        locationManager.distanceFilter  = kCLDistanceFilterNone;
        locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
        locationManager.delegate        = self;
        if ([CLLocationManager locationServicesEnabled])
        {
            [locationManager startUpdatingLocation];
        } else
        {
            [NYMessageToUser  alertUserWithTitle:@"Location Services is Disabled!!!" withMessage:@"This app is designed to share images with location, Please enable location for this app and relucnh the app"];
        }
    }
    return self;
}

- (void)dealloc
{
    // Should never be called, but just here for clarity really.
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *loc =[locations lastObject];
    self.longitude  =loc.coordinate.longitude;
    self.latitude   =loc.coordinate.latitude;
    self.altitude   =loc.altitude;
    NSUserDefaults *savedLocation=[NSUserDefaults standardUserDefaults];
    [savedLocation setObject: [NSString stringWithFormat:@"%f", self.longitude] forKey:@"locationLongitude"];
    [savedLocation setObject: [NSString stringWithFormat:@"%f", self.latitude] forKey:@"locationLatitude"];
    [savedLocation setObject: [NSString stringWithFormat:@"%f", self.altitude ] forKey:@"locationAltitude"];
    [savedLocation synchronize];

    [locationManager stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    [locationManager stopUpdatingLocation];
    [NYMessageToUser alertUserWithTitle:@"Location Error!!!" withMessage:@"This app is designed to use with valid location, Please enable location for this app and relucnh the app"];
}
-(void)startUpdatingLocation
{
    if ([CLLocationManager locationServicesEnabled])
    {
        [locationManager startUpdatingLocation];
    } else
    {
        [NYMessageToUser alertUserWithTitle:@"Location Services is Disabled!!!" withMessage:@"This app is designed to share images with location, Please enable location for this app and relucnh the app"];
    }
}

-(void)stopUpdatingLocation
{
    [locationManager stopUpdatingLocation];
}
-(double)getDistanceFromUserLocationToCordinatesLatitude:(float)lat Longitude:(float)lon
{
    CLLocation *locA = [[CLLocation alloc] initWithLatitude:self.latitude longitude:self.longitude];

    CLLocation *locB = [[CLLocation alloc] initWithLatitude:lat longitude:lon];

    CLLocationDistance distance = [locA distanceFromLocation:locB];
    return distance;
}
@end 




//### How to use



NYLocationManager *loc  =[NYLocationManager getInstance];
 NSLog(@"longitude: %f, latitude: %f, altitude: %f",loc.longitude,loc.latitude,loc.altitude);
于 2014-05-19T13:19:49.270 に答える
0

を使用する代わりにlocationManager.location.coordinate.latitude、タイプのインスタンス変数を保持しますCLLocationCoordinate2D。のように呼ぶことができますcurrentLocation。次に、デリゲート メソッドで値を取得したら、の値をlocationManager:didUpdateLocations:設定しますcurrentLocation

そのデリゲートも呼び出し[locationManager startUpdatingLocation]て設定する必要があります (そのデリゲート メソッドを実装するだけでなく)。

あなたが現在ロケーション マネージャーを使用している方法は間違っています。チュートリアルに従って基本を理解した方がよいと思います。

于 2013-03-28T21:16:49.600 に答える