【问题标题】:Using CLLocationManager on iOS Simulator to find latitude and longitude [duplicate]在iOS模拟器上使用CLLocationManager查找纬度和经度[重复]
【发布时间】:2014-12-27 06:49:26
【问题描述】:

我正在尝试创建一个使用 MKMapView 打开地图的应用程序,然后通过创建一个委托给 CLLocationManger 来查找用户的当前位置。

我在下面发布了我的代码。我现在遇到的问题是,虽然在打开模拟器时地图确实出现了,但它并没有给出应该通过纬度和经度的 NSLog 的位置或新标题。

我是 Objective - C 和应用程序开发的新手,有人知道我是否缺少 CLLocationManger 的某些协议吗?感谢您的所有帮助。

抱歉代码有点草率,赶紧贴上去。

  #import "ViewController.h"
  #import <MapKit/MapKit.h>
  #import <CoreLocation/CoreLocation.h>

  @interface ViewController () <CLLocationManagerDelegate>
  @property (nonatomic,strong)CLLocationManager * myLocationManger;
  @property (nonatomic,strong) IBOutlet MKMapView *myMapView;
  @end

  @implementation ViewController

  - (void)viewDidLoad {
      [super viewDidLoad];
      NSLog(@"insdie view did load");

     //    Setting the map to be big as the view
      self.myMapView =[[MKMapView alloc]init];

//Initial Property to Map
     self.myMapView =[[MKMapView alloc] init];

//Set type to Standard
self.myMapView.mapType = MKMapTypeStandard;

//Set Mask for AutoReszing
self.myMapView.autoresizingMask = UIViewAutoresizingFlexibleWidth |                                              UIViewAutoresizingFlexibleHeight;

//Add the View!
[self.view addSubview:self.myMapView];

///*Now lets Use my CLLocation Manager to Locate the iPhone's Posistion*////



//Chekcs if Location Services are Enabled
if ([CLLocationManager locationServicesEnabled]) {
    self.myLocationManger = [[CLLocationManager alloc] init];
    self.myLocationManger.delegate = self;

    [self.myLocationManger startUpdatingLocation];



}
else{
    //Location Services are available we will need software to ask to turn this On
    //The user is SOL if they refuse to turn on Location Services
    NSLog(@"Location Services not enabled");


}



}

  -(void)locationManager:(CLLocationManager *)manager
      didUpdateToLocation:(CLLocation *)newLocation
      fromLocation:(CLLocation *)oldLocation
  {
      //This method will show us that we recieved the new location
      NSLog(@"Latitude = %f",newLocation.coordinate.latitude );
      NSLog(@"Longitude =%f",newLocation.coordinate.longitude);

  }

  -(void)locationManager:(CLLocationManager *)manager
   didFinishDeferredUpdatesWithError:(NSError *)error{
      NSLog(@"Error with Updating");
  }



  -(void)locationManager:(CLLocationManager *)manager
  didFailWithError:(NSError *)error
  {
//Failed to recieve user's location
NSLog(@"failed to recived user's locatio");
  }

【问题讨论】:

  • 浏览“位置和地图编程指南”。有一个条目必须添加到您的 Info.plist。

标签: objective-c mkmapview cllocationmanager


【解决方案1】:

我发现在新的 iOS 8 中,您需要将两个键添加到 Plist。这篇文章很好地解释了这个过程。

http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-08
    • 2021-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-10
    相关资源
    最近更新 更多