我试图沿路线移动注释,如图
所示我试图通过使用MKGeodesicPolyline
找到源和目标之间的坐标来移动注释。但在这种情况下,注释以直线移动,如图2所示。
此动作的代码如下: -
-(void)toGetRoute:(CLPlacemark*)destinationCoord
{
MKDirectionsRequest *directionRequest=[MKDirectionsRequest new];
MKMapItem *source=[MKMapItem mapItemForCurrentLocation];
[directionRequest setSource:source];
CLLocationCoordinate2D destinationCoordinates= CLLocationCoordinate2DMake(destinationCoord.location.coordinate.latitude, destinationCoord.location.coordinate.longitude);
CLLocation *LAX = [[CLLocation alloc] initWithLatitude:37.332331
longitude:-122.031219];
CLLocation *JFK = [[CLLocation alloc] initWithLatitude:37.365080
longitude:-121.984600];
CLLocationCoordinate2D coordinates[2] =
{LAX.coordinate, JFK.coordinate};
// MKMapPoint points=MKMapPointForCoordinate(destinationCoordinates) ;
geodesic=[MKGeodesicPolyline polylineWithCoordinates:coordinates count:2];//polylineWithPoints:&points count:2];
NSLog(@"pointCount %lu", (unsigned long)geodesic.pointCount);
[self.mapView addOverlay:geodesic];
MKPlacemark *destinationPlacemark=[[MKPlacemark alloc]initWithCoordinate:destinationCoordinates addressDictionary:nil];
MKMapItem *destination=[[MKMapItem alloc]initWithPlacemark:destinationPlacemark];
[directionRequest setDestination:destination];
directionRequest.requestsAlternateRoutes=YES; // to show multiple route.
directionRequest.transportType=MKDirectionsTransportTypeAutomobile;
MKDirections *direction=[[MKDirections alloc]initWithRequest:directionRequest];
//calculate the actual route
[direction calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse * _Nullable response, NSError * _Nullable error) {
if (error) {
NSLog(@"There was an error getting your directions");
return;
}
NSLog(@"no of routes %lu",response.routes.count);
// [self showMultipleRoute:response];
currentRoute=[response.routes firstObject];
[self plotRouteOnMap:currentRoute destinationCoordinate:destinationCoord];
for (MKRouteStep *step in currentRoute.steps)
{
NSLog(@"%@", step.instructions);
}
}];
}
这里我们更新注释: -
-(void)updatePlanePosition:(MKAnnotationView *)view
{
//this example updates the position in increments of 50...
planePositionIndex = planePositionIndex + 1;
if (planePositionIndex >= geodesic.pointCount)
{
//plane has reached end, stop moving
return;
}
MKMapPoint nextMapPoint = geodesic.points[planePositionIndex];
//convert MKMapPoint to CLLocationCoordinate2D...
CLLocationCoordinate2D nextCoord = MKCoordinateForMapPoint(nextMapPoint);
NSLog(@"nextCoord %f,%f",nextCoord.latitude,nextCoord.longitude);
//update the plane's coordinate...
[pntanotation setCoordinate:nextCoord];
// [UIView animateWithDuration:10.0f
// animations:^{
//
// [view.annotation setCoordinate:nextCoord];
// // [mapView setCenterCoordinate:CLLocationCoordinate2DMake(28.5, 77.00) animated:YES];
// }completion:^(BOOL finished) {
// NSLog(@"annotation animation completed");
// }];
//schedule the next update...
[self performSelector:@selector(updatePlanePosition:) withObject:nil afterDelay:0.3];
}
但我不想直线移动注释。我希望它应该沿着路线移动,它应该沿着移动的方向旋转。
请帮帮我。