繁体   English   中英

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

[英]Unable to get current location to set center of map

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

注意:

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

我尝试过使用2种方法。

方法A)我将showsUserLocation设置为YES。 然后将地图的中心设置为map的userLocation中的值。 但是用户位置为空,latitutude和经度为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

ViewController.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.

并且还添加了委托方法,如,

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{   

    NSLog(@"Latitude :  %f", newLocation.coordinate.latitude);
    NSLog(@"Longitude :  %f", newLocation.coordinate.longitude);

    [currentLocation stopUpdatingLocation];
}
 - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
    {
        currentLocation=[userLocation coordinate];
        MKCoordinateRegion region=MKCoordinateRegionMakeWithDistance(currentLocation, 100000, 100000);
        [_map setRegion:region];
    }
It also updates the location if it changes and set the current location as the visible
 region of the map.Import CoreLocation.framework

你可以先设置

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate =self;
[locationManager startUpdatingLocation];

然后跟随方法在currentLocation中获得坐标

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

    [manager stopUpdatingLocation];
    locationManager.delegate = nil;
    locationManager = nil;

    CLLocation *currentLocation = newLocation;


}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM