我环顾四周,但在这个主题上几乎找不到。
我正在尝试在我正在创建的应用程序中实现“查找您的汽车”功能。
我有一个 map View ,用户可以使用它来存储他们汽车的位置。然后他们可以单击“查找汽车”以在 map View 上显示他们当前的位置和汽车。 (见下文)
我的问题:如何让 map 旋转以显示用户面对的方向?所以我可以告诉用户他们需要走哪条路。
很像本地 map 应用程序。
从我所做的阅读来看,我相信这将在 didUpdateToLocation: 委托(delegate)方法中完成,但我不太确定。
(只是意识到我的用户在湖中,但这不是问题)
这是我用来添加图钉并将它们安装在屏幕上的代码。
旧 CODE
编辑:
我已经设法使用 fguchelaar 在评论中提供的链接使其正常工作。我已将代码更改为如下所示。此代码确实允许 map 旋转,但有一些奇怪的行为。见 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];
最佳答案
让 map 旋转的答案如下。
首先启动位置管理器并设置委托(delegate)。
// 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];
上面的代码开始跟踪用户的位置。
然后你需要开始跟踪用户的标题来旋转 map
// Start heading updates.
if ([CLLocationManager headingAvailable]) {
locationManager.headingFilter = 5;
[locationManager startUpdatingHeading];
}
之后实现 LocationManager 委托(delegate)方法。
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading;
最后在该委托(delegate)方法中设置跟踪模式。
[MapView setUserTrackingMode:MKUserTrackingModeFollowWithHeading animated:YES];
希望这可以帮助任何有同样问题的人
关于iphone - 使用用户方向旋转 MKmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14628764/