如何从我当前位置绘制路径到我在mapView中设置的注释引脚?

时间:2016-02-29 10:21:33

标签: ios objective-c iphone mkmapview mkannotation

我创建了一个mapView(对不起,我将我的插座命名为mapKit而不是mapView)。我获取了当前位置,并手动将注释引脚设置为某个位置。现在我需要的是,只要我点击注释引脚(请完全在注释引脚上,而不是calloutAccessoryButton)我需要找到一条路径,可能使用polyPath从我当前的位置到注释引脚。不完全像一个方向,只是一个路径,聚合覆盖。请查看我的代码并帮助我。

v.replace("$${CLICK_URL_ENC}$", "$$$$$${CLICK_URL_ENC}$$$$");

1 个答案:

答案 0 :(得分:0)

单击Annotation pin代理方法将调用

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
    NSLog(@"Latitude: %f", view.annotation.coordinate.latitude);
    NSLog(@"Longitude: %f", view.annotation.coordinate.longitude);
    self.annotationViewCoordinate = view.annotation.coordinate;
    [self GetDirections:view.annotation.coordinate];
} 

用于绘制路径

 - (void)GetDirections:(CLLocationCoordinate2D)locationCoordinate
{
    MKPlacemark *aPlcSource = [[MKPlacemark alloc] initWithCoordinate:self.mapView.userLocation.coordinate addressDictionary:[NSDictionary dictionaryWithObjectsAndKeys:@"",@"", nil]];

    MKPlacemark *aPlcDest = [[MKPlacemark alloc] initWithCoordinate:locationCoordinate addressDictionary:[NSDictionary dictionaryWithObjectsAndKeys:@"",@"", nil]];

    MKMapItem *mpItemSource = [[MKMapItem alloc] initWithPlacemark:aPlcSource];
    [mpItemSource setName:@"Source"];

    MKMapItem *mpItemDest  = [[MKMapItem alloc] initWithPlacemark:aPlcDest];
    [mpItemDest setName:@"Dest"];

    MKDirectionsRequest *aDirectReq = [[MKDirectionsRequest alloc] init];
    [aDirectReq setSource:mpItemSource];
    [aDirectReq setDestination:mpItemDest];
    [aDirectReq setTransportType:MKDirectionsTransportTypeAutomobile];

    MKDirections *aDirections = [[MKDirections alloc] initWithRequest:aDirectReq];
    [aDirections calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
        if (error) {
            NSLog(@"Error :: %@",error);
        }
        else{

            NSArray *aArrRoutes = [response routes];
            NSLog(@"Routes :: %@",aArrRoutes);

            [self.mapView removeOverlays:self.mapView.overlays];

            [aArrRoutes enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                MKRoute *aRoute = obj;

                [self.mapView addOverlay:aRoute.polyline];

                NSLog(@"Route Name : %@",aRoute.name);
                NSLog(@"Total Distance (in Meters) :%f",aRoute.distance);
 NSArray *aArrSteps = [aRoute steps];

                NSLog(@"Total Steps : %lu",(unsigned long)[aArrSteps count]);

                [aArrSteps enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                    NSLog(@"Rout Instruction : %@",[obj instructions]);
                    NSLog(@"Rout Distance : %f",[obj distance]);
                }];

            }];
        }
}];
}

用户位置移动时 你可以调用方法

- (void)GetDirections:(CLLocationCoordinate2D)locationCoordinate

来自方法

 - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
     [self GetDirections:self.annotationViewCoordinate];
}