我到处看了看,但在这个问题上几乎找不到什么。我正在尝试实现一个“找到你的车”功能到我正在创建的应用程序中。
我有一个地图视图,用户可以用来存储他们的汽车的位置。然后,他们可以点击“查找汽车”,在mapView上显示他们的当前位置和汽车。(见下文)
我的问题是:如何让地图旋转以显示用户面对的方向?这样我就可以告诉用户他们需要走哪条路。与原生地图ap非常相似。
从我所做的阅读来看,我相信这将在didUpdateToLocation: delegate方法中完成,但我不太确定。

(只要意识到我的用户在湖中,但这不是问题所在)
这是代码,我已经用来添加引脚和适合他们在屏幕上。
旧CODE
编辑:
我使用fguchelaar在评论中提供的链接,设法让它正常工作。我更改了代码,使其如下所示。这段代码允许地图旋转,但有一些奇怪的行为。请参阅HERE
- (IBAction)BTNPinCar:(id)sender {
CLLocation *location = [locationManager location];
// Configure the new event with information from the location
float longitude=location.coordinate.longitude;
float latitude=location.coordinate.latitude;
NSLog(@"dLongitude : %f", longitude);
NSLog(@"dLatitude : %f", latitude);
[self pins:latitude lon:longitude];
parked.latitude = latitude;
parked.longitude = longitude;
//set the reigion to the coords and a mile distance
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(parked, startZoom , startZoom );
//change the region and animate it
[MapView setRegion:viewRegion animated:YES];
}
- (IBAction)BTNFindCar:(id)sender {
MKMapRect zoomRect = MKMapRectNull;
for (id <MKAnnotation> annotation in MapView.annotations)
{
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1);
if (MKMapRectIsNull(zoomRect)) {
zoomRect = pointRect;
} else {
zoomRect = MKMapRectUnion(zoomRect, pointRect);
}
}
[MapView setVisibleMapRect:zoomRect animated:YES];\
// Start heading updates.
if ([CLLocationManager headingAvailable]) {
locationManager.headingFilter = 5;
[locationManager startUpdatingHeading];
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
// [MapView setTransform:CGAffineTransformMakeRotation(-1 * newHeading.magneticHeading * M_PI / 180)];
double rotation = newHeading.magneticHeading * 3.14159 / 180;
CGPoint anchorPoint = CGPointMake(0, -23); // The anchor point for your pin
[MapView setTransform:CGAffineTransformMakeRotation(-rotation)];
[[MapView annotations] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
MKAnnotationView * view = [MapView viewForAnnotation:obj];
[view setTransform:CGAffineTransformMakeRotation(rotation)];
[view setCenterOffset:CGPointApplyAffineTransform(anchorPoint, CGAffineTransformMakeRotation(rotation))];
}];
} 编辑2:
解决了旋转问题;
变化
[MapView setTransform:CGAffineTransformMakeRotation(-rotation)];为
[MapView setUserTrackingMode:MKUserTrackingModeFollowWithHeading animated:YES];发布于 2013-02-01 00:20:04
让贴图旋转的答案如下所示。
首先启动位置管理器并设置代理。
// locationManager update as location
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
// Start location services to get the true heading.
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];上面的代码开始跟踪用户的位置。然后,您需要开始跟踪用户标题以旋转地图
// Start heading updates.
if ([CLLocationManager headingAvailable]) {
locationManager.headingFilter = 5;
[locationManager startUpdatingHeading];
}之后,实现LocationManager委托方法。
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading;最后,在该委托方法中设置跟踪模式。
[MapView setUserTrackingMode:MKUserTrackingModeFollowWithHeading animated:YES]; 希望这篇文章能帮助任何有同样问题的人
https://stackoverflow.com/questions/14628764
复制相似问题