简体   繁体   English

根据道路计算两个位置之间的距离

[英]Calculate distance between two location based on roads

I have two MKCoordinateRegion objects. 我有两个MKCoordinateRegion对象。 Based on values from those objects I make two annotations on map. 基于这些对象的值,我在地图上做了两个annotations Then I calculate distance between those two locations: 然后,我计算出这两个位置之间的距离:

CLLocationCoordinate2D pointACoordinate = [ann coordinate];
    CLLocation *pointALocation = [[CLLocation alloc] initWithLatitude:pointACoordinate.latitude longitude:pointACoordinate.longitude];  

    CLLocationCoordinate2D pointBCoordinate = [ann2 coordinate];
    CLLocation *pointBLocation = [[CLLocation alloc] initWithLatitude:pointBCoordinate.latitude longitude:pointBCoordinate.longitude];  

    float distanceMeters = [pointBLocation distanceFromLocation:pointALocation];

    distanceMeters = distanceMeters / 1000;

But I ma not sure that values I get is correct. 但是我不确定我得到的价值观是否正确。
Are those values air distance? 这些值是空中距离吗?
Is it possible to get distance based on roads ? 是否可以根据道路获得距离?
I need distance that user must pass with car. 我需要用户必须通过汽车的距离。

Theses values are air distance. 这些值是空中距离。

You can't find the distance based on roads with the Apple SDK. 使用Apple SDK无法找到基于道路的距离。 Try to ask directly to google APIs http://code.google.com/intl/fr-FR/apis/maps/index.html 尝试直接询问Google API http://code.google.com/intl/fr-FR/apis/maps/index.html

Use CLLocation instead of CLLocationCoordinate:- 使用CLLocation而不是CLLocationCoordinate:-

CLLocation has an init method named CLLocation有一个名为的初始化方法

-(id)initWithLatitude:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude. 

Then use 然后使用

- (CLLocationDistance)getDistanceFrom:(const CLLocation *)location 

to get the distance between two CLLocation objects on Road. 获取Road上两个CLLocation对象之间的距离。

The Distance you will get is in kilometers. 您将获得的距离以公里为单位。

Since iOS7 you can get that info with this: 从iOS7开始,您可以通过以下方式获取该信息:

+ (void)distanceByRoadFromPoint:(CLLocationCoordinate2D)fromPoint
                        toPoint:(CLLocationCoordinate2D)toPoint
              completionHandler:(MKDirectionsHandler)completionHandler {

    MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
    request.transportType = MKDirectionsTransportTypeAutomobile;

    request.source = [self mapItemFromCoordinate:fromPoint];
    request.destination = [self mapItemFromCoordinate:toPoint];

    MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
    [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse * routeResponse, NSError *routeError) {

         MKRoute *route = [routeResponse.routes firstObject];
         CLLocationDistance distance = route.distance;
         NSTimeInterval expectedTime = route.expectedTravelTime;

         //call a completion handler that suits your situation

     }];    

   }


+ (MKMapItem *)mapItemFromCoordinate:(CLLocationCoordinate2D)coordinate {

    MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil];
    MKMapItem *item = [[MKMapItem alloc] initWithPlacemark:placemark];

    return item;

}

what @coolanilkothari says is almost correct almost except for the fact that getDistanceFrom is deprecated in ios 3.2. @coolanilkothari所说的几乎是正确的,除了在iOS 3.2中不赞成使用getDistanceFrom。 This is what the apple docs have to say.. 这就是苹果文档必须说的。

getDistanceFrom: getDistanceFrom:

Returns the distance (in meters) from the receiver's location to the specified location. 返回从接收器位置到指定位置的距离(以米为单位)。 (Deprecated in iOS 3.2. Use the distanceFromLocation: method instead.) - (CLLocationDistance)getDistanceFrom:(const CLLocation *)location Parameters (在iOS 3.2中已弃用。请改用distanceFromLocation:方法。)-(CLLocationDistance)getDistanceFrom:(const CLLocation *)location参数

location 位置

 The other location. 

Return Value 返回值

The distance (in meters) between the two locations. 两个位置之间的距离(以米为单位)。 Discussion 讨论区

This method measures the distance between the two locations by tracing a line between them that follows the curvature of the Earth. 该方法通过跟踪两个位置之间遵循地球曲率的直线来测量两个位置之间的距离。 The resulting arc is a smooth curve and does not take into account specific altitude changes between the two locations. 产生的弧线是平滑的曲线,并且没有考虑两个位置之间的特定高度变化。 Availability 可用性

 Available in iOS 2.0 and later. Deprecated in iOS 3.2. 

Declared In CLLocation.h 在CLLocation.h中声明

you have to just pass origin and destination co-ordinate and then parse the result. 您只需要传递起点和终点的坐标,然后解析结果即可。

http://maps.googleapis.com/maps/api/distancematrix/xml?origins=Vancouver+BC&destinations=San+Francisco&sensor=false http://maps.googleapis.com/maps/api/distancematrix/xml?origins=Vancouver+BC&destinations=San+Francisco&sensor=false

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM