如何在iPhone SDK模拟器中获取用户当前位置?

5

我正在使用这个方法获取用户位置

/*Region and Zoom*/
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.2;
span.longitudeDelta=0.2;

CLLocation *location1 = [locationManager location];

CLLocationCoordinate2D location = [location1 coordinate];

location.latitude = location.latitude;
location.longitude = location.longitude;
region.span=span;
region.center = location;

/*Geocoder Stuff*/

MKReverseGeocoder *geoCoder=[[MKReverseGeocoder alloc] initWithCoordinate:location];
geoCoder.delegate = self;
[geoCoder start];
mapView.showsUserLocation = TRUE;

[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region];

这段代码在实际运行中表现良好,但是当我尝试在模拟器中运行时,它会在[mapView setRegion:region animated:TRUE];处崩溃,这表明它无法获取纬度和经度值。那么我该如何使这段代码在模拟器上运行良好呢?

我必须让这段代码在模拟器上运行,这是否可能?


模拟器未显示当前位置。 - Yama
1
是的,我知道,但我必须这样做,所以可以吗? - Hrushikesh Betai
http://stackoverflow.com/questions/6946966/iphone-simulator-4-3-with-xcode-4-is-not-showing-current-location - Yama
我也遇到了和你一样的问题。我无法在模拟器中找到当前位置。 您可以参考这个链接 - Krunal
2个回答

18

在iOS5模拟器中,默认情况下会显示美国的某个位置。如果您想更改该位置,请在iOS模拟器菜单中转到Debug -> Location -> Custom Location。在那里,您可以设置纬度和经度,并相应地测试应用程序。这适用于mapkit和CLLocationManager。


13

尝试以下路径 -

进入产品->编辑方案->选项->选择允许位置模拟

这并不是你的确切位置!但至少你可以验证你的代码是否正常工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接