我对objective-c非常陌生,并且尝试了多种资源来找到答案。到目前为止没有运气。请帮帮我。
我正在构建一个具有位置功能的应用程序。当覆盖给定距离时,应用程序必须发出警报。因此我必须保存一个初始位置。移动时必须显示实际位置和距离。
我所做的是以下内容:
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
//display latitude
NSString *lat =[[NSString alloc]initWithFormat:@"%f",
newLocation.coordinate.latitude];
latitude.text=lat;
//latr = lat;
//display longitude
NSString *lon = [[NSString alloc]initWithFormat:@"%f",
newLocation.coordinate.longitude];
longitude.text=lon;
//longr = lon;
//display accuracy
//NSString *acc = [[NSString alloc]initWithFormat:@"%f",
// newLocation.horizontalAccuracy];
//accuracy.text=acc;
}
-(void)recordLocation{
double valueLat = [[latitude text]doubleValue];
int latSeconds = valueLat * 3600;
int latDegrees = latSeconds / 3600;
latSeconds = abs(latSeconds % 3600);
int latMinutes = latSeconds / 60;
latSeconds %= 60;
char latChar = (latitude > 0) ? 'N' : 'S';
double valueLon = [[longitude text]doubleValue];
int lonSeconds = valueLon * 3600;
int lonDegrees = lonSeconds / 3600;
lonSeconds = abs(lonSeconds % 3600);
int lonMinutes = lonSeconds / 60;
lonSeconds %= 60;
char lonChar = (longitude > 0) ? 'E' : 'W';
CLLocation *tempPos = [[CLLocation alloc] initWithLatitude:valueLat longitude:valueLon];
NSString *recPosition = [[NSString alloc]initWithFormat:@"%i° %i' %i\" %c" " " @"%i° %i' %i\" %c \n", latDegrees, latMinutes, latSeconds, latChar,lonDegrees, lonMinutes, lonSeconds, lonChar];
_labelDegrees.text = recPosition;
NSLog(@"%f" , valueLat);
NSLog(@"%f" , valueLon);
NSLog (@"%@", tempPos);
[self distanceFromLocation:tempPos];
}
- (CLLocationDistance)distanceFromLocation:(const CLLocation *)tempPos{
double lat1 = [[latitude text] doubleValue];
double lon1 = [[longitude text] doubleValue];
CLLocation *currentLocation = [[CLLocation alloc] initWithLatitude:lat1 longitude:lon1];
CLLocationDistance locDistance = [currentLocation distanceFromLocation:tempPos];
NSString *distText = [[NSString alloc]initWithFormat:@"%f", locDistance];
NSLog(@"%@", currentLocation);
NSLog(@"%f", locDistance);
NSLog(@"%@", distText);
[lonDistance setText:distText];
}
它可以工作,但只有一次,当我单击界面中的按钮时。没有 locDistance 的自动更新。初始位置自动更新(在互联网上找到),该位置的记录也可以。我的代码有什么问题。这可能是一个简单的问题,但我无法找到解决方案。
(请原谅我糟糕的英语,我是荷兰人)
预先感谢您的帮助
问候,
阿德里