如何使用GMSMapView绘制步行模式的虚线圆路径路径?

时间:2016-12-06 10:06:32

标签: ios google-maps swift2

enter image description here现在我正在使用谷歌地图,我需要在源和目的地之间绘制路线。我使用GMSPolyline绘制路线,但对于步行模式,我想将虚线显示为谷歌地图。如果有人知道请帮助我。感谢提前

  

dispatch_async(dispatch_get_main_queue(){

        let path = GMSPath(fromEncodedPath: rout)
        var polilin = GMSPolyline(path: path)
        polilin = GMSPolyline(path: path)
        polilin.title = "WALK"
        polilin.strokeWidth = 4.0
        polilin.strokeColor = UIColor.redColor()
        polilin.map = self.mapView
        polilin.tappable = true

    })

2 个答案:

答案 0 :(得分:1)

你可以使用它来绘制点线。

- (void) createDashedLine:(CLLocationCoordinate2D )thisPoint:(CLLocationCoordinate2D )nextPoint (UIColor *)colour
{

    double difLat = nextPoint.latitude - thisPoint.latitude;

    double difLng = nextPoint.longitude - thisPoint.longitude;

    double scale = camera.zoom * 2;

    double divLat = difLat / scale;

    double divLng = difLng / scale;

    CLLocationCoordinate2D tmpOrig= thisPoint;

    GMSMutablePath *singleLinePath = [GMSMutablePath path];

    for(int i = 0 ; i < scale ; i ++)
   {
        CLLocationCoordinate2D tmpOri = tmpOrig;
        if(i > 0)
{
 tmpOri = CLLocationCoordinate2DMake(tmpOrig.latitude + (divLat * 0.25f),
                                                tmpOrig.longitude + (divLng * 0.25f));
 }
        [singleLinePath addCoordinate:tmpOri];

        [singleLinePath addCoordinate:
         CLLocationCoordinate2DMake(tmpOrig.latitude + (divLat * 1.0f),
                                        tmpOrig.longitude + (divLng * 1.0f))];

 tmpOri = CLLocationCoordinate2DMake(tmpOrig.latitude + (divLat * 1.0f),
                                            tmpOrig.longitude + (divLng * 1.0f));

    }

     GMSPolyline *polyline ;

     polyline = [GMSPolyline polylineWithPath:singleLinePath];

     polyline.geodesic = NO;
     polyline.strokeWidth = 5.f;

     polyline.strokeColor = colour;

     polyline.map = mapView_;

    //Setup line style and draw
    _lengths = @[@([singleLinePath lengthOfKind:kGMSLengthGeodesic] / 100)];
    _polys = @[polyline];
    [self setupStyleWithColour:colour];
    [self tick];
}

- (void)tick 
{
    //Create steps for polyline(dotted polylines)

    for (GMSPolyline *poly in _polys)
 {
        poly.spans =
        GMSStyleSpans(poly.path, _styles, _lengths, kGMSLengthGeodesic, _pos);
    }
    _pos -= _step;
}

-(void)setupStyleWithColour:(UIColor *)color
{

    GMSStrokeStyle *gradColor = [GMSStrokeStyle gradientFromColor:color toColor:color];

    _styles = @[
                gradColor,
                [GMSStrokeStyle solidColor:[UIColor colorWithWhite:0 alpha:0]],
                ];
    _step = 50000;
}

答案 1 :(得分:-1)

rvios's answer 的快速版本(在Swiftify的帮助下):

RoutePrefix