0

如何用彩色线连接 Mkmapview 内的 2 个注释?

4

3 回答 3

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];
于 2012-11-07T12:35:38.450 回答
0

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

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我们NSMutableArray存储CLLocation的地方(经纬度)

-(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);

}

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

于 2012-11-07T13:03:20.477 回答
0

这对我来说看起来很复杂。我在网上找到了另一个解决方案。我首先必须将叠加层添加到我的 mapView

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

[_mapView addOverlay:self.routeLine];

然后我将我的视图控制器设置为 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;}

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

于 2012-11-07T13:15:04.377 回答