在Google地图上绘制虚线圆:iOS

3

我一直在努力尝试在Google地图上绘制虚线圆,但没有找到任何有用的东西...

我已经在互联网上搜索了几天,想要找到绘制虚线圆的解决方案,但每次得到的答案都是绘制一个普通的圆形。

以下是我的做法:

map with circle

以上代码为:

import UIKit
import GoogleMaps
import GooglePlaces

class ViewController: UIViewController
{
    @IBOutlet weak var gmsMapView: GMSMapView!

    override func viewDidLoad()
    {
        super.viewDidLoad()

        gmsMapView.isMyLocationEnabled = true
        gmsMapView.settings.myLocationButton = true
        gmsMapView.animate(toZoom: 10.0)
        gmsMapView.animate(toLocation: CLLocationCoordinate2D(latitude: 40.709677, longitude: -74.011088))
        let circleCenter = CLLocationCoordinate2D(latitude: 40.709677, longitude: -74.011088)
        let circle = GMSCircle(position: circleCenter, radius: 5000)
        circle.strokeWidth = 2
        circle.strokeColor = UIColor.blue
        circle.map = gmsMapView
    }

    override func didReceiveMemoryWarning(){
        super.didReceiveMemoryWarning()
    }
}

这是所需的内容:

需要的是:

在此输入图片描述

1个回答

9

使用 GMSCircle 不可实现,必须基于圆绘制折线。

使用 GMSGeometryOffset 可以生成偏移量到您的位置。 360/6 = 60 用于较少的细节和效率。

let path = GMSMutablePath()

for i in 0...60 {
    let offsetLocation = GMSGeometryOffset(location.coordinate, circleRadius, CLLocationDirection(i*6))
    path.add(offsetLocation)
}
let length: [NSNumber] = [10, 10]
let circle = GMSPolyline(path: path)
let styles = [GMSStrokeStyle.solidColor(.clear), GMSStrokeStyle.solidColor(.red)]

circle.spans = GMSStyleSpans(circle.path!, styles, length, GMSLengthKind.rhumb)
circle.strokeWidth = 1.0
circle.map = mapView

太棒了!太厉害了!它完成了任务,非常感谢你。我实际上已经失去了所有的希望。 - Yash Bedi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接