我想让它显示两个CLLocation坐标之间的距离.没有复杂的数学公式,有没有办法做到这一点?如果没有,你会怎么做公式?
我正在寻找一个开源应用程序或库来跟踪后台的用户位置.现在我正在尝试使用CLLocation和后台任务,但准确性对我的情况来说还不够.你能解释一下,像"移动","管理员","endmondo"等应用程序如何创建我的路线?我应该使用Accelerometer或/和指南针在CLLocation背景点之间创建路线吗?
一些代码:
//location manager init
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.delegate = self;
#pragma mark - CLLocationManager Delegate
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if ([self isInBackground]) {
if (self.locationUpdatedInBackground) {
bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler: ^{
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
}];
self.locationUpdatedInBackground(newLocation);
[self endBackgroundTask];
}
} else {
if (self.locationUpdatedInForeground) {
self.locationUpdatedInForeground(newLocation);
}
}
}
Run Code Online (Sandbox Code Playgroud)
UPD:Justed使用下一个属性测试了我的应用程序
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.activityType = CLActivityTypeFitness;
self.locationManager.pausesLocationUpdatesAutomatically=NO;
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我在1.5小时旅行期间有大约10个被解雇的事件
在目标C(我没有经验)你可以用纬度和经度初始化一个CLLocation对象,如下所示:
CLLocation *myLocation = [[CLLocation alloc] initWithLatitude:your_latitiude_value longitude:your_longitude_value];
Run Code Online (Sandbox Code Playgroud)
但是我如何在Swift中做到这一点?
我试过这个,
let loc_coords = CLLocationCoordinate2D(latitude: your_latitiude_value, longitude: your_longitude_value)
let loc = CLLocation().coordinate(loc_coords)
Run Code Online (Sandbox Code Playgroud)
但我得到这个错误:
无法使用类型'(CLLocationCoordiate2D)'的参数列表调用"coordinate"
我需要它作为CLLocation对象,因为我想在locationManager中使用distanceFromLocation方法.
希望你能帮忙,谢谢!
latitude-longitude cllocation cllocationdistance cllocationcoordinate2d swift
在iOS 4.0之前,CoreLocation正确报告高度,现在它总是报告为0英尺.
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation *)oldLocation
{
NSString *tLatitude = [NSString stringWithFormat:@"%3.5f", newLocation.coordinate.latitude];
NSString *tLongitude = [NSString stringWithFormat:@"%3.5f", newLocation.coordinate.longitude];
/* the following returns 0 */
NSString *tAltitude = [NSString stringWithFormat:@"%i", newLocation.altitude];
/* theres more code, but it's not relevant,
and this worked prior to iOS 4.0*/
[manager stopUpdatingLocation];
}
Run Code Online (Sandbox Code Playgroud)
不在设备或模拟器上工作,是否还有其他人遇到此问题?
在这里发布的问题中,用户问:
我有一个充满经度和纬度的阵列.我的用户位置有两个双变量.我想测试用户位置与阵列之间的距离,以查看最接近的位置.我该怎么做呢?
这将获得2个位置之间的距离,但是要了解我如何针对一系列位置进行测试.
作为回应,他得到了以下代码:
NSArray *locations = //your array of CLLocation objects
CLLocation *currentLocation = //current device Location
CLLocation *closestLocation;
CLLocationDistance smallestDistance = DBL_MAX; // set the max value
for (CLLocation *location in locations) {
CLLocationDistance distance = [currentLocation distanceFromLocation:location];
if (distance < smallestDistance) {
smallestDistance = distance;
closestLocation = location;
}
}
NSLog(@"smallestDistance = %f", smallestDistance);
Run Code Online (Sandbox Code Playgroud)
我在我正在处理的应用程序中遇到完全相同的问题,我认为这段代码可以完美地运行.但是,我正在使用Swift,此代码在Objective-C中.
我唯一的问题是:它应该如何看待Swift?
谢谢你的帮助.我是所有这一切的新手,看到Swift中的这段代码可能是一个很大的问题.
有人可以帮我将以下Objective-C语句转换为Swift吗?
CLLocationDistance distance = [fromLocation distanceFromLocation:toLocation];
Run Code Online (Sandbox Code Playgroud)
我知道这样做一定很简单.我是iOS编程的全新,任何帮助将不胜感激.
谢谢!:-)
我正在使用NSLengthFormatter类来格式化用户和某个目标之间的距离.
CLLocation *userLocation; //<- Coordinates fetched from CLLocationManager
CLLocation *targetLocation; //<- Some location retrieved from server data
CLLocationDistance distance = [userLocation distanceFromLocation:targetLocation];
NSLengthFormatter *lengthFormatter = [NSLengthFormatter new];
NSString *formattedLength = [lengthFormatter stringFromMeters:distance];
Run Code Online (Sandbox Code Playgroud)
现在,如果长度小于1000米,则格式化的距离始终以码或米显示(取决于区域设置).
例如.如果距离= 450.0,格式化的字符串将是492.7码或450米.
如何调整NSLengthFormatter以仅以英里/公里为单位返回距离字符串?
我找不到如何将转换任何实例String来CLLocationDistance.例如:
let distance : String = "2000.0"
let mdf = MKDistanceFormatter()
mdf.units = .Metric
var clDistance = mdf.distanceFromString(distance)
Run Code Online (Sandbox Code Playgroud)
我总是得到clDistance = -1.
我确信这个问题有一个明显的解决方案,但我已经尝试了一段时间不同的东西,但我无法弄清楚。基本上我已经得到了两点之间的距离,并且我试图将它从 Double 转换为 Int(以删除所有小数点),但是我尝试过的一切都没有奏效。
这是我的代码:
let userLocation = CLLocation(latitude: mapView.userLocation.coordinate.latitude, longitude: mapView.userLocation.coordinate.longitude)
let annotationLocation = CLLocation(latitude: latitudePassed, longitude: longitudePassed)
var distance = CLLocationDistance(annotationLocation.distanceFromLocation(userLocation))
if distance > 1000 {
distance = distance / 1000
rideAnnotation.subtitle = "\(distance) kilometer(s)"
} else {
rideAnnotation.subtitle = "\(distance) meters"
}
Run Code Online (Sandbox Code Playgroud)
我尝试先转换为字符串,然后转换为 int,再转换为 inttoInt()和其他一些东西,但没有成功。
有人有什么建议吗?
嗨,我想在位置变化50米(不少于此)时呼叫网络服务。我已尝试使用重大更改,但它至少可以运行500米,并且startupdatelocations始终会调用。因此,我如何检测设备是否从该位置移动了50或100米。
在许多stackoverflow问题中,我都使用距离过滤器为50米。但是在移动到50米之前我无法获得设备中的位置更新信息。
这里有一些关于距离过滤器的解释-iPhone的核心位置:距离过滤器如何工作?
我想找到最近的用户到我的位置.(例如最多5km)我的firebase数据库中的节点如下面的结构,
+ users
+ user_id0
- latitude: _
- longitude: _
Run Code Online (Sandbox Code Playgroud)
是否有任何方法可以在该半径内获得确切的用户.否则我应该使用CLLocation distance方法检查他们最近位置的每个用户.
ios cllocationdistance firebase swift firebase-realtime-database
我正在完成一个应用程序希望我需要向用户显示他和大约500坐标之间的距离.
使用CLLocation方法计算它,效果很好,但在iPhone 4中大约需要1分钟才能完成每个位置的计算.
最好的方法是什么?用span?还有其他更快的方法吗
谢谢大家,
瑞
我正在使用此函数来获取从一个CLLocation到另一个CLLocation的距离
这里的venue.lat是NSNumber
GeoAddress *address = [[GeoAddress new] init];
CLLocationCoordinate2D coord;
coord.longitude = (CLLocationDegrees)[venue.lat doubleValue];
coord.latitude = (CLLocationDegrees)[venue.lng doubleValue];
address.latlng = [[CLLocation alloc] initWithLatitude:coord.latitude longitude: coord.longitude];
if (curUserLocation.coordinate.latitude){
address.distance = [address.latlng distanceFromLocation:curUserLocation];
NSLog(@" DISTANCE %f", address.distance);
NSLog(@" LOC1 %f lat %f lng", address.latlng.coordinate.latitude, address.latlng.coordinate.longitude);
NSLog(@" ME %f lat %f lng", curUserLocation.coordinate.latitude, curUserLocation.coordinate.longitude);
}
Run Code Online (Sandbox Code Playgroud)
这是Geoaddress:
// GeoAddress.h
@interface GeoAddress : NSObject
{
CLLocation * latlng;
CLLocationDistance distance;
}
@property CLLocationDistance distance;
@property (nonatomic, retain) CLLocation * latlng;
// GeoAddress.m
#import "GeoAddress.h"
@implementation …Run Code Online (Sandbox Code Playgroud) ios ×9
swift ×7
cllocation ×6
iphone ×2
objective-c ×2
altitude ×1
cocoa-touch ×1
distance ×1
firebase ×1
geolocation ×1
google-maps ×1
ios6 ×1