我环顾四周,但却找不到这个主题。 我正在尝试在我正在创建的应用中实现“找到你的车”功能。
我有一个地图视图,用户可以用它来存储他们汽车的位置。然后,他们可以单击“查找汽车”以在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];
答案 0 :(得分:4)
让地图旋转的答案如下。
首先启动位置管理器并设置委托。
// 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];
希望这可以帮助任何有同样问题的人