主题“旅行追踪器”
1.有没有办法跟踪当前位置并直接从应用程序获取当前位置的地址。
2.如何绘制从用户开始旅行的路线到用户停止旅行的路线。
3.主要的是我要显示当前地址。
4.请允许任何人指导我参与此活动。
答案 0 :(得分:0)
您可以使用CLLocationManager
在.h
CLLocationManager *locationManager;
CLLocationCoordinate2D currentLocation;
在.m
if (!locationManager) {
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = (id)self;
locationManager.distanceFilter = 100;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
}
[locationManager startUpdatingLocation];
在CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
currentLocation = newLocation.coordinate;
}
通过使用当前位置纬度和经度,您可以获得地址。
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
CLLocation *location = [[CLLocation alloc] initWithLatitude:currentLocation.latitude longitude:currentLocation.longitude];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
if (error) {
NSLog(@"failed with error: %@", error);
return;
}
if(placemarks.count > 0) {
NSString *MyAddress = @"";
NSString *city = @"";
CLPlacemark *placemark = [placemarks objectAtIndex:0];
if([placemark.addressDictionary objectForKey:@"FormattedAddressLines"] != NULL)
MyAddress = [[placemark.addressDictionary objectForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
else
MyAddress = @"Address Not founded";
NSLog (@"Address : %@",MyAddress);
}
}];