附近场地到 iphone 成简单阵列
Posted
技术标签:
【中文标题】附近场地到 iphone 成简单阵列【英文标题】:nearby venue to iphone into simple array 【发布时间】:2012-04-20 10:46:13 【问题描述】:我真的很难弄清楚这一点。我已经阅读了一堆关于此的 PDFS 和 *** 问题,但我从未使用过 API 并且无法安静地得到它。
我正在尝试获取我的经度和纬度坐标并使用那里的任何 API(foursquare、谷歌地图、您可以推荐的任何其他免费的?)来获取附近公园的列表并将其放入 NSMutableDictionary key=name 对象=@“距离”。无需获取其所有内容,只需获取距离和名称即可。但我一直坚持第一部分,获得我的经度和纬度坐标超过 4 小时。谁能告诉我代码明智的how I would get my coordinates
,无需解释如何解析API,一旦我有了我的坐标,我觉得我可以弄清楚。我已经下载了苹果的 locateme 源代码,但仍然无法在我的上运行。是否有一组易于遵循的步骤或代码有人可以提供,我可以使用并解决这个问题?我已经导入了位置框架。有没有快速的 10-20 行代码来解决这个问题,而不是 3 个额外的视图控制器,我不太明白。
非常感谢
【问题讨论】:
【参考方案1】:- (void)viewDidLoad
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
#pragma mark Current location methods
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
latit = newLocation.coordinate.latitude;
longit = newLocation.coordinate.longitude;
NSLog(@"latitude = %f",latit);
NSLog(@"longitude = %f",longit);
[manager stopUpdatingLocation];
[self getNearByList];
-(void)getNearByList
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyyMMdd"];
NSString *currentDate = [dateFormat stringFromDate:[NSDate date]];
[dateFormat release];
NSURL *url = [NSURL URLWithString:[[NSString stringWithFormat:@"https://api.foursquare.com/v2/venues/search?ll=%f,%f&client_id=YOUR_CLIENT_ID_HERE&client_secret=YOUR_CLIENT_SECRET_HERE&v=%@&limit=100&radius=2000&categoryId=4bf58dd8d48988d1e0931735,4bf58dd8d48988d176941735,4bf58dd8d48988d1ed941735,4bf58dd8d48988d119951735,4bf58dd8d48988d123941735,4d954afda243a5684865b473,4edd64a0c7ddd24ca188df1a,4bf58dd8d48988d1c9941735,4bf58dd8d48988d1bd941735,4bf58dd8d48988d124951735,4bf58dd8d48988d115951735,4bf58dd8d48988d11a951735,4bf58dd8d48988d10c951735,4bf58dd8d48988d11b951735,4bf58dd8d48988d11e951735,4bf58dd8d48988d1f9941735,4bf58dd8d48988d1f5941735,4bf58dd8d48988d113951735,4f04afc02fb6e1c99f3db0bc,4bf58dd8d48988d110951735,4bf58dd8d48988d1f2941735,4bf58dd8d48988d1fd941735,4bf58dd8d48988d103951735,4bf58dd8d48988d104941735,4f04aa0c2fb6e1c99f3db0b8,4d1cf8421a97d635ce361c31,4bf58dd8d48988d1f8941735,4d4b7105d754a06374d81259,4bf58dd8d48988d16d941735,4bf58dd8d48988d128941735,4bf58dd8d48988d111951735,4bf58dd8d48988d1ca941735,4bf58dd8d48988d117951735,4bf58dd8d48988d107951735,4bf58dd8d48988d1fa941735,4bf58dd8d48988d118951735,4bf58dd8d48988d17f941735,4bf58dd8d48988d1ac941735,4bf58dd8d48988d180941735",latit,longit,currentDate] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; //YOU CAN FOUND MORE CATEGORY ID AT FOURSQURE WEBSITE..
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url cachePolicy: NSURLRequestReloadIgnoringCacheData timeoutInterval: 30.f];
NSLog(@"url = %@",urlRequest);
webData1 = [[NSMutableData data] retain];
urlConnection = [[NSURLConnection alloc] initWithRequest: urlRequest delegate: self];
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
[webData1 setLength: 0];
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
[webData1 appendData:data];
//NSLog(@"webData1 = %@",webData1);
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"Error"
message : @"An error has occured."
delegate:nil
cancelButtonTitle :@"OK"
otherButtonTitles :nil];
[alert1 show];
[alert1 release];
[webData1 release];
[connection release];
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
NSString *data = [[NSString alloc] initWithBytes: [webData1 mutableBytes] length:[webData1 length] encoding:NSUTF8StringEncoding];
NSLog(@"data = %@",data);
[connection release];
[webData1 release];
【讨论】:
【参考方案2】:使用 Google Place Api。您只需注册即可获得 api 的密钥
//示例 api。此 api 将以 json 数据格式为您提供定义的经纬度半径内的食物
https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&types=food&name=harbour&sensor=false&key=AddYourOwnKeyHere
Google Place API
希望,这会帮助你..享受......
【讨论】:
以上是关于附近场地到 iphone 成简单阵列的主要内容,如果未能解决你的问题,请参考以下文章
寻找 Iphone 教程应用程序来搜索我所在位置周围的场地 [关闭]