我有一系列国家/地区名称,我想获取它们的坐标并存储到另一个数组中。在此功能之后,我的经度和纬度阵列仍然是空的。你知道这里有什么问题吗?我无法理解。
我的代码是:
- (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]);
}
答案 0 :(得分: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声明为块存储。
答案 1 :(得分:0)
我接受Nuzhat Zari解决方案。下面是不使用块和第三方库的代码。它只是一个建议,并向您展示了获取位置详细信息的另一种方法。
https://developers.google.com/maps/documentation/geocoding/
此网址提供了很好的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很简单。但是,如果不使用第三方库,这也是直截了当的。
您可以像这样得到Google的回复 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>