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

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

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

【问题讨论】:

    标签: xcode api location


    【解决方案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 部分..

    1. 下载一个json库..https://github.com/johnezang/JSONKit

    2. 将 JSONKit.h 和 JSONKit.m 添加到您的项目中。 (添加文件..或将它们拖过来)

    3. 添加#import "JSONKit.h"(在您的 .m 文件中)

    4. 查看下面的最终方法,了解如何设置变量并从 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项目的从头开发

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      相关资源
      最近更新 更多