高德覆蓋物,優化移動請求,獲取當前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;
}
}