标签: cllocationdistance

如何找出坐标之间的距离?

我想让它显示两个CLLocation坐标之间的距离.没有复杂的数学公式,有没有办法做到这一点?如果没有,你会怎么做公式?

cllocation ios cllocationdistance swift

43
推荐指数
4
解决办法
6万
查看次数

如何在后台跟踪用户位置?

我正在寻找一个开源应用程序或库来跟踪后台的用户位置.现在我正在尝试使用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个被解雇的事件

geolocation core-location ios cllocationdistance ios6

20
推荐指数
3
解决办法
2万
查看次数

使用纬度和经度在swift中初始化CLLocation对象

在目标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

20
推荐指数
2
解决办法
3万
查看次数

iOS CoreLocation Altitude

在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)

不在设备或模拟器上工作,是否还有其他人遇到此问题?

iphone core-location ios altitude cllocationdistance

10
推荐指数
1
解决办法
9450
查看次数

从用户位置查找数组中最接近的经度和纬度 - iOS Swift

这里发布的问题中,用户问:

我有一个充满经度和纬度的阵列.我的用户位置有两个双变量.我想测试用户位置与阵列之间的距离,以查看最接近的位置.我该怎么做呢?

这将获得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中的这段代码可能是一个很大的问题.

google-maps cllocationmanager ios cllocationdistance swift

8
推荐指数
2
解决办法
9175
查看次数

CLLocation distanceFromLocation(在Swift?中)

有人可以帮我将以下Objective-C语句转换为Swift吗?

CLLocationDistance distance = [fromLocation distanceFromLocation:toLocation];
Run Code Online (Sandbox Code Playgroud)

我知道这样做一定很简单.我是iOS编程的全新,任何帮助将不胜感激.

谢谢!:-)

cllocation cllocationdistance swift

7
推荐指数
1
解决办法
9308
查看次数

NSLengthFormatter获取stringFromMeters:仅以英里/公里为单位

我正在使用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以仅以英里/公里为单位返回距离字符串?

objective-c ios cllocationdistance

5
推荐指数
1
解决办法
2387
查看次数

将String转换为CLLocationDistance

我找不到如何将转换任何实例StringCLLocationDistance.例如:

let distance : String = "2000.0"
let mdf = MKDistanceFormatter()
mdf.units = .Metric
var clDistance = mdf.distanceFromString(distance)
Run Code Online (Sandbox Code Playgroud)

我总是得到clDistance = -1.

cllocationdistance swift

3
推荐指数
1
解决办法
830
查看次数

将 distanceFromLocation 转换为 Int

我确信这个问题有一个明显的解决方案,但我已经尝试了一段时间不同的东西,但我无法弄清楚。基本上我已经得到了两点之间的距离,并且我试图将它从 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()和其他一些东西,但没有成功。

有人有什么建议吗?

cllocation ios cllocationdistance swift

2
推荐指数
1
解决办法
2231
查看次数

通知用户每50/100米移动一次iOS

嗨,我想在位置变化50米(不少于此)时呼叫网络服务。我已尝试使用重大更改,但它至少可以运行500米,并且startupdatelocations始终会调用。因此,我如何检测设备是否从该位置移动了50或100米。

在许多stackoverflow问题中,我都使用距离过滤器为50米。但是在移动到50米之前我无法获得设备中的位置更新信息。

这里有一些关于距离过滤器的解释-iPhone的核心位置:距离过滤器如何工作?

cocoa-touch cllocationmanager ios cllocationdistance

1
推荐指数
1
解决办法
1370
查看次数

如何使用Swift查询Firebase中最近的用户?

我想找到最近的用户到我的位置.(例如最多5km)我的firebase数据库中的节点如下面的结构,

+ users
  + user_id0
    - latitude: _
    - longitude: _
Run Code Online (Sandbox Code Playgroud)

是否有任何方法可以在该半径内获得确切的用户.否则我应该使用CLLocation distance方法检查他们最近位置的每个用户.

ios cllocationdistance firebase swift firebase-realtime-database

1
推荐指数
1
解决办法
1963
查看次数

计算2坐标之间的距离iphone - 最佳实践

我正在完成一个应用程序希望我需要向用户显示他和大约500坐标之间的距离.

使用CLLocation方法计算它,效果很好,但在iPhone 4中大约需要1分钟才能完成每个位置的计算.

最好的方法是什么?用span?还有其他更快的方法吗

谢谢大家,

iphone distance cllocation cllocationdistance

0
推荐指数
1
解决办法
3103
查看次数

CLLocationDistance返回完全错误的值

我正在使用此函数来获取从一个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)

objective-c cllocation ios cllocationdistance

0
推荐指数
1
解决办法
1793
查看次数