如何在iPhone应用程序中获取当前位置

时间:2014-02-01 07:22:47

标签: ios iphone objective-c

现在我的代码只是打开地图文件,但我想要当前位置

·H

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


@interface ViewController : UIViewController<CLLocationManagerDelegate>
{
CLLocationManager *locationManager;
CLLocation *currentLocation;
}
@end

的.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    locationManager = [CLLocationManager new];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
}
#pragma mark CLLocationManager Delegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    currentLocation = [locations objectAtIndex:0];
    [locationManager stopUpdatingLocation];
    NSLog(@"Detected Location : %f, %f", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
    CLGeocoder *geocoder = [[CLGeocoder alloc] init] ;
    [geocoder reverseGeocodeLocation:currentLocation
                   completionHandler:^(NSArray *placemarks, NSError *error) {
                       if (error){
                           NSLog(@"Geocode failed with error: %@", error);
                           return;
                       }
                       CLPlacemark *placemark = [placemarks objectAtIndex:0];
                       NSLog(@"placemark.ISOcountryCode %@",placemark.ISOcountryCode);

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

@end

此代码将打开我的地图视图,但它不是我当前的位置 PLZ现在帮助我,我想在我的应用程序中显示我当前的位置

提前致谢

3 个答案:

答案 0 :(得分:0)

获取位置后,按

设置mapview的中心
[mapView setCenterCoordinate: currentLocation.coordinate animated:YES];

答案 1 :(得分:0)

mapView.showsUserLocation = YES;

答案 2 :(得分:0)

    <CoreLocation/CoreLocation.h>

在appdelegate.m中写下以下行

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:       (NSDictionary *)launchOptions{
if (locationManager == nil)
        locationManager = [[CLLocationManager alloc] init];

    locationManager.delegate = self;
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation];
    locationManager.distanceFilter = 500; // meters
    [locationManager startUpdatingLocation];
}

并添加以下方法

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation    *)newLocation fromLocation:(CLLocation *)oldLocation
{
CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error)
 {
     if(placemarks.count)
     {
         NSDictionary *dictionary = [[placemarks objectAtIndex:0] addressDictionary];
         self.strAddress = [NSString stringWithFormat:@"%@", [dictionary valueForKey:@"Street"]];
         if (self.strAddress==(id) [NSNull null] || [self.strAddress length]==0 || [self.strAddress isEqualToString:@""])
         {
             self.strAddress = [NSString stringWithFormat:@"%@", [dictionary valueForKey:@"Name"]];
         }
         if ([dictionary valueForKey:@"City"]==(id) [NSNull null] || [[dictionary valueForKey:@"City"] length]==0)
         {
             self.strAddress = [NSString stringWithFormat:@"%@, %@", strAddress, [dictionary valueForKey:@"State"]];
         }
         else if ([dictionary valueForKey:@"State"]==(id) [NSNull null] || [[dictionary valueForKey:@"State"] length]==0)
         {
             NSArray *arr = [dictionary valueForKey:@"FormattedAddressLines"];
             self.strAddress = [arr objectAtIndex:0];
             for (int i=1; i<arr.count; i++)
             {
                 self.strAddress = [NSString stringWithFormat:@"%@, %@", self.strAddress, [arr objectAtIndex:i]];
             }
         }
         else
         {
             self.strAddress = [NSString stringWithFormat:@"%@, %@", strAddress, [dictionary valueForKey:@"City"]];
         }
     }
 }];
return;

}