高德覆蓋物丁鹉,優(yōu)化移動請求妒潭,獲取當(dāng)前view的經(jīng)緯度,創(chuàng)建覆蓋物揣钦,判斷地圖移動結(jié)束后中心是否在覆蓋物內(nèi)雳灾,若不在發(fā)起請求,移除覆蓋物冯凹,重新創(chuàng)建谎亩,(不符合實(shí)際使用)//這種方法會被放大縮小所影響,因?yàn)楦采w物的大小是不固定的
//聲明
var YhyMAPolygon :MAPolygon!
//調(diào)用
//MARK:筆記 地圖結(jié)束移動
func mapView(_ mapView: MAMapView!, mapDidMoveByUser wasUserAction: Bool) {
if YhyMAPolygon != nil {
panDuanQingQiuFanWei()
}else{
chuangjianQingIuWeiLan()
}
}
//創(chuàng)建
//創(chuàng)建請求范圍
func chuangjianQingIuWeiLan() -> () {
//設(shè)置想要獲取的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來區(qū)別不同覆蓋物(用于自定義不同屬性的多變形的顏色)
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()
}
}
//發(fā)起請求
func faQiQingQiu() -> () {
print("移除范圍匈庭,發(fā)起請求")
}
//覆蓋物顏色
//MARK:地圖上覆蓋物的渲染夫凸,可以設(shè)置路徑線路和覆蓋物的寬度,顏色等
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;
}
}