[英]search in a range of latitude and longitude from text file on server in iOS app
我正在嘗試在文本文件(可在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'*第一個拋出調用棧:
NSDictionaries返回NSNumbers。 檢查您在哪里調用matchAtLongNow:您可能在期望字符串的地方有NSNumbers。
聽起來您需要地理搜索,而不是將其包含在某些閾值內。 我會改寫成這樣的東西:
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];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.