我正在使用 GOogle Places API 开发一个程序,它有两个功能:searchPlaces 和 loadPlaceDetails。
函数 loadPlaceDetails 使用与 searchPlaces 函数相同的代码(已改编),但它没有正确返回数据。
下面是 loadPlaceDetails 函数的代码:
- (NSString *)loadPlaceDetails{
NSString *myJson = [[NSString alloc] initWithContentsOfURL:
[NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/details/json?reference=CnRlAAAATXOCsnqUpz9GCU61PDw2GokDo2DTa_EWsUIDsfx5Fz5SF42iSarv-xE-smvnA6cY_kWbYIFte07Cu-_RsFvLewld_onmhaDvj_lsStNhoDzi_sTpOVhZywIH_8Y5YkrkudefaMF0J9vzUt_LfMzL2xIQXkCcDhJBwamOWFtvAXoAQRoUjwnYvrXeuYy6-ALt1enT1kRfDO4&sensor=true&key=AIzaSyBLY-lBALViJ6ybrgtOqQGhsCDQtsdKsnc"]];
if ([myJson length] == 0) {
[myJson release];
return @"Error";
}
// Create a dictionary from the JSON string
NSDictionary *results = [myJson JSONValue];
NSArray *place = [results objectForKey:@"results"];
placeDetailName = [[NSMutableString alloc] init];
placeDetailAddress = [[NSString alloc] init];
placeDetailLat = [[NSString alloc] init];
placeDetailLng = [[NSString alloc] init];
placeDetailUrl = [[NSString alloc] init];
placeDetailPhone = [[NSString alloc] init];
for (NSDictionary *element in place)
{
NSString *name = [element objectForKey:@"name"];
[placeDetailName stringByAppendingFormat:@"%@",name];
NSLog(@"Nome do estabelecimento: %@",placeDetailName);
NSString *address = [element objectForKey:@"formatted_address"];
[placeDetailAddress stringByAppendingFormat:@"%@",address];
NSLog(@"%@",address);
NSString *phone = [element objectForKey:@"formatted_phone_number"];
[placeDetailPhone stringByAppendingFormat:@"%@",phone];
NSLog(@"%@",phone);
NSString *url = [element objectForKey:@"url"];
[placeDetailUrl stringByAppendingFormat:@"%@",url];
NSLog(@"%@",url);
}
for (NSDictionary *result in [results objectForKey:@"results"])
{
NSDictionary *location = [[result objectForKey:@"geometry"] objectForKey:@"location"];
NSString *latitude = [[location objectForKey:@"lat"] stringValue];
NSString *longitude = [[location objectForKey:@"lng"] stringValue];
[placeDetailLat stringByAppendingFormat:@"%@",latitude];
[placeDetailLng stringByAppendingFormat:@"%@",longitude];
}
NSString *basicurl = @"http://www.(...)/(...)/directions.html";
NSString *funcao = @"loaddetailplace";
NSMutableString *placesURL = [NSMutableString string];
[placesURL appendString:[NSString stringWithFormat:@"%@?function=",basicurl]];
[placesURL appendString:[NSString stringWithFormat:@"%@&latorigem=",funcao]];
[placesURL appendString:[NSString stringWithFormat:@"%@&lngorigem=",placeDetailLat]];
[placesURL appendString:[NSString stringWithFormat:@"%@",placeDetailLng]];
return placesURL;
}
当我在程序中的某个地方(同一类)调用它时,它返回http://www.(...)/(...)/directions.html?function=loaddetailplace&latorigem=&lngorigem=。
它不处理函数的其他部分。我不知道会发生什么,如果有人帮助我,我将不胜感激!
谢谢!