仿真中的核心位置问题

时间:2012-10-23 09:36:39

标签: iphone objective-c ios mapkit core-location

我无法在iphone模拟器中获取当前位置。我将自定义位置设置为纬度和经度到埃及,但没有获得当前位置。

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

    //Make this controller the delegate for the map view.
    self.MapView.delegate = self;     

    // Ensure that you can view your own location in the map view.
    [self.MapView setShowsUserLocation:YES];



    //Instantiate a location object.
    locationManager = [[CLLocationManager alloc] init];
    [locationManager startUpdatingLocation];

    //Make this controller the delegate for the location manager.
    [locationManager setDelegate:self];

    //Set some parameters for the location object.
    [locationManager setDistanceFilter:kCLDistanceFilterNone];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
}

这是委托:

#pragma mark - MKMapViewDelegate methods.

- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views 
{    
    MKCoordinateRegion region;
    region = MKCoordinateRegionMakeWithDistance(locationManager.location.coordinate,2000,2000);


    [mv setRegion:region animated:YES];
}

请任何人帮助我。

2 个答案:

答案 0 :(得分:3)

究竟是什么问题?没有得到一个userLocation你应该有或没有将地图区域移动到正确的位置?

如果是第一个,那么主要是工具问题,而不是代码问题:

如果您的位置位于项目内的gpx文件中,您应该可以通过编辑Scheme / Options / Allow Location Simulation /来选择您的默认位置

Scheme Edition for starting at a specific place

您的项目中应该有一个DemoStart.gpx文件,如下所示:

<?xml version="1.0"?>
<gpx version="1.1" creator="Xcode">
    <wpt lat="20.88072" lon="10.67429">
        <name>Demo Starting Location</name>
    </wpt>
</gpx>

(对不起,我太懒了,无法找到埃及的确切坐标)。

答案 1 :(得分:0)

您必须首先确保您的CLLocationManager更新其位置。为此,将视图控制器设置为CLLocationManager的委托,您已经这样做,并保存此位置。完成所有设置后尝试拨打startUpdatingLocation,并将showsUserLocation设置为YES

- (void)locationManager:(CLLocationManager *)manager

     didUpdateLocations:(NSArray *)locations {

    // If it's a relatively recent event, turn off updates to save power

    CLLocation* myLocation = [locations lastObject];
}

请确保您在iOS模拟器版本&gt; = 5.1

上进行测试