我环顾四周,但在这个主题上几乎找不到。我正在尝试在我正在创建的应用程序中实现“查找您的汽车”功能。
我有一个地图视图,用户可以使用它来存储他们汽车的位置。然后他们可以单击“查找汽车”以在地图视图上显示他们当前的位置和汽车。(见下文)
我的问题:如何让地图旋转以显示用户面对的方向?所以我可以告诉用户他们需要走哪条路。很像本地地图应用程序。
从我所做的阅读来看,我相信这将在 didUpdateToLocation: 委托方法中完成,但我不太确定。
(只是意识到我的用户在湖中,但这不是问题)
这是我用来添加图钉并将它们安装在屏幕上的代码。
旧代码
编辑:
我已经设法使用 fguchelaar 在评论中提供的链接使其正常工作。我已将代码更改为如下所示。此代码确实允许地图旋转,但有一些奇怪的行为。看这里
- (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];