【发布时间】:2014-11-12 17:41:42
【问题描述】:
我是 ios 开发新手,我正在开发一个需要使用 mkmapview 的 iphone 应用程序。 在此我需要显示两个或多个针点之间的路线。
请帮忙。
【问题讨论】:
标签: ios objective-c storyboard mkmapview
我是 ios 开发新手,我正在开发一个需要使用 mkmapview 的 iphone 应用程序。 在此我需要显示两个或多个针点之间的路线。
请帮忙。
【问题讨论】:
标签: ios objective-c storyboard mkmapview
使用下面的代码在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.");
}
}];
}
【讨论】: