将参数传递给url字符串目标c

时间:2011-08-11 13:42:59

标签: iphone objective-c

我想将参数传递到google places url string。 字符串是

@"https://maps.googleapis.com/maps/api/place/search/xml?location=52.577798767,-2.124885567&radius=500&types=bank&sensor=false&key=AIzaSyCcC9pmri9XGOgyhjoHQq37cmcbfhjfghf6bBZe80"

我没有回应。虽然我可以得到位置更新。但是我想在

中使用这个字符串
-(void)ParseXML_of_Google_PlacesAPI { NSURL *googlePlacesURL = [NSURL

URLWithString:googleUrl]; NSData *xmlData = [NSData 

dataWithContentsOfURL:googlePlacesURL]; xmlDocument = [[GDataXMLDocument 

alloc]initWithData:xmlData options:0 error:nil]; NSArray *arr = 

[xmlDocument.rootElement elementsForName:@"result"]; placesOutputArray=[[NSMutableArray 

alloc]init]; for(GDataXMLElement *e in arr ) { [placesOutputArray addObject:e]; }

但不行。它没有回应。

请建议

更新

这是我的原始代码:

- (void)locationUpdate:(CLLocation *)location {



googleUrl= [[NSString alloc ]initWithFormat:@"https://maps.googleapis.com/maps/api/place

/search/xml?location=%f,%f&radius=500&name=money&sensor=false&
 key=AIzaSyCcC9pmri9XGOgyhjoHQq37cmcdfsab6bBZe80",location.coordinate.latitude,location.coordinate.longitude];

}

更新2

googleUrl= @"https://maps.googleapis.com/maps/api/place/search/xml?location=%f,%f&radius=500&name=the%20money&sensor=false&key=AIzaSyCcC9pmrisgd9XGOgyhjoHQq37cmcb6bBZe80",a,b;

此字符串也无效。因为我把a = @“9.281654854”和b = @“ - 3.32532”,即a和b中的静态值并使用它也没有显示任何位置

我已更新了我的问题,现在使用与问题相同的内容并遵循您的建议。但它仍然没有显示任何位置。虽然它要求我点击确定以访问位置数据。当我点击确定它没有显示任何位置。我的字符串是可以的,因为我把静态坐标用作#define googleUrl = @“string”。有用。但是使用dynimic坐标它不起作用

4 个答案:

答案 0 :(得分:1)

使用字符串时,您可以使用%@格式说明符而不是%f。另外,不要忘记发布ab

你还需要location =。所以你可以把它改成

googleUrl= [[NSString alloc ]initWithFormat:@"https://maps.googleapis.com/maps/api/place/search/xml?location=%f,%f &radius=500&types=bank&sensor=false&key=api_key",location.coordinate.latitude,location.coordinate.longitude];

不要忘记发布googleUrl。

PS:

还可以使用更好的变量名来提高可读性。

答案 1 :(得分:1)

这是替换任何URL上的参数的一般答案,可能比问题要求更一般,但无论如何都没有得到用例...:P

-(NSURL*) replaceLocation:(NSURL*)url latitude:(float)latitude longitude:(float)longitude {

    // query to dictionary
    NSMutableDictionary *query = [NSMutableDictionary dictionary];
    for (NSString *param in [[url query] componentsSeparatedByString:@"&"]) {
        NSArray *queryParam = [param componentsSeparatedByString:@"="];
        if([queryParam count] < 2) continue;
        [query setObject:[queryParam objectAtIndex:1] forKey:[queryParam objectAtIndex:0]];
    }

    // override location
    [query setObject:[NSString stringWithFormat:@"%f,%f",latitude,longitude] forKey:@"location"];

    // encode and reassemble
    NSMutableArray *queryComponents = [NSMutableArray array];
    for (NSString *key in query) {
        NSString *value = [query objectForKey:key];
        key = [key stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
        value = [value stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
        NSString *component = [NSString stringWithFormat:@"%@=%@", key, value];
        [queryComponents addObject:component];
    }

    NSString *queryString = [queryComponents componentsJoinedByString:@"&"];

    NSMutableString *newUrl = [NSMutableString string];
    [newUrl appendFormat:@"%@://%@",[url scheme],[url host]];
    if ([url port]!=nil){
        [newUrl appendFormat:@":%@",[url port]];
    }
    [newUrl appendFormat:@"%@",[url path]];
    if ([url parameterString]!=nil){
        [newUrl appendFormat:@";%@",[url parameterString]];
    }
    if (queryString!=nil && [queryString length]>0){
        [newUrl appendFormat:@"?%@",queryString];
    }    
    if ([url fragment]!=nil){
        [newUrl appendFormat:@"#%@",[url fragment]];
    }

    return [NSURL URLWithString:newUrl];
}

用法:

NSURL *url = [NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/search/xml?location=52.577798767,-2.124885567&radius=500&types=bank&sensor=false&key=AIzaSyCcC9pmri9XGOgyhjoHQq37cmcbfhjfghf6bBZe80"];
NSLog(@"from \n%@ \nto\n%@", [url absoluteString], [[self replaceLocation:url latitude:0.54 longitude:0.56] absoluteString]);

答案 2 :(得分:0)

试试这个:

googleUrl = [[NSString alloc] initWithFormat:@"https://maps.googleapis.com/maps/api/place/search/xml?location=%f,%f&radius=500&types=bank&sensor=false&key=AIzaSyCcC9pmri9XGOgyhjoHQq37cmcb6bBZe80", location.coordinate.latitude, location.coordinate.longitude];

您不再需要指定ab =)!

顺便说一句,我不确定,但我想您正在尝试显示以当前位置为中心的地图视图。您应该使用MKMapView

答案 3 :(得分:0)

这听起来像你想要的:

googleUrl = [[NSString alloc] initWithFormat:@"https://maps.googleapis.com/maps/api/place/search/xml?location=%f,%f&radius=500&types=bank&sensor=false&key=AIzaSyCcC9pmri9XGOgyhjoHQq37cmcb6bBZe80",location.coordinate.latitude, location.coordinate.longitude];