在模拟器和我的设备上都可以出现路径,但在个别手机上,也是ios9系统,不出现路径。
-
(void)drawLine{
CLGeocoder *geocoder = [CLGeocoder new];[geocoder geocodeAddressString:@"天宫庄园" completionHandler:^(NSArray placemarks, NSError error) {
CLPlacemark *placemark1 = [placemarks firstObject]; [geocoder geocodeAddressString:@"梁祝文化公园" completionHandler:^(NSArray *placemarks, NSError *error) { CLPlacemark *placemark2 = [placemarks firstObject]; [self drawPlacemark1:placemark1 placemark2:placemark2]; }];
}];
} -
(void)drawPlacemark1:(CLPlacemark )mark1 placemark2:(CLPlacemark )mark2{
MKPlacemark *mkPlacemark1 = [[MKPlacemark alloc] initWithPlacemark:mark1];
MKMapItem *item1 = [[MKMapItem alloc]initWithPlacemark:mkPlacemark1];MKPlacemark *mkPlacemark2 = [[MKPlacemark alloc] initWithPlacemark:mark2];
MKMapItem *item2 = [[MKMapItem alloc]initWithPlacemark:mkPlacemark2];MKDirectionsRequest *request = [MKDirectionsRequest new];
request.source = item1;
request.destination = item2;MKDirections *direction = [[MKDirections alloc] initWithRequest:request];
[direction calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse response, NSError error) {
for (MKRoute *route in response.routes) { MKPolyline *polyline = route.polyline; [self.mapView addOverlay:polyline]; }
}];
}
pragma mark - 画线
-
(MKOverlayRenderer )mapView:(MKMapView )mapView rendererForOverlay:(id<MKOverlay>)overlay
{
MKPolylineRenderer * polyline = [[MKPolylineRenderer alloc] initWithOverlay:overlay];polyline.strokeColor = [UIColor grayColor];
polyline.lineWidth = 4;
polyline.lineJoin = kCGLineJoinBevel;
return polyline;}
[geocoder geocodeAddressString:@"梁祝文化公园" completionHandler:^(NSArray placemarks, NSError error) {
}];
这里是不是应该在遍历过程中,交换
placemark2
和placemark1
指针。抱歉上面是瞎说的。
我测了你的代码,在我的设备上可以画出线路,从
浙江省宁波市鄞州区天宫庄园
到浙江省宁波市鄞州区梁祝文化公园
,建议您在drawPlacemark1方法中加入log或者断点,检查CLPlacemark是否返回正确,网络状况不佳会导致这种情况,应该作出相应处理,即应该检查NSError是否有值,并作出相应处理。