1

当我的 mapView 加载时,我试图在用户的当前位置周围设置大约 10 英里的范围。这是我在 viewDidLoad 中的 Objective C 代码:

self.mapView = [[MKMapView alloc] initWithFrame:self.view.frame];
self.mapView.delegate = self;
self.mapView.showsUserLocation = YES;
[self.view addSubview:self.mapView];


//Set initial scope of map
CLLocationDistance mapWidith = 16000;
CLLocationDistance mapHeight = 16000;
CLLocationCoordinate2D userLocation = self.mapView.userLocation.coordinate;

MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation, mapHeight , mapWidith);
[self.mapView setRegion: region
               animated:false];

当我运行它时,它以正确的缩放开始(我认为),但随机集中在大西洋某处。不确定我是否正确访问了 userLocation 坐标,但它似乎是正确的。我对 MKMapView 很陌生,所以我有点挣扎。

4

1 回答 1

1

The userLocation property on mapView isn't populated yet. You should implement the mapView:didUpdateUserLocation: method and after that point you can reliably use that property (or use the location sent to you).

于 2014-08-21T23:27:07.843 回答