0

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.

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

5
  • When you write "But not working" what does this mean? Always ask yourself, "Does my question have enough information to help others answer it?" Commented Aug 11, 2011 at 13:50
  • I have updates my question. plz have a look. and sorry for that Commented Aug 11, 2011 at 13:57
  • And, while you're at it, you could format your code a little better. Commented Aug 11, 2011 at 14:31
  • Ok i understand but more important is solving problem. Please suggest if you can. Thanks Commented Aug 11, 2011 at 14:35
  • 1
    If you want others to read your code, it makes sense to format it properly. If you get results with static coordinates, something is wrong with your actual code. Commented Aug 11, 2011 at 14:40

4 Answers 4

1

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]);
Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

I put same code as your wrote here. but still it does not show any error but does not show any location as well. i have updates my question you can see what i wanted to do
@Parveen S: 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
0

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

6 Comments

What error do you get? Please describe what "not working" means in more detail.
i am not getting any error. i put this this in - (void)locationUpdate:(CLLocation *)location { googleUrl= [[NSString alloc ]initWithFormat:@"maps.googleapis.com/maps/api/place/search/…; }
I suspect when you say its not working, you are getting incorrect or no response. you may want to encode the string for correct response which is a totally new issue altogether.
Yes 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]; }
Are you ultimately trying to work with a NSURL or NSString object? Please describe what your real problem is, by editing your original question.
|
0

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.

4 Comments

is the space in %f,%f & needed and valid?
@Marek, it's a typo, its both invalid and unneeded, I'll edit it =).
@Praveen S, why? You don't need to encode the coordinates to submit with Google Maps.
i am using now coordinates instead of a and b. but still it is not showing any location.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.