xcode 如何使用我的位置自动查找附近的位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7463340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to automatically find nearby locations using my location?
提问by Thomas rocks
I am VERY new to xcode but what i am trying to accomplish is when you open the app it shows a map and my location and the current food places that are around me. I dont have my own database for the locations i am trying to use google for this function. what i was able to do is find a tutorial but it has a search bar and when i search for something it shows me but i want it to automatically show me and not have the function of searching.
我对 xcode 非常陌生,但我想要完成的是,当您打开应用程序时,它会显示地图和我的位置以及我周围的当前食物地点。我没有自己的数据库,用于我尝试使用 google 执行此功能的位置。我能做的是找到一个教程,但它有一个搜索栏,当我搜索某些东西时,它会显示给我,但我希望它自动显示给我,而没有搜索功能。
Is this possible? any help/tutorial is greatly appreciated. Thank you
这可能吗?非常感谢任何帮助/教程。谢谢
回答by EO2
You can try to use Place search api. http://code.google.com/apis/maps/documentation/places/#PlaceSearches
您可以尝试使用 Place search api。 http://code.google.com/apis/maps/documentation/places/#PlaceSearches
They support a "restaurant" and "food" type. http://code.google.com/apis/maps/documentation/places/supported_types.html
他们支持“餐厅”和“食物”类型。 http://code.google.com/apis/maps/documentation/places/supported_types.html
So you can remove the search bar and instead send a request to google places api with current location and types="restaurant" or types="restaurant|food". You can get the results as JSON data wich you can easily use in your app.
因此,您可以删除搜索栏,而是使用当前位置和类型 =“餐厅”或类型 =“餐厅|食物”向谷歌地点 api 发送请求。您可以获得 JSON 数据形式的结果,您可以轻松地在您的应用程序中使用它。
Next step would be to make annotations and add them to a map.
下一步是制作注释并将它们添加到地图中。
__
Here are details on the first parts.
Once you get this working you can move on to getting an API-key for google places, get current position and then start adding json results to a map using map annotations. :)
The async url connection is the biggest part here. So once you get this working, you are well on your way to finding nearby locations.
__
这是第一部分的详细信息。
一旦你开始工作,你就可以继续为谷歌地点获取API 密钥,获取当前位置,然后开始使用地图注释将 json 结果添加到地图。:)
异步 url 连接是这里最大的部分。所以一旦你开始工作,你就可以很好地找到附近的位置。
To setup the JSON part..
要设置 JSON 部分..
Download a json library.. https://github.com/johnezang/JSONKit
Add JSONKit.h and JSONKit.m to your project. (add files.. or drag them over)
Add #import "JSONKit.h" (in your .m file)
Look in the final method below to see how to setup the variables and get data from json.
下载一个 json 库.. https://github.com/johnezang/JSONKit
将 JSONKit.h 和 JSONKit.m 添加到您的项目中。(添加文件.. 或将它们拖过去)
添加 #import "JSONKit.h"(在您的 .m 文件中)
查看下面的最终方法,了解如何设置变量并从 json 获取数据。
For the url connection part...
Based on: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html
PS: You would make a change later to use google places json api-url, api-key, current location and the "restaurant" type (to get the needed response in json from google).
Create a request:
对于 url 连接部分...
基于:http: //developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html
PS:您稍后会进行更改以使用 google放置 json api-url、api-key、当前位置和“餐厅”类型(从 google 获取所需的 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.
}
}
Then you need to implement the delegate methods.. (copy this into .m file) This one is invoked when you get response (not the actual data, hence they reset it).
然后你需要实现委托方法..(将它复制到 .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];
}
This one is invoked when data is received - can happen several times, so they append data each time.
这个在接收到数据时被调用 - 可能发生多次,所以他们每次都附加数据。
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Append the new data to receivedData.
[receivedData appendData:data];
}
Sometimes the connection would fail, then this delegate method is invoked - and you can present a message to the user (apple says to always inform users of what is happening)..
有时连接会失败,然后调用此委托方法 - 您可以向用户显示一条消息(苹果说要始终通知用户正在发生的事情)。
- (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]);
}
Finally, this method is invoked when the connection finished successfully. Now you have your complete data, and you save it to a variable. Here we also place the data into jsonDict.
最后,当连接成功完成时调用此方法。现在您有了完整的数据,并将其保存到一个变量中。这里我们也将数据放入 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];
}
Try to get this going, then we can get back to using places search and adding results to a map.
尝试进行此操作,然后我们可以重新使用地点搜索并将结果添加到地图中。
回答by Sivaprasad Km
Refer the following link http://www.raywenderlich.com/13160/using-the-google-places-api-with-mapkit
请参阅以下链接 http://www.raywenderlich.com/13160/using-the-google-places-api-with-mapkit
The link shows the from the scratch development of map API project
链接展示了地图API项目的从头开发