将参数传递到url字符串目标c [英] Passing arguments into url string objective c

查看:34
本文介绍了将参数传递到url字符串目标c的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将参数传递给Google Places网址字符串.字符串是

I want to pass argument into google places url string. The string is

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

我没有回应.虽然我可以获取位置更新.但我想在

i get no response. though i can get location updates. but i want to use this string in

-(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]; }

但是不起作用.它没有回应.

But not working. It gives no response.

请提出建议

更新:

这是我的原始代码:

- (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中使用静态值并使用它时,它也不会显示任何位置

This string is also not working. becaue.when i put a=@"9.281654854", and b=@"-3.32532", i.e. static values in a and b and use it it also does not show any location

我已经更新了我的问题,现在使用与问题所示相同的内容并遵循您的建议.但是它仍然不显示任何位置.尽管它要求我单击确定"以访问位置数据.当我单击确定时,它不会显示任何位置.我的字符串没问题,因为当我放置静态坐标并用作#define googleUrl = @"string"时.有用.但带有动态坐标则不起作用

I have updated my question and now using same thing as shown in question and following your advice. But still it does not show any location. though it ask me to click ok to access location data. when i click ok it does not show any location. My string is ok because when i put static coordinates and used as #define googleUrl=@"string". it works. but with dynimic coordinates it does not work

推荐答案

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

This is a general answer to replace a parameter on any URL, probably more general than the question asks for, but didn't quite get the use case anyway... :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]);

这篇关于将参数传递到url字符串目标c的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆