本文介绍了卡在ObjC和MapKit CoreLocation中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用ios的mapkit和corelocation.我只是想显示地图并暂时放大当前位置.我可以绘制地图以及自己的位置.我可以得到我的当前位置.我只是无法将位置数据输入到map方法中.我有点菜鸟,但以为我知道到目前为止我在做什么! :)任何帮助表示赞赏.我的代码在这里,是什么样的作品,但是让我在大西洋中部的零长零纬位置陷入困境!

I've been getting into mapkit and corelocation for ios. I'm just trying to display a map and zoom in on my current location for now. I can draw the map, with my location. I can get my current location. I just can't get the location data into the map method. I'm a bit of a noob, but thought I knew what I was doing up until this point! :) Any help is appreciated. My code is here, which sort of works but plonks me in the middle of the Atlantic at 0 long and 0 lat!

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

locationManager = [[CLLocationManager alloc] init];
[locationManager setPurpose:@"What use is a nav app if it doesn't know where you are?!?"];
[locationManager setDelegate:self ];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation];
[locationManager setDistanceFilter:10];
[locationManager startUpdatingLocation];

mapView=[[MKMapView alloc] initWithFrame:self.view.bounds];
[self.view insertSubview:mapView atIndex:0];
mapView.showsUserLocation = TRUE;
mapView.mapType = MKMapTypeStandard;


MKCoordinateSpan span;
span.latitudeDelta=0.2;
span.longitudeDelta=0.2;

CLLocationCoordinate2D location = mapView.userLocation.coordinate;

MKCoordinateRegion region;
region.span=span;
region.center=location;

[mapView setRegion:region animated:TRUE];
[mapView    regionThatFits:region];
[self.view insertSubview:mapView atIndex:0];

}

-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
      fromLocation:(CLLocation *)oldLocation
{
CLLocationCoordinate2D here = newLocation.coordinate;
NSLog(@"%f  %f", here.latitude, here.longitude);

[locationManager stopUpdatingLocation];

}

推荐答案

showsUserLocation设置为YES后,通常不会立即使用地图视图userLocation.

The map view userLocation will not usually be available right after setting showsUserLocation to YES.

实施地图视图的委托方法didUpdateUserLocationdidFailToLocateUserWithError.您需要设置地图视图的delegate属性,否则将不会调用它们.在didUpdateUserLocation方法中更改地图的区域或中心,而不是在viewDidLoad中.

Implement the map view's delegate methods didUpdateUserLocation and didFailToLocateUserWithError. You'll need to set the map view's delegate property or they won't get called. Change the map's region or center in the didUpdateUserLocation method--not in viewDidLoad.

或者,在已实现的CLLocationManager的didUpdateToLocation中设置区域.它会被调用吗?还要实现它的didFailWithError方法,看看它是否被调用.

Or, set the region in the CLLocationManager's didUpdateToLocation which you've implemented. Does it get called? Also implement its didFailWithError method and see if it gets called.

如果您使用的是MKMapView,则只需使用showsUserLocation或CLLocationManager即可(而不是同时使用两者)来获取当前位置.但是要获得蓝色大理石,您需要使用showsUserLocation.

You only need to use either showsUserLocation or the CLLocationManager, not both, to get the current location if you're using MKMapView. But to get the blue marble, you need to use showsUserLocation.

此外,为什么还要在mapView上进行两次insertSubview?

Also, why are you doing insertSubview twice on mapView?

这篇关于卡在ObjC和MapKit CoreLocation中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 04:38