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 ソリューションを受け入れます。以下は、ブロックとサードパーティ ライブラリを使用しないコードです。これは単なる提案であり、場所の詳細を取得するもう 1 つの方法を示しています。

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 は単純です。しかし、これはサードパーティのライブラリを使用しなくても簡単です。

この 200,1,23.8954,34.5478 のような Google からの応答を取得します。

200 - 成功 1 - 精度 23.8954 - ロケーションの緯度 34.5478 - ロケーションの経度

これは、JSON および XML 形式でさらに明確な情報を取得することもできますが、それらを使用するには、JSON および XML 形式から辞書にデコードする必要があります。

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 に答える