如果您是驱动程序,则可以使用Geolocation来检测您的当前位置。
根据此thread,您必须使用CLLocationManager
检索位置。
首先,添加CoreLocation.framework
到您的项目:
- 围棋在
Project Navigator
- 选择项目
- 单击该选项卡上
Build Phases
- 添加
CoreLocation.framework
在Link Binary with Libraries
然后,您只需按照Apple documentation的基本示例进行操作即可。
可能在你ViewDidLoad
创建CLLocationManager
:
if (nil == locationManager)
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
//Configure Accuracy depending on your needs, default is kCLLocationAccuracyBest
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
// Set a movement threshold for new events.
locationManager.distanceFilter = 500; // meters
[locationManager startUpdatingLocation];
随着每一个位置 更新时间CLLocationManagerDelegate
,您可以在Google Maps
更新用户的位置:
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations {
// If it's a relatively recent event, turn off updates to save power.
CLLocation* location = [locations lastObject];
NSDate* eventDate = location.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (abs(howRecent) < 15.0) {
// Update your marker on your map using location.coordinate.latitude
//and location.coordinate.longitude);
}
}
如果您使用本机MapKit.framework
,它也可以工作。您需要添加yourMapView.myLocationEnabled = YES;
并且框架将完成所有工作。 (除了在你的位置上放置地图)。
按照documentation的步骤操作。如果您想更新地图以跟随您的位置,可以复制框架目录中包含的Google示例MyLocationViewController.m
。他们只需在myLocation
属性中添加一个观察者即可更新相机属性。
要获得总行程,请检查此related question。它规定您需要将地址编码为纬度/经度,然后您可以使用CLLocation
框架计算距离。
要对地址进行地址解析,您可以使用forward geocoding API。
// get CLLocation fot both addresses
CLLocation *location = [[CLLocation alloc] initWithLatitude:address.latitude longitude:address.longitude];
// calculate distance between them
CLLocationDistance distance = [firstLocation distanceFromLocation:secondLocation];
您还可以检查此related link。
希望这会有所帮助!