开发者

Passing arguments into url string objective c

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 = [[GDataXML开发者_运维问答Document 

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.

Please suggest

Update:

This is my original code:

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

}

Update 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;

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

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


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

Usage:

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]);


You can use the %@ format specifier instead of %f when using strings. Also dont forget to release a and b.

Also you need location=. So you can change it to

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

And don't forget to release googleUrl.

PS:

Also use better variable names for better readability.


It sounds like you want:

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


Try this:

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

You won't need to specify a and b anymore =)!

BTW, I don't know for sure, but I guess you're trying to display a map view centered at the current location. You should use MKMapView for this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜