【问题标题】:how to get current location in iPhone app如何在 iPhone 应用程序中获取当前位置
【发布时间】:2014-02-01 18:22:08
【问题描述】:

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

.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

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

提前致谢

【问题讨论】:

  • 您在设备或模拟器中运行?现在检测到的位置是哪个?
  • CoreLocation 仅适用于设备!

标签: ios iphone objective-c


【解决方案1】:

获取位置后,设置地图视图的中心

[mapView setCenterCoordinate: currentLocation.coordinate animated:YES];

【讨论】:

  • 感谢回答,但获取我在 Xib 或故事板中的当前位置是否可能
【解决方案2】:

设置

mapView.showsUserLocation = YES;

【讨论】:

    【解决方案3】:
        <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;
    

    }

    【讨论】:

      猜你喜欢
      • 2012-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多