我对xcode非常陌生,但我想要完成的是当您打开应用程序时,它会显示地图和我的位置以及当前位于我周围的食物场所。我没有我自己的数据库的位置,我正在尝试使用谷歌这个功能。我能够做的是找到一个教程,但它有一个搜索栏,当我搜索它显示我的东西,但我想它会自动显示我,并没有搜索功能。如何使用我的位置自动查找附近的位置?
这可能吗?任何帮助/教程是非常感谢。谢谢
我对xcode非常陌生,但我想要完成的是当您打开应用程序时,它会显示地图和我的位置以及当前位于我周围的食物场所。我没有我自己的数据库的位置,我正在尝试使用谷歌这个功能。我能够做的是找到一个教程,但它有一个搜索栏,当我搜索它显示我的东西,但我想它会自动显示我,并没有搜索功能。如何使用我的位置自动查找附近的位置?
这可能吗?任何帮助/教程是非常感谢。谢谢
你可以尝试使用地方搜索api。 http://code.google.com/apis/maps/documentation/places/#PlaceSearches
他们支持“餐厅”和“食物”类型。 http://code.google.com/apis/maps/documentation/places/supported_types.html
所以你可以删除搜索栏,而是发送一个请求到谷歌API与当前位置和类型=“餐厅”或类型=“餐厅|食品”。 你可以得到结果作为JSON数据,你可以很容易地在你的应用程序中使用。
下一步是制作注释并将它们添加到地图中。
__
这里是第一部分的细节。
一旦你得到这个工作,你可以继续获取API密钥谷歌地方,获取当前位置,然后开始使用地图注释添加json结果到地图。 :)
异步URL连接是这里最大的部分。所以一旦你得到这个工作,你很好地找到附近的位置。
要设置JSON一部分。这
下载一个JSON库.. https://github.com/johnezang/JSONKit
添加JSONKit.h和JSONKit.m到您的项目。在最后的方法(添加文件..或将它们拖动以上)
添加#进口“JSONKit.h”(在你的.m文件)
看看下面来看看如何设置变量,并得到来自json的数据。
的URL连接部分...基于
:http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html
PS:你会做出改变后使用谷歌则以JSON API-URL,API密钥,当前位置与“餐厅“类型(从谷歌获得json所需的响应)。
创建一个请求:
- (void)viewDidLoad
{
// Create the request.
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com/calendar/feeds/[email protected]/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.
}
}
然后,你需要实现委托方法。(复制到这个.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];
}
当接收到数据时可以调用此函数 - 可以发生多次,因此它们每次都追加数据。
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Append the new data to receivedData.
[receivedData appendData:data];
}
有时连接将失败,那么这种委托方法被调用 - 你可以提出一个消息给用户(苹果说总是告知发生了什么的用户)..
- (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]);
}
最后,当连接成功完成时调用此方法。现在你有完整的数据,并将其保存到一个变量中。在这里,我们也将数据放入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];
}
试图让这个打算,那么我们就可以回去使用地方搜索和添加结果地图。
请参考下面的链接 http://www.raywenderlich.com/13160/using-the-google-places-api-with-mapkit
链接显示了从地图API项目
作为新的编码,因为我,你有什么教程想法或诸如此类的事来的划痕发展让我走向正确的方向?我如何将请求发送给谷歌? –
JSON:https://github.com/gabriel/yajl-objc 这是一个关于使用mapkit的好教程 - 带有json数据。 http://www.raywenderlich.com/2847/introduction-to-mapkit-on-ios-tutorial 本教程使用ASIHTTPRequest,这可以使发送请求更简单一些 - 但我会使用正常的方式,因为它更快。 查找asyncronus请求和/或asihttp文档。 NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:@“https://maps.googleapis.com/maps/api/place/search/json?location=61.8670522,32.1957362&radius=500&types=restaurant&sensor=false&key=YOURE_API_KEY_GOES_HERE” ]]; – EO2
我仍然很难找出如何使用这个...有没有一种方法可以解释这更多或愚蠢的下降到我的水平请 –