9

在 iPhone 3GS 上的“地图”应用程序中,您可以单击通常两次显示您的位置的图标,蓝点获得看起来像前照灯的光束,基本上显示您在地图上面对的方向并旋转图像因此。

这个选项可以使用 MapKit MapView 吗?

我知道我可以用类似的东西来标题

- (void)locationManager:(CLLocationManager *) manager didUpdateHeading:(CLHeading *) newHeading {

// 如果准确率有效,则处理事件。

if (newHeading.headingAccuracy > 0) {

    CLLocationDirection theHeading = newHeading.magneticHeading;
    ...
}

}

但我不知道如何在 Mapkit 中获得那种漂亮的前照灯效果,而且似乎没有任何文档。

有任何想法吗?

4

5 回答 5

6

添加用户跟踪模式也有帮助。我知道我迟到了,但可能对像我这样的其他开发人员有所帮助:)

self.mapView.userTrackingMode = RMUserTrackingModeFollowWithHeading;
于 2013-11-17T13:03:18.307 回答
4

我找到了一个解决方案:

我使用可用的航向信息旋转地图

[mapView setTransform:CGAffineTransformMakeRotation(heading.magneticHeading * M_PI / -180.0)];

因此,“光束”始终指向设备的顶部。我现在只是在地图顶部显示一个 ImageView 并更改它在 locationManager:didUpdateToLocation:fromLocation 中的位置:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
        // scroll to new location
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 2000, 2000);
    [self.mapView setRegion:region animated:YES];

        // set position of "beam" to position of blue dot
    self.headingAngleView.center = [self.mapView convertCoordinate:newLocation.coordinate toPointToView:self.view];
        // slightly adjust position of beam
    self.headingAngleView.frameTop -= self.headingAngleView.frameHeight/2 + 8;
 }

其中 frameTop 和 frameHeight 是 frame.origin.y 和 frame.size.height 的快捷方式。当点改变它的位置时,它并不理想,有时会缺少一点点,但我对它的解决方案 b/c 很满意。

看看我的 OpenSource 框架 MTLocation,它可以完成这一切(以及许多其他与地图相关的酷东西):

MTLocation

于 2011-02-08T20:13:35.320 回答
3

其他答案的旋转逻辑很好,但是依赖位置管理器委托方法不会产生好的解决方案。“光束”图像很少会与蓝点在同一个位置,并且会反弹很多。

最好的解决方案是获取对蓝点视图的引用,并将“光束”图像作为子视图添加到其中。我选择了一个正方形的光束图像,光束在顶部,周围是透明的。想象一下蓝点位于图像的中心。


// An MKMapViewDelegate method. Use this to get a reference to the blue dot annotation view as soon as it gets added
- (void)mapView:(MKMapView *)aMapView didAddAnnotationViews:(NSArray *)views {

    for (MKAnnotationView *annotationView in views) {

        // Get the user location view, check for all of your custom annotation view classes here
        if (![annotationView isKindOfClass:[MKPinAnnotationView class]])
        {
            self.userLocationAnnotationView = annotationView;
            gotUsersLocationView = YES;
        }
    }
}


// Adds the 'viewPortView' to the annotation view. Assumes we have a reference to the annotation view. Call this before you start listening to for heading events.
- (void)addViewportToUserLocationAnnotationView {
    TTDASSERT(self.userLocationAnnotationView != nil);
    if (self.userLocationAnnotationView == nil) {
        // No reference to the view, can't do anything
        return;
    }

    if (self.viewPortView == nil) {
        self.viewPortView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"map_viewport.png"]] autorelease];
    }

    [self.userLocationAnnotationView addSubview:self.viewPortView];
    [self.userLocationAnnotationView sendSubviewToBack:self.viewPortView];

    self.viewPortView.frame = CGRectMake((-1 * self.viewPortView.frame.size.width/2) + self.userLocationAnnotationView.frame.size.width/2,
                                         (-1 * self.viewPortView.frame.size.height/2) + self.userLocationAnnotationView.frame.size.height/2,
                                         self.viewPortView.frame.size.width,
                                         self.viewPortView.frame.size.height);
}
于 2011-03-18T15:15:45.137 回答
2

我可以想到一种方法,虽然我还没有实现,但可能会对你有所帮助。

  1. 首先,您需要一个带有您选择的指针的图像,该指针必须指向向上 90 度。
  2. 现在在您的 MapKit 中关闭当前位置标记。
  3. 在您didUpdateHeading使用 x 和 y 值来计算方向的角度。
  4. 使用这个角度来旋转指针的图像
  5. 将此旋转图像用作地图中的注释图钉。
  6. 您需要经常更新指针的位置。

请发布您对上述方法的建议/更改。

于 2010-06-22T13:39:10.340 回答
2

斯威夫特 3 实施

mapView.userTrackingMode = MKUserTrackingMode.followWithHeading
于 2016-10-05T09:54:25.450 回答