我需要检测应用用户所处的环境。例如他是在森林里,还是在城市里,还是在海边......
因此我只是根据 map 制作了一张图片,计算了平均像素并将该颜色与绿色、蓝色、棕色、灰色进行比较... 但这是非常不准确的,因为附近可能有大海,但平均颜色根本不是蓝色。此外,颜色的比较并不总是符合您的期望。
有没有更好的方法来检测用户所处的环境?由于它必须在全局范围内工作,我认为没有任何可能的服务可以为我提供有关福雷斯特、海洋、也许是山脉的可靠信息......
也许你们中有人知道如何解决这个问题或者对我有提示。
在这里,您可以看到到目前为止我是如何尝试的(它的 objective-c 代码,但我很高兴得到任何答案,以及与 iOS 应用程序开发无关的答案)。
- (UIColor *)mergedColor
{
CGSize size = {1, 1};
UIGraphicsBeginImageContext(size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(ctx, kCGInterpolationMedium);
[self drawInRect:(CGRect){.size = size} blendMode:kCGBlendModeCopy alpha:1];
uint8_t *data = CGBitmapContextGetData(ctx);
UIColor *color = [UIColor colorWithRed:data[2] / 255.0f
green:data[1] / 255.0f
blue:data[0] / 255.0f
alpha:1];
UIGraphicsEndImageContext();
return color;
}
UIColor *forrest = [UIColor greenColor];
const CGFloat *components1 = CGColorGetComponents([[self.view mergedColor] CGColor]);
const CGFloat *components2 = CGColorGetComponents([forrest CGColor]);
double fDistance = sqrt(pow(components1[0] - components2[0], 2) + pow(components1[1] - components2[1], 2) + pow(components1[2] - components2[2], 2));
double fPercentage = fDistance / sqrt(pow(255, 2) + pow(255, 2) + pow(255, 2));
最佳答案
这个解决方案的想法不涉及图像处理。
但是如果您知道您的纬度和经度(这很容易从 CoreLocation 获得),您可以将其传递给地理编码服务。
例如,当我查看 Google 的 Geocoding API 时,我看到了 "Address Types and Address Component Types" 的部分,类型包括:
natural_feature
表示突出的自然特征。机场
表示一个机场。park
表示命名公园。point_of_interest
指示命名的兴趣点。通常,这些“兴趣点”是 不容易归入另一个类别的著名本地实体 例如“帝国大厦”或“自由女神像”。
因此,该 API 中可能有足够的内容供您使用。
关于ios - 检测您所在的 GPS 环境的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22430280/