ios - 无法获取当前位置以设置 map 中心

标签 ios objective-c mkmapview cllocationmanager

我正在学习使用 map ,我的目标是在应用程序首次加载时在 map 上显示当前位置,而不是显示世界地图。但是我不能让它专注于想要的位置,因为我得到的坐标都是 0。

笔记:

  • 我在 Xcode 中将默认位置设置为日本东京。
  • 所有代码都在 viewDidLoad
  • 我正在使用模拟器

  • 我尝试过使用 2 种方法。

    方法A)我将showsUserLocation设置为YES。然后将 map 的中心设置为 map 用户位置的值。但是用户位置为空,经纬度为0.000000。

    方法 B)我创建位置管理器并从中获取纬度和经度。也得到纬度和经度是0.000000。

    下面是代码:

    View Controller .h
    #import <UIKit/UIKit.h>
    #import <MapKit/MapKit.h>
    #import <CoreLocation/CoreLocation.h>
    
    @interface ViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>
         @property (weak, nonatomic) IBOutlet MKMapView *myMap;
         @property CLLocationManager *locationManager;
    @end
    

    View Controller .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.
    
    //Approach A)
        self.myMap.showsUserLocation = YES; //tokyo Japan, blue pin with ripple displays on Default Location that I set in XCode = Tokyo, Japan
    
        NSLog(@"using MapView.userLocation, user location = %@", self.myMap.userLocation.location); //output = null
        NSLog(@"using MapView.userLocation, latitude = %f", self.myMap.userLocation.location.coordinate.latitude); //output = 0.000000
        NSLog(@"using MapView.userLocation, longitude = %f", self.myMap.userLocation.location.coordinate.longitude); //output = 0.000000
    
        CLLocationCoordinate2D centerCoord = {self.myMap.userLocation.location.coordinate.latitude, self.myMap.userLocation.location.coordinate.longitude};
        [self.myMap setCenterCoordinate:centerCoord animated:TRUE]; //nothing happens, as latitude and longitude = 0.000000
    
    //Approach B)
        // create the location manager
        CLLocationManager *locationManager;
        if (nil == self.locationManager) {
        self.locationManager = [[CLLocationManager alloc] init];
        }
    
        self.locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
        self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
        self.locationManager.delegate = self; //add as suggested
        [self.locationManager startUpdatingLocation];
    
        NSLog(@"using LocationManager:current location latitude = %f, longtitude = %f", self.locationManager.location.coordinate.latitude, self.locationManager.location.coordinate.longitude);
    
        //if getting latitude and longitude, will call "setCenterCoordinate"
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
      //add as suggested
    -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
    {    
         NSLog(@"didUpdateToLocation:Latitude :  %f", newLocation.coordinate.latitude);
         NSLog(@"didUpdateToLocation:Longitude :  %f", newLocation.coordinate.longitude);
    
         [self.locationManager stopUpdatingLocation];
    }    
       @end
    

    输出:
    xxxx using MapView.userLocation, user location = (null)
    xxxx using MapView.userLocation, latitude = 0.000000
    xxxx using MapView.userLocation, longitude = 0.000000
    xxxx using LocationManager:current location latitude = 0.000000, longtitude = 0.000000
    

    现在输出:
    xxx using MapView.userLocation, user location = (null)
    xxx using MapView.userLocation, latitude = 0.000000
    xxx using MapView.userLocation, longitude = 0.000000
    xxx using LocationManager:current location latitude = 0.000000, longtitude = 0.000000
    xxx didUpdateToLocation:Latitude :  35.702069
    xxx didUpdateToLocation:Longitude :  139.775327
    

    最佳答案

    添加 CoreLocation.framework给你的项目并添加到.h文件
    并且

    locationManager.delegate = self; // may be you forget to it.
    

    并且还添加了 Delegate 方法,例如,
    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
    {   
    
        NSLog(@"Latitude :  %f", newLocation.coordinate.latitude);
        NSLog(@"Longitude :  %f", newLocation.coordinate.longitude);
    
        [currentLocation stopUpdatingLocation];
    }
    

    关于ios - 无法获取当前位置以设置 map 中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18628573/

    相关文章:

    ios - 在 NSMutableString 上使用 substringFromIndex

    objective-c - 从2个或更多MKPolygon对象的交集创建MKPolygon

    ios - 检测一个点是否在 MKPolygon 叠加层内

    ios - 如果单元格有按钮,我们如何获得单元格的行号和节号

    ios - RxSwift 单元测试

    objective-c - 从 CSV 文件中读取并在 Objective-C Xcode 中创建数组

    ios - 将 mysql 查询转移到 NSPredicate

    ios - 识别在iOS循环中创建的UIImageView和UITextView

    ios - 如何更改 iOS "UITableView negative space"颜色?

    ios - 来自 MPMapView 的奇怪行为