如何使用我的位置自动查找附近的位置?

Posted

技术标签:

【中文标题】如何使用我的位置自动查找附近的位置?【英文标题】:How to automatically find nearby locations using my location? 【发布时间】:2011-11-19 18:30:42 【问题描述】:

我对 xcode 非常陌生,但我想要完成的是,当您打开应用程序时,它会显示地图和我的位置以及我周围的当前美食场所。我没有自己的数据库来存储我尝试使用 google 执行此功能的位置。我能做的是找到一个教程,但它有一个搜索栏,当我搜索它显示给我的东西时,我希望它自动显示给我并且没有搜索功能。

这可能吗?非常感谢任何帮助/教程。谢谢

【问题讨论】:

【参考方案1】:

您可以尝试使用 Place search api。 http://code.google.com/apis/maps/documentation/places/#PlaceSearches

它们支持“餐厅”和“食物”类型。 http://code.google.com/apis/maps/documentation/places/supported_types.html

因此,您可以删除搜索栏,而是使用当前位置和 types="restaurant" 或 types="restaurant|food" 向 google places api 发送请求。 您可以获得 JSON 数据形式的结果,您可以轻松地在您的应用中使用。

下一步是进行注释并将它们添加到地图中。

__ 以下是第一部分的详细信息。 完成这项工作后,您可以继续获取 Google 地方的 API 密钥,获取当前位置,然后开始使用地图注释将 json 结果添加到地图中。 :) 异步 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:稍后您将进行更改以使用 google places json api-url、api-key、当前位置和“餐厅”类型(从 google 获取所需的 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.

 

然后您需要实现委托方法..(将其复制到 .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];


尝试完成这项工作,然后我们可以重新使用地点搜索并将结果添加到地图中。

【讨论】:

和我一样是编码新手,您有任何教程想法或任何类似的东西可以让我朝着正确的方向前进吗?如何向谷歌发送请求? JSON: github.com/gabriel/yajl-objc 这是一个很好的使用 mapkit 的教程 - 带有 json 数据。 raywenderlich.com/2847/introduction-to-mapkit-on-ios-tutorial 本教程使用 ASIHTTPRequest,它可以让发送请求变得更简单——但我会使用正常的方式,因为它更快。查找 asyncronus 请求和/或 asihttp 文档。 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"maps.googleapis.com/maps/api/place/search/…; 我仍然很难弄清楚如何使用这个......有没有办法你可以解释更多或者把它简化到我的水平【参考方案2】:

参考以下链接 http://www.raywenderlich.com/13160/using-the-google-places-api-with-mapkit

该链接展示了地图API项目的从头开发

【讨论】:

以上是关于如何使用我的位置自动查找附近的位置?的主要内容,如果未能解决你的问题,请参考以下文章

在 Xcode 中使用拆分数据查找用户位置附近的数据

谷歌地图查找当前位置和附近的地方

Xcode:如何制作可以显示附近位置的地图?

使用相同的安卓应用在谷歌地图中查找附近设备的位置

如何在弹性搜索中使用纬度经度搜索位置附近的区域

在特定角度的位置附近查找 - 在圆形查询中只考虑前面的半圈而不是后面