2

我有这个:

    //request stuff
NSString *searchString = [NSString stringWithFormat:@"Sydney"];
NSString *urlString = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/textsearch/json?query=%@&sensor=true&key=mykeynotyours!",searchString];

NSURL *requestURL = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:(requestURL)];

//response
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSError *jsonParsingError = nil;
NSDictionary *locationResults = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];

NSLog(@"%@",locationResults);

这会吐出一些结果,其中第一个是我想要的结果,但我无法像使用其他 API 那样得到 lat 和 lng:

NSString *stringLatitude = [locationResults objectForKey:@"lat"];

我怀疑它正在寻找的键是“结果”键的一个子集,我不知道如何告诉它:-(

结果:

    2013-04-12 18:47:49.784 Test[26984:c07] {
"html_attributions" =     (
);
results =     (
            {
        "formatted_address" = "Sydney NSW, Australia";
        geometry =             {
            location =                 {
                lat = "-33.8674869";
                lng = "151.2069902";
            };
            viewport =                 {
                northeast =                     {
                    lat = "-33.4245981";
                    lng = "151.3426361";
                };
                southwest =                     {
                    lat = "-34.1692489";
                    lng = "150.502229";
                };
            };
        };

感谢任何指导!

4

3 回答 3

5

您可以使用以下代码访问几何 lat 和 lang

NSString *stringLatitude = [[[[[locationResults objectForKey:@"results"] objectAtIndex:0] objectForKey:@"geometry"] objectForKey:@"location"] valueForKey:@"lat"];
NSString *stringLongitude = [[[[[locationResults objectForKey:@"results"] objectAtIndex:0] objectForKey:@"geometry"] objectForKey:@"location"] valueForKey:@"lng"];
于 2013-04-12T09:05:06.223 回答
0

用这个

 NSString *stringLatitude = [[[[[locationResults objectForKey:@"results" ]objectAtIndex:0] objectForKey:@"geometry"] objectForKey:@"location"] objectForKey:@"lat"];
 NSString *stringLongitude = [[[[[locationResults objectForKey:@"results" ]objectAtIndex:0] objectForKey:@"geometry"] objectForKey:@"location"] objectForKey:@"lng"];
于 2013-04-12T09:20:44.410 回答
0

使用 iOS 7?将此放入您的项目中:https ://github.com/gdyer/GDGooglePlaces

然后……就用这个- (id)initWithPlaceCategory:(NSString *)_category urlStub:(NSString *)_stub lat:(float)\ _lat andLon:(float)_lon;

映射、解析、查看、方向、共享等都为您处理。

于 2014-04-19T20:44:31.133 回答