簡體   English   中英

如何在MKMapView中用彩色線連接2條注釋?

[英]How to connect 2 annotations with a coloured line inside a MKMapView?

如何在Mkmapview內部用彩色線條連接2條注釋?

可以使用google direction 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];

在這里在.h文件中創建創建對象routeView作為UIImageView類,還創建lineColor作為UIColor類,例如bellow

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];

然后在您要畫線時更新。還有routesNSMutableArray ,我們在其中存儲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如何在mapkit上的兩點之間畫線

在我看來,這看起來很復雜。 我在網上找到了另一個解決方案。 我首先必須將疊加層添加到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;}

但是它仍然沒有顯示,我在做什么錯?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM