iOS iPhone展示用户在空间中的方向和方位,就像MKMapView上的指南针应用程序一样

5
当指南针应用程序使用地图视图来显示其位置时,会有一个小锥体显示手机指向的方向。然而,我无法在显示用户位置的MKMapView中复制它。这个视野锥体功能是否可供开发者使用,还是我需要自己实现一个?
谢谢!

在这里的另一篇帖子中提供了一个简洁的解决方案:https://dev59.com/l2kw5IYBdhLWcg3wdKXD - user1567676
5个回答

6
这非常简单,就像使用MapKit一样容易。 这被称为UserTrackingMode。只需编写一行代码即可使地图变成“指南针”:
 [mapView setUserTrackingMode:MKUserTrackingModeFollowWithHeading animated:YES];

此外,有大约三种用户跟踪模式:
- MKUserTrackingModeNone (free scrolling map)
- MKUserTrackingModeFollow (center the user's location)
- MKUserTrackingModeFollowWithHeading (center user's location and track user's heading (direction)).

如果您正在使用两种跟踪(非免费)模式之一,然后您将地图滚动到外面,该模式将自动更改为免费模式。

1


1

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

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

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

希望这可以在某种程度上帮助。


你能提供一些你所讨论的工作样例吗? - Adeel Ur Rehman
代码示例可在用户1567676提供的帖子链接中找到,请查看。如果您正在寻找自定义方向指示器,也是可能的。 - zolio

-1
假设您在缓存目录中保存了一个名为“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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接