使用ios的定位器

时间:2015-07-16 11:39:16

标签: objective-c iphone

这是我的代码如下。 这是我的.h文件

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

@interface ShowmyLocation : UIViewController<CLLocationManagerDelegate>

@property (strong, nonatomic) IBOutlet UILabel *Longitude;
@property (strong, nonatomic) IBOutlet UILabel *Latitude;
@property (strong, nonatomic) IBOutlet UILabel *MyAddress;
- (IBAction)getlocation:(id)sender;
@property (strong, nonatomic) CLLocationManager *locationManager;

@end

这是我的.m文件

#import "ShowmyLocation.h"

@interface ShowmyLocation ()

@end

@implementation ShowmyLocation
{
    //CLLocationManager *locationmanager;
    CLGeocoder *geocoder;
    CLPlacemark *placemark;
}
@synthesize Longitude;
@synthesize Latitude;
@synthesize MyAddress;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.locationManager = [[CLLocationManager alloc] init];
    geocoder = [[CLGeocoder alloc] init];
    self.locationManager.delegate = self;

    if ([self.locationManager respondsToSelector:@selector
         (requestWhenInUseAuthorization)]) {
        [self.locationManager requestWhenInUseAuthorization];
    }
    [self.locationManager startUpdatingLocation];

}

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

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)getlocation:(id)sender {
    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    [self.locationManager startUpdatingLocation];

}

#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"didFailWithError: %@", error);
    UIAlertView *errorAlert = [[UIAlertView alloc]
                               initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [errorAlert show];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(CLLocation *)newLocation
{
    CLLocationCoordinate2D loc = [newLocation coordinate];
    NSLog( @"We're at %f, %f\n", loc.latitude, loc.longitude );
    [self.locationManager stopUpdatingLocation];

    //NSLog(@"location info object=%@", newLocation);
    //CLLocation *currentLocation = newLocation;
    Longitude.text = [NSString stringWithFormat:@"%.8f", loc.latitude];
    Latitude.text = [NSString stringWithFormat:@"%.8f", loc.longitude];


}

我在info.plist文件中包含了requestWhenInUseAuthorization。

当我点击getlocation时,它什么也没有返回。 我正在使用xcode 6.2并在simmulator上运行iphone 6 / ios 8.2

任何人都可以帮助我解决我所缺少的问题。我是IOS开发的新手。

3 个答案:

答案 0 :(得分:0)

希望这有助于你

- (IBAction)getlocation:(id)sender {

    if (nil == locationManager)      
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
    locationManager.distanceFilter = 500; // meters
    [locationManager startUpdatingLocation];

}


   - (void)locationManager:(CLLocationManager *)manager
 didUpdateLocations:(NSArray *)locations {

      CLLocation *newLocation = locations.lastObject;    
      NSLog(@"Latitude===%f ::", newLocation.coordinate.latitude);
      NSLog(@"Longitude===%f ::", newLocation.coordinate.longitude);

}

答案 1 :(得分:0)

我看到您在 viewDidLoad IBAction 中两次调用 startUpdatingLocation 。我认为你可以考虑为你的位置更新创建一种方法,这样你就可以在调用 startUpdatingLocation 之前获得iOS8所需的位置授权部分。它可能看起来像这样:

- (void)startStandardUpdates {

    if (nil == locationManager) {
        locationManager = [[CLLocationManager alloc] init];
    }

    locationManager.delegate = self;

    // This part was added due to a location authorization issue on iOS8
    // See more at: http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/
    if ([self._locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        [self._locationManager requestWhenInUseAuthorization];
    }
    self.mapView.showsUserLocation = YES;

    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    [locationManager startUpdatingLocation];

    CLLocation *currentLocation = locationManager.location;
    if (currentLocation) {
        PAWAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
        appDelegate.currentLocation = currentLocation;
    }

}

还有一件事,当您在模拟器上进行测试时,请尝试调试 - >位置服务。希望这会有所帮助。

答案 2 :(得分:-1)

请尝试以下解决方案,希望这对您有用。

SELECT  DISTINCT 
        Customers.CustomerID, 
        Customers.ContactName AS CustomerContactName, 
        Employees.EmployeeID, 
        Employees.FirstName AS EmployeeFirstName, 
        Employees.LastName AS EmployeeLastName
FROM    Customers
        INNER JOIN Employees
            ON Customers.ContactName LIKE '%'+Employees.FirstName+'%';

谢谢..