0

我有一系列国家名称,我想获取它们的坐标并存储到另一个数组中。在这个函数之后,我的经度和纬度数组仍然是空的。你知道这里有什么问题吗?我无法理解。

我的代码是:

    - (void)geocode {
    NSString *strcountry=[[NSString alloc]init];
    countryLati=[[NSMutableArray alloc]init];
    countryLongi=[[NSMutableArray alloc]init];
    countryName =[NSMutableArray arrayWithObjects:@"Bahrain",@"Bangladesh",@"Bhutan",@"China",@"Georgia",@"Hong Kong",nil ];
    int count=[countryName count];



    for(int i=0;i<count;i++)
    {
        strcountry=[countryName objectAtIndex:i];


    [SVGeocoder geocode:strcountry
             completion:^(NSArray *placemarks, NSHTTPURLResponse *urlResponse, NSError *error) {

                 //NSLog(@"placemarks = %@", placemarks);

                 CLPlacemark *place = [placemarks objectAtIndex:0];
                 CLLocation *location = place.location;
                 CLLocationCoordinate2D coord = location.coordinate;

                 NSString *tempLati=[[NSString alloc]initWithFormat:@"%g",coord.latitude];
                 NSString *tempLongi=[[NSString alloc]initWithFormat:@"%g",coord.longitude];
                 NSLog(@"-------------------------");
                 NSLog(@"Country : %@",strcountry);
                 NSLog(@"Latitude : %@ ",tempLati);
                 NSLog(@"Longitude : %@ ",tempLongi);

                 [countryLati addObject:tempLati];
                 [countryLongi addObject:tempLongi];

             }];


    }

    NSLog(@"%d",[countryName count]);
    NSLog(@"%d ",[countryLati count]);
    NSLog(@"%d ",[countryLongi count]);      


}
4

2 回答 2

0

我认为当你想改变块中的局部变量时,你需要将它声明为块,如下所示:

    NSArray *stringsArray = @[ @"string 1",

                              @"String 21", 

                              @"string 12",

                              @"String 11",

                              @"Strîng 21", 

                              @"Striñg 21", 

                              @"String 02" ];

    NSLocale *currentLocale = [NSLocale currentLocale];

    __block NSUInteger orderedSameCount = 0;

     NSArray *diacriticInsensitiveSortArray = [stringsArray sortedArrayUsingComparator:^(id string1, id string2) {
      NSRange string1Range = NSMakeRange(0, [string1 length]);

    NSComparisonResult comparisonResult = [string1 compare:string2 options:NSDiacriticInsensitiveSearch range:string1Range locale:currentLocale];

   if (comparisonResult == NSOrderedSame) {
       orderedSameCount++;
   }
    return comparisonResult;
}];

NSLog(@"diacriticInsensitiveSortArray: %@", diacriticInsensitiveSortArray);

NSLog(@"orderedSameCount: %d", orderedSameCount);

编辑:来自参考的声明。

块的一个强大特性是它们可以在相同的词法范围内修改变量。您表示块可以使用 __block 存储类型修饰符修改变量。

所以你应该将 countryLati 和 countryLongi 声明为块存储。

于 2012-10-15T07:23:37.407 回答
0

我接受 Nuzhat Zari 解决方案。下面是不使用块和 3rd 方库的代码。它只是一个建议,向您展示了另一种获取位置详细信息的方法。

https://developers.google.com/maps/documentation/geocoding/

这个 URL 有一个很好的 Google API 文档。

- (void)geocode 
{
NSString *strcountry=[[NSString alloc]init];
countryLati=[[NSMutableArray alloc]init];
countryLongi=[[NSMutableArray alloc]init];
countryName =[NSMutableArray arrayWithObjects:@"Bahrain",@"Bangladesh",@"Bhutan",@"China",@"Georgia",@"Hong Kong",nil ];
int count=[countryName count];



for(int i=0;i<count;i++)
{
    strcountry=[countryName objectAtIndex:i];


    NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", strCountry];
    NSString* webStringURL = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:webStringURL];
    NSString *locationString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"Location String = %@", locationString);
    NSArray *receivedGoogleData = [[NSArray alloc] initWithArray:[locationString componentsSeparatedByString:@","]];
    searchTermLocation = [[CLLocation alloc] initWithLatitude:[[receivedGoogleData objectAtIndex:2] doubleValue] longitude:[[receivedGoogleData objectAtIndex:3] doubleValue]];

    [countryLati addObject:[[receivedGoogleData objectAtIndex:2] doubleValue]];
    [countryLongi addObject:[[receivedGoogleData objectAtIndex:3] doubleValue]];

}

NSLog(@"%d",[countryName count]);
NSLog(@"%d ",[countryLati count]);
NSLog(@"%d ",[countryLongi count]);      
}

@Mahesh Dhapa:试试这个,从我的角度来看,我觉得这比你的 SVGeoCoder 方式简单,因为不需要额外导入 SVGeocoder 类。顺便说一句,SVGeocoder 很简单。但是,这也很简单,无需使用 3rd 方库。

你从谷歌得到这样的回应 200,1,23.8954,34.5478

其中,200 - 成功 1 - 准确度 23.8954 - 位置纬度 34.5478 - 位置经度

您也可以使用 JSON 和 XML 格式获取此信息,并提供更清晰的信息,但您需要将它们从 JSON 和 XML 格式解码为 Dictionary 才能使用它们。

示例 JSON 格式:

{
  "name": "Bahrain",
  "Status": {
    "code": 200,
    "request": "geocode"
  },
  "Placemark": [ {
    "id": "p1",
    "address": "Bahrain",
    "AddressDetails": {
   "Accuracy" : 1,
   "Country" : {
      "CountryName" : "Bahrain",
      "CountryNameCode" : "BH"
  }
},
    "ExtendedData": {
      "LatLonBox": {
        "north": 26.3240565,
        "south": 25.5798401,
        "east": 50.8228639,
        "west": 50.3781509
      }
    },
    "Point": {
      "coordinates": [ 50.5577000, 26.0667000, 0 ]
    }
  } ]
}

示例 XML 响应:

<kml xmlns="http://earth.google.com/kml/2.0">
<Response>
<name>Bahrain</name>
<Status>
<code>200</code>
<request>geocode</request>
</Status>
<Placemark id="p1">
<address>Bahrain</address>
<AddressDetails xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0" Accuracy="1">
<Country>
<CountryNameCode>BH</CountryNameCode>
<CountryName>Bahrain</CountryName>
</Country>
</AddressDetails>
<ExtendedData>
<LatLonBox north="26.3240565" south="25.5798401" east="50.8228639" west="50.3781509"/>
</ExtendedData>
<Point>
<coordinates>50.5577000,26.0667000,0</coordinates>
</Point>
</Placemark>
</Response>
</kml>
于 2012-10-15T07:36:05.127 回答