简体   繁体   English

如何在iOS中通过Localsearch(搜索栏)计算当前位置与所选位置/目的地之间的距离

[英]How to calculate distance between current location and selected location/destination from Localsearch (search bar) in iOS

Basically I have used locationSearch / MKLocationSearch library that works similar to google places to give user autocomplete option prediction results. 基本上,我已经使用了locationSearch / MKLocationSearch库,该库的工作方式类似于Google地方信息,以便为用户提供自动填充选项预测结果。 Prior to this appoach I request user to input their destination on a UITextField then CCLocationDistance would calculate the distance between current location and their input location. 在此方法之前,我要求用户在UITextField上输入其目的地,然后CCLocationDistance将计算当前位置与其输入位置之间的距离。 That was working fine, until adding the locationSearch. 一切正常,直到添加locationSearch。

Now my issue is that I don't get the result of the distance between A and B. My assumption because I am not very well versed in Objective-C, I think in this line of class; 现在我的问题是我没有得到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];

} }

I should get the item selected to able to calculate the distance, but I don't know how to do that. 我应该选择能够计算距离的项目,但是我不知道该怎么做。

This is my declaration in the mapviewcontroller.m; 这是我在mapviewcontroller.m中的声明;

CLLocationDistance distance;

and the reversegeoforwarding I use this snippet below; 我在下面使用此代码段进行反向地理转发; - (void)requestForwardGeoCoding:(NSString *)address { CLGeocoder geocoder = [[CLGeocoder alloc] init]; -(void)requestForwardGeoCoding:(NSString *)address {CLGeocoder geocoder = [[CLGeocoder alloc] init]; [geocoder geocodeAddressString: address completionHandler:^(NSArray placemarks, NSError* error){ for (CLPlacemark* aPlacemark in placemarks) { [geocoder geocodeAddressString:地址完成处理程序:^(NSArray地标,NSError *错误){对于(CLPlacemark *地标中的aPlacemark){

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


    }
}];

} }

I am not sure how to get the distance of the selected item from the search display controller tableView. 我不确定如何从搜索显示控制器tableView中获取所选项目的距离。

Please guide me how to get the distance when select location from predicted results. 从预测结果中选择位置时,请指导我如何获取距离。

Thanks in advance. 提前致谢。

Use this delegate method Also read document for CoreLocation. 使用此委托方法另请参阅CoreLocation文档。 // Deprecated in iOS 3.2 method - (CLLocationDistance)getDistanceFrom:(const CLLocation *)location //在iOS 3.2方法中不推荐使用-(CLLocationDistance)getDistanceFrom:(const CLLocation *)location

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

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

[selectedLocation distanceFromLocation:currentLocation]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何计算我当前位置和MapView中其他Pins之间的距离 - How to calculate the distance between my current location and other Pins in MapView 在iOS中的固定点和我当前位置之间行走时计算距离 - Calculate distance while walking between a fixed point and my current location in iOS 查找自动完成结果位置与当前位置IOS的距离 - finding distance of a Autocomplete resulted location from current location IOS 如何计算用户当前位置和多个地理坐标之间的最短距离 - How to calculate shortest distance between user current location and multiple geo coordinate 如何计算当前位置与JSON文件中填充的位置之间的距离 - How to calculate the distance between my current location and locations populated in a JSON File 通过与其他位置的距离计算位置 - Calculate location by distance from other location 如何在地图视图 xamarin iOS 上绘制从当前位置到目的地位置的路线 - How to draw route from current to destination location on map view xamarin iOS 如何计算两个位置的距离? - How to calculate two location distance? 在iOS上按与当前位置的距离对实体进行排序的最佳策略是什么? - What is the best strategy to sort entities by distance from current location on iOS 根据道路计算两个位置之间的距离 - Calculate distance between two location based on roads
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM