1

我环顾四周,但在这个主题上几乎找不到。我正在尝试在我正在创建的应用程序中实现“查找您的汽车”功能。

我有一个地图视图,用户可以使用它来存储他们汽车的位置。然后他们可以单击“查找汽车”以在地图视图上显示他们当前的位置和汽车。(见下文)

我的问题:如何让地图旋转以显示用户面对的方向?所以我可以告诉用户他们需要走哪条路。很像本地地图应用程序。

从我所做的阅读来看,我相信这将在 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];
4

1 回答 1

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]; 

希望这可以帮助任何有同样问题的人

于 2013-01-31T16:20:04.340 回答