【问题标题】:iOS iPhone show user direction and orientation in space like the compass app on MKMapViewiOS iPhone 在空间中显示用户方向和方向,就像 MKMapView 上的指南针应用程序一样
【发布时间】:2012-04-22 00:07:11
【问题描述】:

当指南针应用使用地图视图显示其位置时,会有一个小圆锥体显示手机指向的方向。但是,我无法使用显示用户位置的 MKMapView 重现它。开发人员是否可以使用此视锥角功能,还是我必须自己实现?

谢谢!

【问题讨论】:

标签: iphone objective-c mkmapview cllocationmanager compass-geolocation


【解决方案1】:

这对 MapKit 来说就像小菜一碟一样简单。 这称为用户跟踪模式。你只需要写下一行代码就可以让地图变成“指南针”:

 [mapView setUserTrackingMode:MKUserTrackingModeFollowWithHeading animated:YES];

此外,UserTrackingMode 大约有 3 种类型:

- MKUserTrackingModeNone (free scrolling map)
- MKUserTrackingModeFollow (center the user's location)
- MKUserTrackingModeFollowWithHeading (center user's location and track user's heading (direction)).

如果您处于两种跟踪(非免费)模式之一,则滚动地图,该模式将自动更改为免费模式。

【讨论】:

    【解决方案2】:

    我也遇到过类似的情况。我认为我们没有库或设置来在蓝色图标上显示方向(至少我的搜索不成功)。

    但是,使用 CLHeading 创建我们自己的方向指示器并不难(参考 TommyG 的答案)。

    我所做的是显示地图中的蓝色图标,并在不同的视图中提供一个小箭头来指示方向。

    希望这在某种程度上有所帮助

    【讨论】:

    • 你能提供一些你所讨论的工作样本吗??
    • 代码示例可在上面 user1567676 提供的帖子链接中找到。请看一看。如果您正在寻找自定义方向指示器,那也是可能的。
    【解决方案3】:
    【解决方案4】:

    MTLocation 包含大量图像,包括您所追求的锥体: https://github.com/myell0w/MTLocation/tree/master/Resources/MTLocation.bundle

    【讨论】:

      【解决方案5】:

      假设您在缓存目录中保存了一个名为“imageTexture.jpg”的临时图像。最喜欢的“FavoritePhoto.jpg”保存在文档目录中。 要覆盖文档目录中最喜欢的一个,您可以这样做。

          NSError *errorDesc;
      
          NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
          NSString *documentsDirectory = [paths objectAtIndex:0];
          NSString *statesDescriptionPath = [documentsDirectory stringByAppendingPathComponent:@"FavoritePhoto.jpg"];
      
          NSFileManager *fileManager = [NSFileManager defaultManager];
      
          NSString *cacheDirectory = [NSFileManager getCacheDirectory];
          NSString *temporaryPath = [cacheDirectory stringByAppendingPathComponent:@"imageTexture.jpg"];
      
          NSURL *originalURL = [NSURL fileURLWithPath:statesDescriptionPath];
          [fileManager replaceItemAtURL:originalURL withItemAtURL:[NSURL fileURLWithPath:temporaryPath] backupItemName:nil options:NSFileManagerItemReplacementUsingNewMetadataOnly resultingItemURL:&originalURL error:&errorDesc];
      
          if (errorDesc)
          {
              NSLog(@"there was an error overwriting the favorite photo: %@", errorDesc.description);
          }
      

      我正在使用 NSFileManager 类别来获取缓存目录

      这里是 NSFileManager+Powertools.h 的代码

      #import <Foundation/Foundation.h>
      @interface NSFileManager (Powertools)
      + (NSString *)getCacheDirectory;
      @end
      

      这里可以看到NSFileManager+Powertools.m的代码

      #import "NSFileManager+Powertools.h"
      
      @implementation NSFileManager (Powertools)
      
      + (NSString *)getCacheDirectory
      {
          NSString *path = nil;
              NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
              if ([paths count])
              {
                  NSString *bundleName =
                  [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"];
                  path = [[paths objectAtIndex:0] stringByAppendingPathComponent:bundleName];
              }
              return path;
      }
      @end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-05-07
        • 1970-01-01
        • 1970-01-01
        • 2011-05-22
        相关资源
        最近更新 更多