我正在 ios 中的 mapkit 上工作,我必须从当前位置追踪我的路线。 现在,我已经使用模拟器(调试->位置->自定义位置)分配了我当前的位置,即纬度和经度,但是当我尝试追踪我的源时,我得到的源位置为零,因此我无法获得路线。
[directionsRequest setSource:[MKMapItem mapItemForCurrentLocation]];
谁能为我的问题提出可能的解决方案
代码
MKDirectionsRequest *directionsRequest = [[MKDirectionsRequest alloc] init];
MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:thePlacemark];
[directionsRequest setSource:[MKMapItem mapItemForCurrentLocation]];
[directionsRequest setDestination:[[MKMapItem alloc] initWithPlacemark:placemark]];
directionsRequest.transportType = MKDirectionsTransportTypeAutomobile;
MKDirections *directions = [[MKDirections alloc] initWithRequest:directionsRequest];
最佳答案
如果您使用的是 MapView,那么有一种委托(delegate)方法可以让您在不使用 CLLocationManager 的情况下获取当前位置。
-(void)viewDidLoad
{
mapView.delegate=self;
}
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
`enter code here` //Here MKUserLocation will give you current Location and every time will method will call when the location is change...'
}
如果你只想要用户位置然后使用
MKUserLocation *userlocation=mapView.userlocation
关于ios - mapItemForCurrentLocation 找不到我的当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22059112/