在UITextView中显示纬度和经度,UITextView是UIWindow的子视图:

时间:2016-06-02 10:30:21

标签: objective-c uitextview

我正在尝试编写一个程序,在UITextView中显示计数,纬度和经度,UITextView是UIWindow的子视图。我无法在模拟器屏幕上正确显示信息。任何帮助表示赞赏!

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController () <CLLocationManagerDelegate>
@end
@implementation ViewController
{
    CLLocationManager *locationManager;
    int count;
}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    count++;
    CLLocation *location=locations[0];
    UITextView *textview = [[UITextView alloc] initWithFrame:CGRectMake(20, 10, 280, 30)];
    textview.backgroundColor=[UIColor whiteColor];
    NSString *coun=[NSString stringWithFormat:@"%i",count];
    NSString *lat=[NSString stringWithFormat:@"%f",location.coordinate.latitude];
    NSString *lon=[NSString stringWithFormat:@"%f",location.coordinate.longitude];
   textview.text =(@"%@ %@-%@", coun, lat, lon);
   [self.view addSubview:textview];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    locationManager=[[CLLocationManager alloc] init];
    locationManager.desiredAccuracy=kCLLocationAccuracyBest;
    locationManager.distanceFilter=kCLDistanceFilterNone;
    locationManager.delegate=self;
    [locationManager startUpdatingLocation];
    [locationManager requestWhenInUseAuthorization];
}

1 个答案:

答案 0 :(得分:0)

我希望,以下信息可以帮助您:

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

@interface yourController : UIViewController <CLLocationManagerDelegate> {
    CLLocationManager *locationManager;
    int count;

}

@end

并添加:

    locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    [self.locationManager requestWhenInUseAuthorization];

[locationManager startUpdatingLocation];

回调函数:

    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(@"OldLocation %f %f", oldLocation.coordinate.latitude, oldLocation.coordinate.longitude);
NSLog(@"NewLocation %f %f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
}

将上述纬度和经度值存储在字符串

 NSString *coun=[NSString stringWithFormat:@"%i",count];
NSString *lat=[NSString stringWithFormat:@"%f",newlocation.coordinate.latitude];
NSString *lon=[NSString stringWithFormat:@"%f",newlocation.coordinate.longitude];

然后将其设置为textfield:

   textview.text =(@"%@ %@-%@", coun, lat, lon);

您还必须为(在info.plist中)

添加一个字符串
    [`NSLocationAlwaysUsageDescription`]