Tho*_*cks 2 api xcode location
我对xcode非常陌生,但我想要完成的是当你打开应用程序时它会显示一张地图和我的位置以及我周围的当前美食地点.我没有自己的数据库我想尝试使用谷歌这个功能的位置.我能做的是找到一个教程,但它有一个搜索栏,当我搜索它给我看的东西,但我希望它自动显示我,而不是具有搜索功能.
这可能吗?任何帮助/教程非常感谢.谢谢
您可以尝试使用Place search api. http://code.google.com/apis/maps/documentation/places/#PlaceSearches
他们支持"餐馆"和"食物"类型. http://code.google.com/apis/maps/documentation/places/supported_types.html
因此,您可以删除搜索栏,而是向Google地方api发送请求,其中包含当前位置和类型="restaurant"或types ="restaurant | food".您可以将结果作为JSON数据获得,您可以在应用中轻松使用.
下一步是制作注释并将它们添加到地图中.
__
以下是第一部分的详细信息.
一旦你开始工作,你可以继续获取谷歌地方的API密钥,获取当前位置,然后开始使用地图注释将json结果添加到地图.:)
async url连接是这里最重要的部分.因此,一旦你开始工作,你就可以找到附近的地点了.
要设置JSON部分..
下载一个json库.. https://github.com/johnezang/JSONKit
将JSONKit.h和JSONKit.m添加到您的项目中.(添加文件..或拖动它们)
添加#import"JSONKit.h"(在您的.m文件中)
查看下面的最终方法,了解如何设置变量并从json获取数据.
对于URL连接部分...
基于:http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html
PS:你会做出改变以后使用谷歌放置json api-url,api-key,当前位置和"餐馆"类型(从谷歌获得json所需的响应).
创建请求:
- (void)viewDidLoad
{
// Create the request.
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com/calendar/feeds/developer-calendar@google.com/public/full?alt=json"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
// Create the NSMutableData to hold the received data.
receivedData = [[NSMutableData data] retain];
} else {
// Inform the user that the connection failed.
}
}
Run Code Online (Sandbox Code Playgroud)
然后你需要实现委托方法..(将其复制到.m文件中)当你得到响应时调用这个(不是实际的数据,因此他们重置它).
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// This method is called when the server has determined that it
// has enough information to create the NSURLResponse.
// It can be called multiple times, for example in the case of a
// redirect, so each time we reset the data.
[receivedData setLength:0];
}
Run Code Online (Sandbox Code Playgroud)
收到数据时会调用此数据 - 可能会多次发生,因此每次都会附加数据.
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Append the new data to receivedData.
[receivedData appendData:data];
}
Run Code Online (Sandbox Code Playgroud)
有时连接会失败,然后调用这个委托方法 - 你可以向用户显示一条消息(苹果总是告诉用户发生了什么).
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
// release the connection, and the data object
[connection release];
[receivedData release];
// inform the user
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
Run Code Online (Sandbox Code Playgroud)
最后,在连接成功完成时调用此方法.现在您拥有完整的数据,并将其保存到变量中.这里我们还将数据放入jsonDict.
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// do something with the data ...
// Your data is now stored in "receivedData",
// set up this mutable variable in the header file, then synthesize.
// in the .h file, inside the @interface block.:
//
// NSMutableData *receivedData;
// NSDictionary *jsonDict;
// }
//
// @protocol (nonatomic, retain) NSMutableData *receivedData;
// @protocol (nonatomic, retain) NSDictionary *jsonDict;
// And in the .m file, under the @implementation line.
// @synthesize receivedData, jsonDict;
// Log to test your connection
NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
// Place the received data into a json dictionary
jsonDict = [receivedData objectFromJSONData];
// Get sections from your data
NSString *feed = [jsonDict objectForKey:@"feed"]; // asumes there is only one title in your json data, otherwise you would use an array (with dictionary items) ..look in your feed to find what to use.
// Log your data
NSLog(@"My feed: %@", feed);
// release the connection, and the data object
[connection release];
[receivedData release];
}
Run Code Online (Sandbox Code Playgroud)
尝试实现这一目标,然后我们可以回到使用地点搜索并将结果添加到地图.
| 归档时间: |
|
| 查看次数: |
12171 次 |
| 最近记录: |