我是ios开发的新手,我正在开发一个我需要使用mkmapview的iphone应用程序。 在这里我需要显示两个或多个针点之间的路线。
请帮助。
答案 0 :(得分:0)
使用以下代码在MKMapView
上绘制图钉。在这里,我使用了旧金山城市的两个附近地点。
- (void) setDirectionLocation {
CLLocationCoordinate2D location = CLLocationCoordinate2DMake(37.79520324238053, -122.40283370018005);
MKCoordinateRegion region;
region = MKCoordinateRegionMake(location, MKCoordinateSpanMake(0.02, 0.02));
MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:region];
[self.mapView setRegion:adjustedRegion animated:YES];
CLLocationCoordinate2D toCoordinate = CLLocationCoordinate2DMake(37.802932, -122.401612);
MKPlacemark *fromPlacemark = [[MKPlacemark alloc] initWithCoordinate:location addressDictionary:nil];
MKPlacemark *toPlacemark = [[MKPlacemark alloc] initWithCoordinate:toCoordinate addressDictionary:nil];
MKMapItem *fromItem = [[MKMapItem alloc] initWithPlacemark:fromPlacemark];
MKMapItem *toItem = [[MKMapItem alloc] initWithPlacemark:toPlacemark];
MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
request.source = fromItem;
request.destination = toItem;
request.requestsAlternateRoutes = YES;
MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
[directions calculateDirectionsWithCompletionHandler:
^(MKDirectionsResponse *response, NSError *error) {
if (!error) {
MKRoute *route = response.routes[0];
[self.mapView addOverlay:route.polyline];
// Drop a pin
MKPointAnnotation *startPin = [[MKPointAnnotation alloc] init];
startPin.coordinate = location;
startPin.title = @"Start";
[self.mapView addAnnotation:startPin];
MKPointAnnotation *endPin = [[MKPointAnnotation alloc] init];
endPin.coordinate = toCoordinate;
endPin.title = @"End";
[self.mapView addAnnotation:endPin];
} else {
NSLog(@"direction not found.");
}
}];
}