【问题标题】:How to calculate distance between current location and selected location/destination from Localsearch (search bar) in iOS如何从 iOS 中的 Localsearch(搜索栏)计算当前位置与所选位置/目的地之间的距离
【发布时间】:2015-04-07 03:01:29
【问题描述】:

基本上,我使用了 locationSearch / MKLocationSearch 库,它的工作原理类似于 google 地方来为用户提供自动完成选项预测结果。在此方法之前,我要求用户在 UITextField 上输入他们的目的地,然后 CCLocationDistance 将计算当前位置与其输入位置之间的距离。在添加 locationSearch 之前,一切正常。

现在我的问题是我没有得到A和B之间距离的结果。我的假设是因为我对Objective-C不是很精通,我认为在这一类课程中;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.searchDisplayController setActive:NO animated:YES];

MKMapItem *item = results.mapItems[indexPath.row];
[mapView addAnnotation:item.placemark];
[mapView selectAnnotation:item.placemark animated:YES];

[mapView setCenterCoordinate:item.placemark.location.coordinate animated:YES];

[mapView setUserTrackingMode:MKUserTrackingModeNone];

}

我应该选择能够计算距离的项目,但我不知道该怎么做。

这是我在 mapviewcontroller.m 中的声明;

CLLocationDistance distance;

和反向地理转发我在下面使用这个sn-p; - (void)requestForwardGeoCoding:(NSString *)address { CLGeocoder geocoder = [[CLGeocoder alloc] init]; [geocoder geocodeAddressString: address completionHandler:^(NSArray placemarks, NSError* error){ for (CLPlacemark* aPlacemark in placemarks) {

        CLLocationCoordinate2D fromCoordinate = CLLocationCoordinate2DMake(self.mapView.userLocation.location.coordinate.latitude,self.mapView.userLocation.location.coordinate.longitude);


        CLLocationCoordinate2D toCoordinate   = CLLocationCoordinate2DMake(aPlacemark.location.coordinate.latitude ,aPlacemark.location.coordinate.longitude);

        NSLog(@"my location lat %f lon %f",self.mapView.userLocation.location.coordinate.latitude,self.mapView.userLocation.location.coordinate.longitude);

        MKPlacemark *fromPlacemark = [[MKPlacemark alloc] initWithCoordinate:fromCoordinate
                                                           addressDictionary:nil];

        MKPlacemark *toPlacemark   = [[MKPlacemark alloc] initWithCoordinate:toCoordinate
                                                           addressDictionary:nil];

        NSLog(@"Search Location lat %f  long %f", aPlacemark.location.coordinate.latitude , aPlacemark.location.coordinate.longitude );

        MKMapItem *fromItem = [[MKMapItem alloc] initWithPlacemark:fromPlacemark];

        MKMapItem *toItem   = [[MKMapItem alloc] initWithPlacemark:toPlacemark];

        [self findDirectionsFrom:fromItem
                              to:toItem];


        CLLocationDegrees latitude, longitude;

        latitude = aPlacemark.location.coordinate.latitude;
        longitude = aPlacemark.location.coordinate.longitude;
        CLLocation *to = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];

        CLLocationCoordinate2D location;
        location.latitude = latitude;
        location.longitude = longitude;
        VBAnnotation *ann1 = [[VBAnnotation alloc] initWithPosition:location];
        [ann1 setCoordinate:location];
        ann1.title = address;
        ann1.subtitle = @"Destination";
        [self.mapView addAnnotation:ann1];

        latitude = self.mapView.userLocation.location.coordinate.latitude;
        longitude = self.mapView.userLocation.location.coordinate.longitude;
        CLLocation *from = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];

         distance= [to distanceFromLocation:from];

        NSLog(@"Distance %.1f", distance/ 1000);


        location.latitude = latitude;
        location.longitude = longitude;
        VBAnnotation *ann = [[VBAnnotation alloc] initWithPosition:location];
        [ann setCoordinate:location];
        ann.title = @"My Location";
        ann.subtitle = @"Where you are";
        [self.mapView addAnnotation:ann];

        goThere = CLLocationCoordinate2DMake(aPlacemark.location.coordinate.latitude,aPlacemark.location.coordinate.longitude);


    }
}];

}

我不确定如何从搜索显示控制器 tableView 中获取所选项目的距离。

请指导我从预测结果中选择位置时如何获取距离。

提前致谢。

【问题讨论】:

    标签: ios objective-c location tableview geocoding


    【解决方案1】:

    使用此委托方法另请阅读 CoreLocation 的文档。 // 在 iOS 3.2 方法中已弃用 - (CLLocationDistance)getDistanceFrom:(const CLLocation *)location

    // 正确的方法 - (CLLocationDistance)distanceFromLocation:(const CLLocation *)location

    【讨论】:

      【解决方案2】:

      这里是查找两个位置之间距离的方法。

      [selectedLocation distanceFromLocation:currentLocation]
      

      【讨论】:

        猜你喜欢
        • 2014-05-20
        • 1970-01-01
        • 1970-01-01
        • 2017-04-20
        • 2013-04-13
        • 2012-10-05
        • 1970-01-01
        • 2011-12-01
        • 2016-12-22
        相关资源
        最近更新 更多