我想从 iPhone 获取 GPS 坐标并将这些 GPS 坐标发送到网络服务。该网络服务将获取我的 GPS 坐标并向我发送距离当前位置最近的 ATM 的位置。
现在我想分两个阶段进行。
第一阶段,我只想将 GPS 坐标发送到网络服务,作为回报,我想要 ATM 位置的地址。
第二阶段,我想在iPhone应用程序中显示的地图上指向这个ATM。
我开发了网络服务,它需要 2 个输入参数:纬度和经度。
并以字符串格式返回 ATM 位置的地址。
从第 1 阶段开始:请帮助我了解如何获取 GPS 坐标并将其发送到网络服务。
这样我就可以在视图上以字符串格式显示地址(我从网络服务获得的结果)。
将 Core Location 添加到您的项目中:
- 突出应用目标。
- 单击构建阶段选项卡并展开将二进制文件与库链接部分。
- 然后单击 + 按钮并选择 CoreLocation 框架。
头文件 (.h)
注意委托的使用!
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@class DetailViewController;
@interface MasterViewController : UITableViewController<CLLocationManagerDelegate>
@property (nonatomic, retain) CLLocationManager *locationManager;
@end
实施文件(.m)
#import "MasterViewController.h"
@implementation MasterViewController
#pragma mark - Properties
@synthesize locationManager;
#pragma mark - Methods
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
[self initializeMenuItems];
if (self.locationManager == nil)
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.desiredAccuracy =
kCLLocationAccuracyNearestTenMeters;
self.locationManager.delegate = self;
}
[self.locationManager startUpdatingLocation];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
// Turn off the location manager to save power.
[self.locationManager stopUpdatingLocation];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1. Get the current location
CLLocation *curPos = locationManager.location;
NSString *latitude = [[NSNumber numberWithDouble:curPos.coordinate.latitude] stringValue];
NSString *longitude = [[NSNumber numberWithDouble:curPos.coordinate.longitude] stringValue];
NSLog(@"Lat: %@", latitude);
NSLog(@"Long: %@", longitude);
}
- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"%@", @"Core location has a position.");
}
- (void) locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
NSLog(@"%@", @"Core location can't get a fix.");
}
@end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)