【问题标题】:How to draw dotted circle path route for the walking mode using GMSMapView?如何使用 GMSMapView 为步行模式绘制虚线圆路径路线?
【发布时间】:2016-12-06 10:06:32
【问题描述】:

现在我正在使用谷歌地图,我需要在源和目的地之间绘制路线。我正在使用 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

    })

【问题讨论】:

标签: ios google-maps swift2


【解决方案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;
}

【讨论】:

  • 谢谢你的回复,你在swift中有这个吗
  • 不适合我,因为它正在添加虚线折线而不是虚线折线
【解决方案2】:

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
}

【讨论】:

    猜你喜欢
    • 2021-09-14
    • 2012-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-31
    • 2022-01-26
    • 1970-01-01
    相关资源
    最近更新 更多