如何在MKMapView中将2个注释与彩色线连接?

时间:2012-11-07 12:29:24

标签: iphone objective-c ios mkmapview mkannotation

如何在Mkmapview中使用彩色线连接2个注释?

3 个答案:

答案 0 :(得分:1)

谷歌方向api可用于..通过原点和目的地位置

//http://maps.googleapis.com/maps/api/directions/jsonorigin=origin_place&destination=destination_place&waypoints=Charlestown,MA|Lexington,MA&sensor=false

它将提供您已解析并拉出坐标的jSON响应。与

划一条线
self.routeLine = [MKPolyline polylineWithPoints:arrPoints count:routeArray.count];

答案 1 :(得分:0)

这里创建创建对象routeView作为UIImageView类,并将lineColor创建为.h文件中的UIColor类,如下文

UIImageView* routeView;  
UIColor* lineColor; 

viewDidLoad:方法之后编写此代码..

routeView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, mapView.frame.size.width, mapView.frame.size.height)];
routeView.userInteractionEnabled = NO;
lineColor = [UIColor magentaColor];
[mapView addSubview:routeView];

然后在您想要绘制线条时进行更新.. routes NSMutableArrayCLLocation,其中我们存储-(void) updateRouteView { CGContextRef context = CGBitmapContextCreate(nil, routeView.frame.size.width, routeView.frame.size.height, 8, 4 * routeView.frame.size.width, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedLast); CGContextSetStrokeColorWithColor(context, lineColor.CGColor); CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0); CGContextSetLineWidth(context, 3.0); for(int i = 0; i < routes.count; i++) { CLLocation* location1 = [routes objectAtIndex:i]; CGPoint point = [mapView convertCoordinate:location1.coordinate toPointToView:routeView]; if(i == 0) { CGContextMoveToPoint(context, point.x, routeView.frame.size.height - point.y); } else { CGContextAddLineToPoint(context, point.x, routeView.frame.size.height - point.y); } } CGContextStrokePath(context); CGImageRef image = CGBitmapContextCreateImage(context); UIImage* img = [UIImage imageWithCGImage:image]; routeView.image = img; CGContextRelease(context); } (lat-long)

{{1}}

另请参阅此链接iphone-how-to-draw-line-between-two-points-on-mapkit

答案 2 :(得分:0)

这对我来说很复杂。我在网上找到了另一个解我首先要将叠加层添加到我的mapView

CLLocationCoordinate2D coordinateArray[2];
coordinateArray[0] = start_location;
coordinateArray[1] = end_location;
self.routeLine = [MKPolyline polylineWithCoordinates:coordinateArray count:2];
/***********************************************/

[_mapView addOverlay:self.routeLine];

然后我将viewcontroller设置为mkmapviewdelegate并实现委托方法:

-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay{
if(overlay == self.routeLine)
{
    if(nil == self.routeLineView)
    {
        self.routeLineView = [[MKPolylineView alloc] initWithPolyline:self.routeLine];
        self.routeLineView.fillColor = [UIColor redColor];
        self.routeLineView.strokeColor = [UIColor redColor];
        self.routeLineView.lineWidth = 5;
    }
    return self.routeLineView;
}
return nil;}

但它仍然没有出现,我做错了什么?