iOS swift-高德覆盖物,优化移动请求(1)

高德覆盖物,优化移动请求,获取当前view的经纬度,创建覆盖物,判断地图移动结束后中心是否在覆盖物内,若不在发起请求,移除覆盖物,重新创建,(不符合实际使用)//这种方法会被放大缩小所影响,因为覆盖物的大小是不固定的

//声明

var YhyMAPolygon :MAPolygon!

//调用

//MARK:笔记 地图结束移动

func mapView(_ mapView: MAMapView!, mapDidMoveByUser wasUserAction: Bool) {

if YhyMAPolygon != nil {

panDuanQingQiuFanWei()

}else{

chuangjianQingIuWeiLan()

}

}

//创建

//创建请求范围

func chuangjianQingIuWeiLan() -> () {

//设置想要获取的view的指定位置,传入view即可

let weizhiLeft_Top = gdMapView.convert(CGPoint.init(x: CGFloat(0), y: CGFloat(64)), toCoordinateFrom: self.view)

let weizhiLeft_dow = gdMapView.convert(CGPoint.init(x: CGFloat(0), y: CGFloat(ScreenHeight-64)), toCoordinateFrom: self.view)

let weizhiRinght_top = gdMapView.convert(CGPoint.init(x: CGFloat(ScreenWidth), y: CGFloat(64)), toCoordinateFrom: self.view)

let weizhiRinght_dow = gdMapView.convert(CGPoint.init(x: CGFloat(ScreenWidth), y: CGFloat(ScreenHeight-64)), toCoordinateFrom: self.view)

var coords1 = [weizhiLeft_Top,

weizhiRinght_top,

weizhiRinght_dow,

weizhiLeft_dow]

YhyMAPolygon = MAPolygon.init(coordinates: &coords1, count: UInt(coords1.count))

//用titile来区别不同覆盖物(用于自定义不同属性的多变形的颜色)

YhyMAPolygon.title = "请求范围"

gdMapView.add(YhyMAPolygon, level: MAOverlayLevel.aboveLabels)

}

//移除范围

func qingChuQingQiuWeiLan() -> () {

if YhyMAPolygon != nil {

gdMapView.remove(YhyMAPolygon)

chuangjianQingIuWeiLan()

}

}

//判断是否移除范围

func panDuanQingQiuFanWei() -> () {

let tinchePolygon: MAPolygon = YhyMAPolygon

let YHylocationPoint = MAMapPointForCoordinate(gdMapView.centerCoordinate)

if (MAPolygonContainsPoint(YHylocationPoint, tinchePolygon.points, tinchePolygon.pointCount)) {

print("在里面")

} else {

print("在外面")

qingChuQingQiuWeiLan()

faQiQingQiu()

}

}

//发起请求

func faQiQingQiu() -> () {

print("移除范围,发起请求")

}

//覆盖物颜色

//MARK:地图上覆盖物的渲染,可以设置路径线路和覆盖物的宽度,颜色等

func mapView(_ mapView: MAMapView!, rendererFor overlay: MAOverlay!) -> MAOverlayRenderer! {

if (overlay.isKind(of: MAPolygon.self))

{

let renderer = MAPolygonRenderer.init(polygon: overlay as! MAPolygon!)

renderer?.strokeColor = #colorLiteral(red: 0.02943656221, green: 0.5472248197, blue: 0.9345962405, alpha: 1)

renderer?.fillColor = #colorLiteral(red: 0.4650182724, green: 0.8273108602, blue: 0.9848365188, alpha: 1).withAlphaComponent(0.1)

renderer?.lineWidth = 2.0

renderer?.lineDash = true

if overlay.title == "请求范围"{

renderer?.strokeColor = UIColor.red

renderer?.fillColor = UIColor.red.withAlphaComponent(0.1)

renderer?.lineWidth = 4.0

}

return renderer;

}

}

你可能感兴趣的:(iOS swift-高德覆盖物,优化移动请求(1))