Ios 无法获取设置地图中心的当前位置

Ios 无法获取设置地图中心的当前位置,ios,objective-c,mkmapview,cllocationmanager,Ios,Objective C,Mkmapview,Cllocationmanager,我正在学习使用地图,我的目标是在应用程序首次加载时在地图上显示当前位置,而不是显示世界地图。但是我不能让它像我想要的那样聚焦在这个位置上,因为我得到的坐标都是0 注: 我已经在Xcode中将默认位置设置为日本东京 所有代码都在viewDidLoad中 我在用模拟器 我尝试了两种方法 方法A)我将showsUserLocation设置为“是”。然后将地图中心设置为地图用户位置的值。但用户位置为空,纬度和经度为0.000000 方法B)我创建位置管理器并从中获取纬度和经度。同时获得的纬度和经度为0.

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

注:

  • 我已经在Xcode中将默认位置设置为日本东京
  • 所有代码都在viewDidLoad中
  • 我在用模拟器
  • 我尝试了两种方法

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

    方法B)我创建位置管理器并从中获取纬度和经度。同时获得的纬度和经度为0.000000

    代码如下:

    ViewController.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
    
    输出:

    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.
    
    并添加委托方法,如

    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
    {   
    
        NSLog(@"Latitude :  %f", newLocation.coordinate.latitude);
        NSLog(@"Longitude :  %f", newLocation.coordinate.longitude);
    
        [currentLocation stopUpdatingLocation];
    }
    

    CoreLocation.framework
    添加到项目中,并添加到
    .h
    文件中 而且

    locationManager.delegate = self; // may be you forget to it.
    
    并添加委托方法,如

    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
    {   
    
        NSLog(@"Latitude :  %f", newLocation.coordinate.latitude);
        NSLog(@"Longitude :  %f", newLocation.coordinate.longitude);
    
        [currentLocation stopUpdatingLocation];
    }
    
    你可以先定

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate =self;
    [locationManager startUpdatingLocation];
    
    然后按照下面的方法在当前位置获取坐标

    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    
        [manager stopUpdatingLocation];
        locationManager.delegate = nil;
        locationManager = nil;
    
        CLLocation *currentLocation = newLocation;
    
    
    }
    
    你可以先定

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate =self;
    [locationManager startUpdatingLocation];
    
    然后按照下面的方法在当前位置获取坐标

    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    
        [manager stopUpdatingLocation];
        locationManager.delegate = nil;
        locationManager = nil;
    
        CLLocation *currentLocation = newLocation;
    
    
    }
    


    试用设备。并在设备上使用0.0000000.00000 lat和logTry打印NSLOG。打印NSLOG有时模拟器使用0.0000000.00000 lat,logI现在添加了这一行,仍然得到0.000000。另外,我在.h.@user2704095的声明中添加了CLLocationManagerDelegate-还添加了这个委托方法,您将能够在这个委托方法中获得lat和long。而且您无法在
    [self.locationManager startupdatatinglocation]之后获得lat和long行:)你在模拟器中使用/测试了吗?@iPatel-Okie,它现在和我的新输出一样工作。我的目标是从location manager获得当前位置的地图焦点。根据您的建议和输出,在viewDidLoad中,位置管理器的纬度和经度似乎为0。那么,当地图第一次出现时,我如何才能使它聚焦在正确的位置?@user2704095-你需要创建自定义注释,并在你的lat和long上添加注释,这是从
    didUpdateToLocation
    方法获得的,你可以通过现在选中它来创建自定义注释。我现在添加了这一行,仍然得到0.000000。另外,我在.h.@user2704095的声明中添加了CLLocationManagerDelegate-还添加了这个委托方法,您将能够在这个委托方法中获得lat和long。而且您无法在
    [self.locationManager startupdatatinglocation]之后获得lat和long行:)你在模拟器中使用/测试了吗?@iPatel-Okie,它现在和我的新输出一样工作。我的目标是从location manager获得当前位置的地图焦点。根据您的建议和输出,在viewDidLoad中,位置管理器的纬度和经度似乎为0。那么,当地图第一次出现时,我如何才能使它聚焦在正确的位置?@user2704095-你需要创建自定义注释,并在你的lat和long上添加注释,这是从
    didUpdateToLocation
    方法获得的,你可以通过现在选中它来创建自定义注释。是的,此代码将当前位置作为地图的中心。现在我有两个部分来实现我的目标(1.获取当前位置2.使地图在当前位置居中。这与我最初在viewDidLoad->CLLocationCoordinate2D centerCoord={self.myMap.userLocation.location.coordinate.latitude,self.myMap.userLocation.location.coordinate.longitude}中所做的工作类似;[self.myMap setcentercoord:centerCoord动画:TRUE];感谢@iPatel和Wizkid1490-您的建议有助于填写我的问题。是的,此代码将当前位置作为地图的中心。现在我有了这两个部分来实现我的目标(1.获取当前位置2.使地图在当前位置居中。这与我最初在viewDidLoad->CLLocationCoordinate2D centerCoord={self.myMap.userLocation.location.coordinate.latitude,self.myMap.userLocation.location.coordinate.longitude}中放置的内容类似;[self.myMap setcentercoord:centerCoord动画:TRUE];感谢@iPatel和Wizkid1490-您的建议有助于填写我的问题。