【问题标题】:Find distance longitude and latitude in array from user location从用户位置查找数组中的距离经度和纬度
【发布时间】:2014-12-15 21:02:27
【问题描述】:

我在计算我的位置与从从 Parse.com 上创建的数据库下载数据的数组获得的坐标之间的距离时遇到问题。我也想找到离我的位置最近的距离。我使用的代码是这样的:

    NSString *Lat = [[object objectForKey:@"Latitudine"] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString* Long = [[object objectForKey:@"Longitudine"] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    Latdouble = [Lat doubleValue];
    Longdouble = [Long doubleValue];


    NSArray *locations = [NSArray arrayWithObjects:[[CLLocation alloc] initWithLatitude:Latdouble longitude:Longdouble], nil];

    NSLog(@"LOCATIONS COORDINATE: %@", locations);

    CLLocation *currentLocation = [[CLLocation alloc] initWithLatitude:locationManager.location.coordinate.latitude longitude:locationManager.location.coordinate.longitude];;
    CLLocation *closestLocation;
    CLLocationDistance smallestDistance = DBL_MAX;

    for (CLLocation *location in locations) {
        CLLocationDistance distance = [currentLocation distanceFromLocation:location];

        NSLog(@" ");
        NSLog(@"DIST: %f", distance);
        NSLog(@" ");

        if (distance < smallestDistance) {
            distance = smallestDistance;
            closestLocation = location;

            NSLog(@"CLOSEST LOCATION: %@ \n\n DISTANCE: %f", closestLocation, distance);
        }
    }

编译后得到这个结果:

2014-12-15 21:54:37.101 Veg[28614:263678] LOCATIONS COORDINATE: (
    "<+45.45592700,+11.05733500> +/- 0.00m (speed -1.00 mps / course -1.00) @ 12/15/14, 10:08:08 PM Central European Standard Time"
)
2014-12-15 21:54:37.101 Veg[28614:263678] DIST: 5172045.683883
2014-12-15 21:54:37.101 Veg[28614:263678]  
2014-12-15 21:54:37.102 Veg[28614:263678] CLOSEST LOCATION: <+45.43574900,+10.98694600> +/- 0.00m (speed -1.00 mps / course -1.00) @ 12/15/14, 9:54:37 PM Central European Standard Time 

 DISTANCE: 
17976931348623157081452742373170435679807056752584499659891747680315726078002853876058955863276687817154
04589535143824642343213268894641827684675467035375169860499105765512820762454900903893289440758685084551
33942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.00
0000

怎么了?为什么我会获得如此巨大的价值?提前感谢您的帮助

【问题讨论】:

  • 你分配的方向错误,你想要smallestDistance = distance(例如你正在记录 DBL_MAX)
  • 我刚试了下,没办法
  • 但是 DBL_MAX 必须以#define 开头?
  • 您正在将 minimumDistance 初始化为 DBL_MAX。检查距离 if (distance < smallestDistance) { smallestDistance = distance; ...
  • 好的,我进行了更改,但我看到的是 Meters 中的 distranza,对吧?

标签: objective-c xcode localization parse-platform cllocationmanager


【解决方案1】:

你有:

if (distance < smallestDistance) {
    distance = smallestDistance;
    closestLocation = location;

将初始化为DBL_MAXsmallestDistance分配给distance,因此smallestDistance永远不会改变。

你需要什么:

if (distance < smallestDistance) {
    smallestDistance = distance;
    closestLocation = location;

distance 更小时,将smallestDistance 更新为distance

【讨论】:

    猜你喜欢
    • 2012-02-10
    • 1970-01-01
    • 2010-11-28
    • 1970-01-01
    • 2021-05-10
    • 2012-09-07
    • 1970-01-01
    • 1970-01-01
    • 2017-11-10
    相关资源
    最近更新 更多