Not getting json response in google query of longitude and latitude in iOS?

北城以北 提交于 2019-12-24 00:52:20

问题


I am new to iOS, so if any help it will be appreciated. I am trying to get the longitude and latitude from address, earlier the code was working fine but now the JSON data are coming null.

Here my sample code,

     url = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?address=%@&sensor=false",appDelegate.sAddress];

    url=[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"Address URL: %@",url);

    //Formulate the string as a URL object.
    NSURL *requestURL=[NSURL URLWithString:url];
    NSData* data = [NSData dataWithContentsOfURL: requestURL];

    NSString *returnString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    NSLog(@"my Coordinate : %@",returnString);

    NSError* error;
    NSDictionary* json = [NSJSONSerialization 
                          JSONObjectWithData:data
                          options:kNilOptions 
                          error:&error];

But i am getting the output as null.

So please help me out.

Thanks!


回答1:


Thanks for your replies that all make me learn a lots. As one of my friend just tell me the solution so i am sharing with you.

Here is the code,

url = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?address=%@&sensor=false",appDelegate.sAddress];

url=[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"Address URL: %@",url);

//Formulate the string as a URL object.
NSURL *requestURL=[NSURL URLWithString:url];
NSData* data = [NSData dataWithContentsOfURL: requestURL];

NSString *returnString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

SBJSON *parser = [[SBJSON alloc] init];
NSDictionary *locationResult = [parser objectWithString:returnString];
//[reverseGeoString copy]`

And its working fine.

But still there is a question that why this happen.As earlier that code is working fine but it suddenly stopped working.




回答2:


You must construct your returnString in the following method that actually receives the data:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

Check out this for additional information on how to use NSURLConnection and the delegate methods.




回答3:


I would say you're missing the all-important "REQUEST"...

This is what I do. Hope it helps:

    NSString *encodedAddress = (__bridge_transfer NSString *) CFURLCreateStringByAddingPercentEscapes(NULL, (__bridge_retained CFStringRef)searchBar.text, NULL, (CFStringRef) @"!*'();:@&=+$,/?%#[]",kCFStringEncodingUTF8 );

    NSString* searchURL = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=%@&sensor=true",encodedAddress];
    NSError* error = nil;
    NSURLResponse* response = nil;
    NSMutableURLRequest* request = [[NSMutableURLRequest alloc] init];

    NSURL* URL = [NSURL URLWithString:searchURL];
    [request setURL:URL];
    [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
    [request setTimeoutInterval:30];

    NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    if (error){
        NSLog(@"Error performing request %@", searchURL);
        return;
    }

    NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    if (jsonString!=nil){
        NSLog(@"%@",jsonString);
    }


来源:https://stackoverflow.com/questions/12502961/not-getting-json-response-in-google-query-of-longitude-and-latitude-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!