在IOS中从Google Map Place API提取要求信息

萨拉

我正在使用GPS应用程序,其中我需要根据某个地方的经度和纬度确定位置。我能够确定经度和纬度以及位置,但是我正在以json格式获取google maps api调用的结果其中有很多数据以及城市名称。

我已经完成了这样的工作(我正在使用模拟器,所以我已经通过了固定的纬度和经度)

NSString *urlString = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=false",30.819297, 76.444017];
    NSError* error;
        NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSASCIIStringEncoding error:&error];
        locationString = [locationString stringByReplacingOccurrencesOfString:@"\"" withString:@""];

    NSLog(@"%@",locationString ); 

我得到的输出字符串是

015-04-16 10:38:45.056 demolocationapp [690:12005] {

   results : [
      {
         address_components : [
            {
               long_name : Rauni Kalan Link Road,
               short_name : Rauni Kalan Link Rd,
               types : [ route ]
            },
            {
             **long_name : Rauni Kalan,
               short_name : Rauni Kalan,
               types : [ locality, political]**

            },
            {
               long_name : Rupnagar,
               short_name : Rupnagar,
               types : [ administrative_area_level_2, political ]
            },
            {
               long_name : Punjab,
               short_name : PB,
               types : [ administrative_area_level_1, political ]
            },
            {
               long_name : India,
               short_name : IN,
               types : [ country, political ]
            },
            {
               long_name : 140102,
               short_name : 140102,
               types : [ postal_code ]
            }
         ],
         formatted_address : Rauni Kalan Link Road, Rauni Kalan, Punjab 140102, India,
         geometry : {
            bounds : {
               northeast : {
                  lat : 30.8209903,
                  lng : 76.4550081
               },
               southwest : {
                  lat : 30.8178417,
                  lng : 76.44540219999999
               }
            },
            location : {
               lat : 30.8186057,
               lng : 76.4507395
            },
            location_type : APPROXIMATE,
            viewport : {
               northeast : {
                  lat : 30.8209903,
                  lng : 76.4550081
               },
               southwest : {
                  lat : 30.8178417,
                  lng : 76.44540219999999
               }
            }
         },
         place_id : ChIJc8IoHwICEDkR38Ie5TOeGu0,
         types : [ route ]
      },
      {
         address_components : [
            {
               long_name : Rauni Kalan,
               short_name : Rauni Kalan,
               types : [ locality, political ]
            },
            {
               long_name : Rupnagar,
               short_name : Rupnagar,
               types : [ administrative_area_level_2, political ]
            },
            {
               long_name : Punjab,
               short_name : PB,
               types : [ administrative_area_level_1, political ]
            },
            {
               long_name : India,
               short_name : IN,
               types : [ country, political ]
            },
            {
               long_name : 140102,
               short_name : 140102,
               types : [ postal_code ]
            }
         ],
         formatted_address : Rauni Kalan, Punjab 140102, India,
         geometry : {
            bounds : {
               northeast : {
                  lat : 30.8219531,
                  lng : 76.4462614
               },
               southwest : {
                  lat : 30.81705139999999,
                  lng : 76.4414979
               }
            },
            location : {
               lat : 30.8192965,
               lng : 76.44401660000001
            },
            location_type : APPROXIMATE,
            viewport : {
               northeast : {
                  lat : 30.8219531,
                  lng : 76.4462614
               },
               southwest : {
                  lat : 30.81705139999999,
                  lng : 76.4414979
               }
            }
         },
         place_id : ChIJ_f1PWh0CEDkRKlzFwhc62zo,
         types : [ locality, political ]
      },
      {
         address_components : [
            {
               long_name : 140102,
               short_name : 140102,
               types : [ postal_code ]
            },
            {
               long_name : Punjab,
               short_name : PB,
               types : [ administrative_area_level_1, political ]
            },
            {
               long_name : India,
               short_name : IN,
               types : [ country, political ]
            }
         ],
         formatted_address : Punjab 140102, India,
         geometry : {
            bounds : {
               northeast : {
                  lat : 30.869706,
                  lng : 76.47508049999999
               },
               southwest : {
                  lat : 30.7970006,
                  lng : 76.3335855
               }
            },
            location : {
               lat : 30.8288892,
               lng : 76.4215988
            },
            location_type : APPROXIMATE,
            viewport : {
               northeast : {
                  lat : 30.869706,
                  lng : 76.47508049999999
               },
               southwest : {
                  lat : 30.7970006,
                  lng : 76.3335855
               }
            }
         },
         place_id : ChIJHX2_rA0BEDkR_QslHcc_M0g,
         types : [ postal_code ]
      },
      {
         address_components : [
            {
               long_name : Fatehgarh Sahib,
               short_name : Fatehgarh Sahib,
               types : [ administrative_area_level_2, political ]
            },
            {
               long_name : Punjab,
               short_name : PB,
               types : [ administrative_area_level_1, political ]
            },
            {
               long_name : India,
               short_name : IN,
               types : [ country, political ]
            }
         ],
         formatted_address : Fatehgarh Sahib, Punjab, India,
         geometry : {
            bounds : {
               northeast : {
                  lat : 30.8779699,
                  lng : 76.63427999999999
               },
               southwest : {
                  lat : 30.4275501,
                  lng : 76.0761799
               }
            },
            location : {
               lat : 30.6810924,
               lng : 76.4100267
            },
            location_type : APPROXIMATE,
            viewport : {
               northeast : {
                  lat : 30.8779699,
                  lng : 76.63427999999999
               },
               southwest : {
                  lat : 30.4275501,
                  lng : 76.0761799
               }
            }
         },
         place_id : ChIJqf6CHWocEDkR4EljDGd5be8,
         types : [ administrative_area_level_2, political ]
      },
      {
         address_components : [
            {
               long_name : Punjab,
               short_name : PB,
               types : [ administrative_area_level_1, political ]
            },
            {
               long_name : India,
               short_name : IN,
               types : [ country, political ]
            }
         ],
         formatted_address : Punjab, India,
         geometry : {
            bounds : {
               northeast : {
                  lat : 32.4981352,
                  lng : 76.92175809999999
               },
               southwest : {
                  lat : 29.537147,
                  lng : 73.8708879
               }
            },
            location : {
               lat : 31.1471305,
               lng : 75.34121789999999
            },
            location_type : APPROXIMATE,
            viewport : {
               northeast : {
                  lat : 32.4981352,
                  lng : 76.92175809999999
               },
               southwest : {
                  lat : 29.537147,
                  lng : 73.8708879
               }
            }
         },
         place_id : ChIJVXOeVqpkGTkRYYijAzEmvY8,
         types : [ administrative_area_level_1, political ]
      },
      {
         address_components : [
            {
               long_name : India,
               short_name : IN,
               types : [ country, political ]
            }
         ],
         formatted_address : India,
         geometry : {
            bounds : {
               northeast : {
                  lat : 35.5087008,
                  lng : 97.395561
               },
               southwest : {
                  lat : 6.7535159,
                  lng : 68.162386
               }
            },
            location : {
               lat : 20.593684,
               lng : 78.96288
            },
            location_type : APPROXIMATE,
            viewport : {
               northeast : {
                  lat : 35.5087008,
                  lng : 97.395561
               },
               southwest : {
                  lat : 6.7535159,
                  lng : 68.16288519999999
               }
            }
         },
         place_id : ChIJkbeSa_BfYzARphNChaFPjNc,
         types : [ country, political ]
      }
   ],
   status : OK
}

我已将我的必填数据放在类型为本地性,政治性的**必填文本**中

我该怎么办

Chirag Shah

使用了这种方法

 NSURL *url = [NSURL URLWithString:[NSString stringWithFormat: @"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=false",30.819297, 76.444017]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    if (!error)
    {
        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

             NSArray *arr=[[json valueForKeyPath:@"results.address_components"] objectAtIndex:0];

        for (NSDictionary *dictAddress in arr)
        {
            if ([[dictAddress objectForKey:@"types"] count] > 0)
            {
                if([[[dictAddress objectForKey:@"types"] objectAtIndex:0] isEqualToString:@"locality"])
                {
                    return [dictAddress objectForKey:@"long_name"];
                }
                else if([[[dictAddress objectForKey:@"types"] objectAtIndex:0] isEqualToString:@"political"])
                {
                    return [dictAddress objectForKey:@"long_name"];
                }
            }
            else
            {
                return @"";
            }
        }
        return @"";
        }
    }
}];

使用这种方法,您将找到以“ locality”或“ political”开头的区域名称。

但是我已经获取了api响应的第一个对象,但是如果您需要的不是此对象,那么请放入循环以获取被调用的json对象。

希望这对您有帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Google Map API-Infowindow显示Google Place API信息

来自分类Dev

Google Map API-Infowindow显示Google Place API信息

来自分类Dev

Google Map Engine API中的信息窗口

来自分类Dev

Google Place API在Android中显示错误

来自分类Dev

解析Google Place API JSON

来自分类Dev

google place api图标列表

来自分类Dev

在Android上的Google LatLngBounds中搜索Google Place API

来自分类Dev

Google Map在iOS中闪烁

来自分类Dev

为什么在 Google API 中尝试使用 iOS 版 Place Autocomplete 时只得到 5 个结果而不是更多?

来自分类Dev

Google Place Autocomplete API下拉列表未显示在Modal中

来自分类Dev

在Google Place API中显示超过50公里的地方

来自分类Dev

Google Place API(地方搜索器)

来自分类Dev

使用Google Place API获取事件

来自分类Dev

如何获得评论数Google Place API?

来自分类Dev

Google Place Autocomplete API调用入门

来自分类Dev

google place搜寻api和“ Listing by”文字

来自分类Dev

google-place-api返回null json

来自分类Dev

google place搜寻api和“ Listing by”文字

来自分类Dev

Google Place API限制-非使用限制

来自分类Dev

Google Map API-foreach循环中的信息窗口

来自分类Dev

显示信息窗口的Javascript Google Map API问题

来自分类Dev

Google Map API在信息窗口中显示标记位置

来自分类Dev

Google Place API提取具有食物类型的餐厅的列表

来自分类Dev

Google Place API:无法从 google place api android 获取大陆名称

来自分类Dev

陷入Google Map API中的mousewheel事件

来自分类Dev

Google Map API中的Legend div

来自分类Dev

在Google Map API中检索公交线路或地铁上的详细信息

来自分类Dev

如何从iOS的Google Place API获取20个以上的可能性地点

来自分类Dev

iOS Swift-Google Place API(Web)以奇怪的格式返回JSON

Related 相关文章

  1. 1

    Google Map API-Infowindow显示Google Place API信息

  2. 2

    Google Map API-Infowindow显示Google Place API信息

  3. 3

    Google Map Engine API中的信息窗口

  4. 4

    Google Place API在Android中显示错误

  5. 5

    解析Google Place API JSON

  6. 6

    google place api图标列表

  7. 7

    在Android上的Google LatLngBounds中搜索Google Place API

  8. 8

    Google Map在iOS中闪烁

  9. 9

    为什么在 Google API 中尝试使用 iOS 版 Place Autocomplete 时只得到 5 个结果而不是更多?

  10. 10

    Google Place Autocomplete API下拉列表未显示在Modal中

  11. 11

    在Google Place API中显示超过50公里的地方

  12. 12

    Google Place API(地方搜索器)

  13. 13

    使用Google Place API获取事件

  14. 14

    如何获得评论数Google Place API?

  15. 15

    Google Place Autocomplete API调用入门

  16. 16

    google place搜寻api和“ Listing by”文字

  17. 17

    google-place-api返回null json

  18. 18

    google place搜寻api和“ Listing by”文字

  19. 19

    Google Place API限制-非使用限制

  20. 20

    Google Map API-foreach循环中的信息窗口

  21. 21

    显示信息窗口的Javascript Google Map API问题

  22. 22

    Google Map API在信息窗口中显示标记位置

  23. 23

    Google Place API提取具有食物类型的餐厅的列表

  24. 24

    Google Place API:无法从 google place api android 获取大陆名称

  25. 25

    陷入Google Map API中的mousewheel事件

  26. 26

    Google Map API中的Legend div

  27. 27

    在Google Map API中检索公交线路或地铁上的详细信息

  28. 28

    如何从iOS的Google Place API获取20个以上的可能性地点

  29. 29

    iOS Swift-Google Place API(Web)以奇怪的格式返回JSON

热门标签

归档