我正在製作一個應用程序,獲取當前位置,名稱和地址,經度和緯度,但是如何找到最近的地方,如銀行,餐廳,巴士站在iPhone中當前位置附近。如何在iPhone中找到最近的位置,如銀行,餐廳
0
A
回答
0
您需要一個API來爲您提供這些東西,標準SDK中沒有一個。
2
您可以使用谷歌的服務,像這樣並把它在一個循環中,每次改變pointOfInterest字符串:
CLLocationCoordinate2D coordinate = location.coordinate;
NSString *pointOfInterest = @"banks";
NSString *URLString = [NSString stringWithFormat:@"http://ajax.googleapis.com/ajax/services/search/local?v=1.0&rsz=small&sll=%f,%f&q=%@",coordinate.latitude,coordinate.longitude, pointOfInterest];
URLString = [URLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:URLString]];
// Perform request and get JSON back as a NSData object
NSError *error = nil;
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
if(error != nil) {
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Error"
message:[error localizedDescription]
delegate:self
cancelButtonTitle:@"Done"
otherButtonTitles:nil] autorelease];
[alert show];
}
else {
// Get JSON as a NSString from NSData response
NSString *jsonString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
//Return the values in NSDictionary format
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *jsonResponse = [parser objectWithString:jsonString error:nil];
NSDictionary *responseData = [jsonResponse objectForKey:@"responseData"];
NSArray *results = [responseData objectForKey:@"results"];
}
你可以得到JSON API這裏:
0
你應該有代表銀行,餐廳或公共汽車的對象(示例中的電臺)。您可能希望它實現MKAnnotation協議,因爲您可能希望將它們添加到MKMapView中。每個對象都需要一個座標屬性(CLLocationCoordinate2D)。當我做這樣的事情時,我還添加了距離屬性(CLLocationDistance)。在viewcontroller中實例化這些對象時,您可以將它們添加到數組中。
現在,當用戶的應用程序更新時,您讓每個對象計算從該位置到自身的距離。
例如:
- (void)calculateDistance:(CLLocation *)location {
CLLocation *stationLocation = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
distance = [location distanceFromLocation:stationLocation];
[stationLocation release];
}
一旦你讓每一個對象計算出它的距離,你現在可以排序的陣列與遠處的物體。
[stations sortUsingSelector:@selector(compareDistance:)]
這要求你的對象有這樣的方法來實現:
- (NSComparisonResult)compareDistance:(Station *)aStation {
if (distance < aStation.distance) return NSOrderedAscending;
if (distance > aStation.distance) return NSOrderedDescending;
return NSOrderedSame;
}
現在你應該有與代表銀行等按距離排序的對象你的數組。
[stations objectAtIndex:0]
將關閉到您的位置。
相關問題
- 1. Google Street View API JavaScript最近的餐廳
- 2. 谷歌距離矩陣API - 找到一個給定的位置和最近的公交車站,餐廳等
- 3. 如何計算最近的餐廳,但我的代碼是顯示所有附近的餐館
- 4. 如何找到我附近的最近的10個位置
- 5. 當前的位置如何映射到餐館的餐廳查找應用程序android
- 6. 找到單點餐廳的列表
- 7. 找不到與'id'= 5的餐廳
- 8. 在Android中使用谷歌地圖查找最近的餐廳,咖啡廳和雜貨店
- 9. 如何在pos餐廳收據中添加餐桌名稱?
- 10. 獲取清真餐廳附近FourSquare API
- 11. 查詢找到最好的五家餐廳
- 12. 最近的酒吧和餐廳從api谷歌地圖改造
- 13. 如何在我的位置附近找到醫院位置?
- 14. 如何在我目前的位置附近找到最近的地方?
- 15. 從我們的位置找到位置最近的位置
- 16. 附近的餐廳顯示在地圖上查看使用用戶當前位置地圖套餐
- 17. Foursquare餐廳API
- 18. 餐廳模擬
- 19. 外賣餐廳查找幫助在php
- 20. 如何找到距離當前位置最近的經緯度?
- 21. 如何從默認位置找到最近的Vector?
- 22. 如何尋找到當前位置最近的元素用jQuery
- 23. Google maps api v2如何找到最近的位置
- 24. 如何編寫JavaScript代碼餐廳
- 25. 如何通過menuId獲取餐廳?
- 26. 如何在android中找到特定的附近位置
- 27. 查找STRtree中的最近位置
- 28. android,找到最近的位置,不經過所有的位置
- 29. 如何將POS餐廳備註添加到POS Restaurant reciept中?
- 30. 如何在openstreetmap中搜索最近的野餐桌和長凳?
什麼樣的api – user748001