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

sigwle7e  于 2022-11-06  发布在  Swift
关注(0)|答案(2)|浏览(197)


现在我正在使用谷歌Map,我有一个要求,以绘制路线之间的来源和目的地。我正在使用GMSPolyline绘制路线,但步行模式,我想显示虚线作为谷歌Map。如果任何一个知道请帮助我在这一点上。提前感谢
如果是,则返回一个新的值。

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

    })
jv2fixgn

jv2fixgn1#

你可以用它来画点线。

- (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;
}
kg7wmglp

kg7wmglp2#

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

func createDashedLine(thisPoint: CLLocationCoordinate2D, nextPoint: CLLocationCoordinate2D, color: UIColor) {
    let difLat = nextPoint.latitude - thisPoint.latitude
    let difLng = nextPoint.longitude - thisPoint.longitude
    let scale = camera.zoom * 2
    let divLat = difLat / scale
    let divLng = difLng / scale
    let tmpOrig = thisPoint

    var singleLinePath = GMSMutablePath()

    for i in 0 ..< scale {
        var tmpOri = tmpOrig
        if i > 0 {
            tmpOri = CLLocationCoordinate2DMake(tmpOrig.latitude + (divLat * 0.25), tmpOrig.longitude + (divLng * 0.25))
        }

        singleLinePath.addCoordinate(tmpOri)
        singleLinePath.addCoordinate(CLLocationCoordinate2DMake(tmpOrig.latitude + (divLat * 1.0), tmpOrig.longitude + (divLng * 1.0)))
        tmpOri = CLLocationCoordinate2DMake(tmpOrig.latitude + (divLat * 1.0), tmpOrig.longitude + (divLng * 1.0))
    }

    let polyline = GMSPolyline(path: singleLinePath)
    polyline.geodesic = false
    polyline.strokeWidth = 5.0
    polyline.strokeColor = color
    polyline.map = mapView

    //Setup line style and draw

    lengths = [(singleLinePath.lengthOfKind(kGMSLengthGeodesic) / 100)]
    polys = [polyline]
    setupStyle(with: color)
    tick()
}

func tick() {
    for poly in polys {
        poly.spans = GMSStyleSpans(poly.path, styles, lengths, kGMSLengthGeodesic, pos)
    }

    pos -= step
}

func setupStyle(with color: UIColor) {
    let gradColor = GMSStrokeStyle.gradient(from: color, to: color)
    styles = [gradColor, GMSStrokeStyle.solidColor(.white)]
    step = 50000
}

相关问题