gpt4 book ai didi

ios - 如何在 iOS 上的 Google map 中徒手绘制?

转载 作者:可可西里 更新时间:2023-11-01 04:16:09 24 4
gpt4 key购买 nike

在绘图应用程序中,我们可以识别用户何时开始绘图并移动手指以绘制线条/形状。我想在 map 上做同样的事情,我该怎么做?

最佳答案

基本步骤将包括:

1)每当用户开始绘图时添加一个叠加 View 。

lazy var canvasView:CanvasView = {

var overlayView = CanvasView(frame: self.googleMapView.frame)
overlayView.isUserInteractionEnabled = true
overlayView.delegate = self
return overlayView

}()
@IBAction func drawActn(_ sender: AnyObject?) {

self.coordinates.removeAll()
self.view.addSubview(canvasView)
let origImage = UIImage(named: "pen")
let tintedImage = origImage?.withRenderingMode(UIImageRenderingMode.alwaysTemplate)
drawBtn.setImage(tintedImage, for: .normal)
drawBtn.tintColor = UIColor.white
drawBtn.backgroundColor = UIColor.red

}

2)在叠加 View 中自由手绘

class CanvasView: UIImageView {

weak var delegate:NotifyTouchEvents?
var lastPoint = CGPoint.zero
let brushWidth:CGFloat = 3.0
let opacity :CGFloat = 1.0


override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {


if let touch = touches.first {

self.delegate?.touchBegan(touch: touch)
lastPoint = touch.location(in: self)
}
}


override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

if let touch = touches.first {

self.delegate?.touchMoved(touch: touch)
let currentPoint = touch.location(in: self)
drawLineFrom(fromPoint: lastPoint, toPoint: currentPoint)
lastPoint = currentPoint
}
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

if let touch = touches.first {

self.delegate?.touchEnded(touch: touch)

}
}

func drawLineFrom(fromPoint: CGPoint, toPoint: CGPoint) {

UIGraphicsBeginImageContext(self.frame.size)
let context = UIGraphicsGetCurrentContext()
self.image?.draw(in: CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height))

context?.move(to: fromPoint)
context?.addLine(to: toPoint)

context?.setLineCap(.round)
context?.setLineWidth(brushWidth)
context?.setStrokeColor(UIColor.black.cgColor)
context?.setBlendMode(.normal)
context?.strokePath()

self.image = UIGraphicsGetImageFromCurrentImageContext()
self.alpha = opacity
UIGraphicsEndImageContext()

}

}

3)使用委托(delegate)模式从OverlayView获取数组中的所有坐标到Controller

//MARK: GET DRAWABLE COORDINATES
extension ViewController:NotifyTouchEvents{

func touchBegan(touch:UITouch){

let location = touch.location(in: self.googleMapView)
let coordinate = self.googleMapView.projection.coordinate(for: location)
self.coordinates.append(coordinate)

}

func touchMoved(touch:UITouch){

let location = touch.location(in: self.googleMapView)
let coordinate = self.googleMapView.projection.coordinate(for: location)
self.coordinates.append(coordinate)

}

func touchEnded(touch:UITouch){

let location = touch.location(in: self.googleMapView)
let coordinate = self.googleMapView.projection.coordinate(for: location)
self.coordinates.append(coordinate)
createPolygonFromTheDrawablePoints()
}
}

4)将该坐标更改为多边形

   func createPolygonFromTheDrawablePoints(){

let numberOfPoints = self.coordinates.count
//do not draw in mapview a single point
if numberOfPoints > 2 { addPolyGonInMapView(drawableLoc: coordinates) }//neglects a single touch
coordinates = []
self.canvasView.image = nil
self.canvasView.removeFromSuperview()

let origImage = UIImage(named: "pen")
let tintedImage = origImage?.withRenderingMode(UIImageRenderingMode.alwaysTemplate)
drawBtn.setImage(tintedImage, for: .normal)
drawBtn.tintColor = UIColor.red
drawBtn.backgroundColor = UIColor.white

}

func addPolyGonInMapView( drawableLoc:[CLLocationCoordinate2D]){

isDrawingModeEnabled = true
let path = GMSMutablePath()
for loc in drawableLoc{

path.add(loc)

}
let newpolygon = GMSPolygon(path: path)
newpolygon.strokeWidth = 3
newpolygon.strokeColor = UIColor.black
newpolygon.fillColor = UIColor.black.withAlphaComponent(0.5)
newpolygon.map = googleMapView
if cancelDrawingBtn.isHidden == true{ cancelDrawingBtn.isHidden = false }
userDrawablePolygons.append(newpolygon)
addPolygonDeleteAnnotation(endCoordinate: drawableLoc.last!,polygon: newpolygon)
}

我创建了一个演示项目,用于在 Swift 3 中的 Google map 上绘制/删除多个多边形 here .

请记住在 AppDelegate 中设置 API key 并更改包标识符以运行项目。

关于ios - 如何在 iOS 上的 Google map 中徒手绘制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17132266/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com