从 iOS 应用程序服务器上的文本文件中搜索一系列经纬度
Posted
技术标签:
【中文标题】从 iOS 应用程序服务器上的文本文件中搜索一系列经纬度【英文标题】:search in a range of latitude and longitude from text file on server in iOS app 【发布时间】:2017-03-31 15:45:33 【问题描述】:我正在尝试在文本文件中搜索(可在 url 上找到),其中有纬度、经度和其他以逗号分隔的信息。 我需要搜索最接近用户在应用程序中选择的纬度经度的纬度经度。 完成代码以读取该文件并进行搜索,但它不能完美地工作。 请让我知道,在这种情况下我应该如何处理。 1)文件实时更新,所以每次用户选择位置并点击获取信息时,我都需要调用url并读取文件。 2)然后搜索与用户选择的经度最近的经度。
NSDICTIONARY 数据获取:-
(
"altitude_m" = "90.0";
engName = “Station 1”;
grkName = "\U039d\U03ad\U03b1 \U039c\U03ac\U03ba\U03c1\U03b7 \U0391\U03c4\U03c4\U03b9\U03ba\U03ae\U03c2";
"humidity_P" = "50.0";
lat = "38.058873";
"local_time" = "16:20";
lon = "23.976741";
"pressure_hPA" = "1019.9";
"rain_mm" = "0.0";
"tem_C" = "17.2";
"wind_Gusts_khm" = "51.5";
"wind_degrees" = "22.5";
"wind_kmh" = "25.7";
"altitude_m" = "90.0";
engName = “Station 2”;
grkName = "\U039d\U03ad\U03b1 \U039c\U03ac\U03ba\U03c1\U03b7 \U0391\U03c4\U03c4\U03b9\U03ba\U03ae\U03c2";
"humidity_P" = "50.0";
lat = "38.048873";
"local_time" = "16:20";
lon = "23.876741";
"pressure_hPA" = "1019.9";
"rain_mm" = "0.0";
"tem_C" = "17.2";
"wind_Gusts_khm" = "51.5";
"wind_degrees" = "22.5";
"wind_kmh" = "25.7";
"altitude_m" = "90.0";
engName = “Station 3”;
grkName = "\U039d\U03ad\U03b1 \U039c\U03ac\U03ba\U03c1\U03b7 \U0391\U03c4\U03c4\U03b9\U03ba\U03ae\U03c2";
"humidity_P" = "50.0";
lat = "38.059873";
"local_time" = "16:20";
lon = "23.986741";
"pressure_hPA" = "1019.9";
"rain_mm" = "0.0";
"tem_C" = "17.2";
"wind_Gusts_khm" = "51.5";
"wind_degrees" = "22.5";
"wind_kmh" = "25.7";
// 匹配函数:-
-(void)matchLAtLongNow: (NSString *)latitude :(NSString *)longitude
CLLocationCoordinate2D pinlocation;
pinlocation.latitude = [latitude doubleValue];
pinlocation.longitude = [longitude doubleValue];
float minLat = pinlocation.latitude - (searchDistance / 69);
float maxLat = pinlocation.latitude + (searchDistance / 69);
float minLon = pinlocation.longitude - searchDistance / fabs(cos(deg2rad(pinlocation.latitude))*69);
float maxLon = pinlocation.longitude + searchDistance / fabs(cos(deg2rad(pinlocation.longitude))*69);
NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"lat <= %f AND lat >= %f AND lon <= %f AND lon >= %f",
maxLat, minLat, maxLon, minLon];
MatchedTxtArray = [myArray filteredArrayUsingPredicate:applePred];
//获取错误 * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSCFNumber _fastCStringContents]:无法识别的选择器已发送到实例 0x175426460” * 首先抛出调用栈:
【问题讨论】:
【参考方案1】:NSDictionaries 返回 NSNumbers。检查你在哪里调用 matchAtLongNow:你可能在你期望字符串的地方有 NSNumbers。
【讨论】:
感谢您的回复。我检查了有字符串。【参考方案2】:听起来您需要进行地理搜索,而不是包含在某些阈值内。我会重新表述为这样的:
NSArray *objects; // this is your parsed file, an array of dictionaries
CLLocation *userLocation; // this is your user location
// sort the dictionary in order of distance to the userLocation
NSArray *sortedObjects = [objects sortedArrayUsingComparator:^(NSDictionary *objA,NSDictionary *objB)
CLLocationDistance distanceA = [self distanceToObject:objA from:userLocation];
CLLocationDistance distanceB = [self distanceToObject:objB from:userLocation];
return (NSComparisonResult)(distanceB-distanceA);
];
sortedObjects
将包含从userLocation
从最近到最远排序的字典我们只需要一个函数来生成从字典中的纬度/经度值到 userLocation 的距离...
- (CLLocationDistance)distanceToObject:(NSDictionary *)object from:(CLLocation *)location
CLLocationDegrees latitude = [object[@"lat"] doubleValue];
CLLocationDegrees longitude = [object[@"lob"] doubleValue];
CLLocation *objectLocation = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
return [objectLocation distanceFromLocation:location];
【讨论】:
以上是关于从 iOS 应用程序服务器上的文本文件中搜索一系列经纬度的主要内容,如果未能解决你的问题,请参考以下文章