-1

我正在尝试通过 iPhone 应用程序上的 Google Places API 来完成特定国家/地区的美食场所,例如墨西哥菜、西班牙菜、泰国菜、印度菜。我怎样才能做到这一点??正如我看到的支持的类型,里面只有食物、餐厅等。除了每当我使用这个查询..

NSString *url = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=%@&type=restaurant&query=%@&sensor=true&key=%@", currentCentre.latitude, currentCentre.longitude, [NSString stringWithFormat:@"%i", currenDist], googleType, kGOOGLE_API_KEY];

我只得到很少的结果,每当我尝试添加更多类型(如食品|餐厅|场所)时,如下查询我的应用程序崩溃。

NSString *url = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=%@&type=food|restaurant|establishment&query=%@&sensor=true&key=%@", currentCentre.latitude, currentCentre.longitude, [NSString stringWithFormat:@"%i", currenDist], googleType, kGOOGLE_API_KEY];

正如我告诉我的应用程序因此错误而崩溃可能是我对 url 做错了什么。

*终止应用程序由于未捕获的异常'NSInvalidArgumentException',理由是: '数据参数是零' *第一掷调用堆栈:(0x13be022 0x200fcd6 0x1366a48 0x13669b9 0xbadfad 0x2dd2 0x13bfe42 0xaa09df 0x139294f 0x12f5b43 0x12f5424 0x12f4d84 0x12f4c9b 0x15d47d8 0x15d488a 0x17a626 0x236d 0x22d5)终止叫做抛出一个异常(lldb)

请问有人可以指导我吗?我希望我清除了我所有的观点。

谢谢和问候, 马尔哈尔

4

2 回答 2

0

您似乎正在尝试使用 Places API Textsearch,因为您正在使用该query参数。如果这是正确的,请确保您访问的是正确的后端:

maps.googleapis.com/maps/api/place/ textsearch /json

还要确保您使用正确的参数来指定类型:

类型=食物|餐厅|机构

我建议彻底阅读文档并通过浏览器执行一些测试 html 请求,以确保在将其插入应用程序之前有正确的请求。

于 2012-07-29T23:53:33.967 回答
0

尽管这已经快 7 个月了,而且我相信您现在已经解决了它或放弃了它,但我刚刚遇到了同样的问题,并且需要一些解决。我想我会把答案放在这里以帮助其他有类似错误的人。

我发现的问题与不喜欢该|字符的 URL 编码有关。

我发现的解决方法是:

// Create an NSString with the URL which includes the '|' characters.
NSString * urlString = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=51.189781,-114.488907&radius=6918&types=food|bar&keyword=&sensor=true&key=YOUR KEY HERE";

// Encode the URL
NSString* encodedURLString = [urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];

// Covert to a NSURL
NSURL *googleRequestURL=[NSURL URLWithString:encodedURLString];

然后使用googleRequestURL作为 URL。

希望这将为您解决!

于 2013-01-18T16:55:04.553 回答