iOS:获取位置之间的距离

时间:2013-03-20 15:57:14

标签: ios mkmapview mapkit geocoding

请勿在未阅读整个问题的情况下将其标记为重复

有没有办法在iOS中的两个CLLocation点之间获取驱动的路线的长度,没有显示地图?

不像乌鸦飞!但就像在MK驾驶模式中一样

我之后无法使用Google,因为我必须在Google地图上显示结果。

我找到了这段代码,但这仅在显示iOS-Map时有效:

-(void)showMapRouteFromCurrentLocation
{
    CLLocationCoordinate2D currentUserCoordinate = YourCoordinate;
    //currentUserCoordinate.latitude=  +53.509980;
    //currentUserCoordinate.longitude =  -0.133700;

    MKPlacemark *place = [[MKPlacemark alloc]
                          initWithCoordinate:currentUserCoordinate
                          addressDictionary:nil];


    MKMapItem *mapItem = [[MKMapItem alloc]initWithPlacemark:place];

    NSDictionary *routeOption = [[NSDictionary alloc] initWithObjectsAndKeys:MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsDirectionsModeKey, nil];

    [mapItem openInMapsWithLaunchOptions:routeOption];
}

但这不是我需要的。

5 个答案:

答案 0 :(得分:6)

正如脑力劳动所说,从iOS7开始,你可以做到以下几点:

MKDirectionsRequest *routeRequest = [[MKDirectionsRequest alloc] init];
routeRequest.transportType = MKDirectionsTransportTypeAutomobile;
[routeRequest setSource:startPoint]; // startPoint is an MKMapItem with the starting lat/long coordinate
[routeRequest setDestination :endPoint]; // endPoint an MKMapItem with the ending lat/long coordinate

MKDirections *routeDirections = [[MKDirections alloc] initWithRequest:routeRequest];
[routeDirections calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse * routeResponse, NSError *routeError)
{
    // Check for error and then do something with the route...
    MKRoute *route = routeResponse.routes[0];
}];

答案 1 :(得分:3)

据我所知,使用Apple的API无法做到这一点。您需要使用第三方系统来询问两点之间的路线。像谷歌一样,但它们经常有其他限制,比如在地图上显示它。(如你所说)。

不确定他们是否有类似的要求,但您可以查看Map Quest's Api

答案 2 :(得分:2)

使用iOS7 get full directions within the API

查看本教程Tutorial

答案 3 :(得分:0)

虽然Ken给出了一个很好的答案,但我还会添加将坐标转换为MKMapItem的方式:

+ (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;

}

答案 4 :(得分:-3)

give latitude2,lang of destination   and latitude,longitude of first place.

this function return distance in kilometer.


  -(double)distanceFilter:(double) latitude2 :(double)lang{
        double distance=(((acos(sin((latitude*M_PI/180)) * sin((latitude2*M_PI/180))+cos((latitude*M_PI/180)) * cos((latitude2*M_PI/180)) * cos(((longitude- lang)*M_PI/180))))*180/M_PI)*60*1.1515*1.609344);

        return distance;
    }