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

Xcode 如何使用“我的位置”自动查找附近的位置?,xcode,api,location,Xcode,Api,Location,我对xcode非常陌生,但我要做的是,当你打开应用程序时,它会显示地图、我的位置以及我周围当前的美食地点。我没有我自己的数据库的位置,我正试图使用谷歌这个功能。我能做的是找到一个教程,但它有一个搜索栏,当我搜索的东西,它显示我,但我希望它自动显示我,而不是有搜索功能 这可能吗?非常感谢您的帮助/指导。谢谢您可以尝试使用位置搜索api。 他们支持“餐馆”和“食物”类型。 因此,您可以删除搜索栏,而是使用当前位置和types=“restaurant”或types=“restaurant | fo

我对xcode非常陌生,但我要做的是,当你打开应用程序时,它会显示地图、我的位置以及我周围当前的美食地点。我没有我自己的数据库的位置,我正试图使用谷歌这个功能。我能做的是找到一个教程,但它有一个搜索栏,当我搜索的东西,它显示我,但我希望它自动显示我,而不是有搜索功能


这可能吗?非常感谢您的帮助/指导。谢谢

您可以尝试使用位置搜索api。

他们支持“餐馆”和“食物”类型。

因此,您可以删除搜索栏,而是使用当前位置和types=“restaurant”或types=“restaurant | food”向google places api发送请求。 您可以将结果作为JSON数据获取,您可以在应用程序中轻松使用这些数据

下一步是制作注释并将其添加到地图中

__
以下是第一部分的详细信息。
一旦你完成了这项工作,你可以继续为google places获取一个API密钥,获取当前位置,然后开始使用地图注释向地图添加json结果。:)
异步url连接是这里最大的部分。所以,一旦你开始工作,你就可以找到附近的位置了

要设置JSON部件

  • 下载一个json库

  • 将JSONKit.h和JSONKit.m添加到项目中。(添加文件..或将其拖到上方)

  • 添加#导入“JSONKit.h”(在.m文件中)

  • 查看下面的最后一个方法,了解如何设置变量并从json获取数据

  • 对于url连接部分…
    基于:
    PS:稍后您将更改为使用google places json api url、api键、当前位置和“餐厅”类型(以从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 didReceiveData:(NSData *)data
    {
    
        // Append the new data to receivedData.
        [receivedData appendData:data];
    
    }
    
    有时连接会失败,然后调用此委托方法-您可以向用户显示一条消息(苹果说总是通知用户正在发生的事情)

    最后,当连接成功完成时调用此方法。现在您有了完整的数据,并将其保存到变量中。这里,我们还将数据放入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];
    
    }
    
    试着去做这件事,然后我们可以继续使用places search并向地图添加结果。

    参考以下链接


    该链接显示了MapAPI项目的从头开始的开发

    与我一样是编码新手,你有什么指导思想或类似的想法让我走上正确的方向吗?如何将请求发送到google?JSON:这是一个关于使用mapkit和JSON数据的很好的教程。本教程使用ASIHTTPRequest,它可以使发送请求变得更简单一点,但我会使用普通方法,因为它更快。查找asyncronus请求和/或asihttp文档。NSURLRequest*request=[NSURLRequest requestWithURL:[NSURL URLWithString:@]“我仍然很难弄清楚如何使用这个……有什么方法可以让你更详细地解释这个问题,或者把它降低到我的水平吗
    - (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]);
    
    }
    
    - (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];
    
    }