Browse Source

Merge branch 'fix_other_shpes' into 'master'

修复线型之外其他图形的显示问题

See merge request BA/amap-shapes-generator!8
haoxinlei 7 years ago
parent
commit
0644124e77
1 changed files with 57 additions and 39 deletions
  1. 57 39
      index.js

+ 57 - 39
index.js

@@ -15,21 +15,44 @@ function getCircleRegions(circle) {
     return [{ lat: d1.lat, lng: d1.lon }, { lat: d2.lat, lng: d2.lon }, { lat: d3.lat, lng: d3.lon }, { lat: d4.lat, lng: d4.lon }]
 }
 
+function getDefaultStyle() {
+    let imageName = 'BA_oval'
+    let lineWidth = Global.amapLineWidth
+    let strokeColor = Global.amapStrokeColor
+    let fillColor = Global.amapFillColor
+
+    return {imageName, lineWidth, strokeColor, fillColor}
+}
+
 function getCircles(airspaceInfos, setStyle, currentAirspaceIndex) {
     let circles = [];
     if (!Array.isArray(airspaceInfos)) {
         return circles;
     }
+
+    let {lineWidth, strokeColor, fillColor} = getDefaultStyle()
+
+    if(circleStyle) {
+        lineWidth = circleStyle.lineWidth
+        strokeColor = circleStyle.strokeColor
+        fillColor = circleStyle.fillColor
+    }
+
     //通过该方法获取样式
     let circleStyle = setStyle('circle');
     for (let i = 0; i < airspaceInfos.length; i++) {
         let tmpCircle = airspaceInfos[i]
-        let airspaceTypeFix;
-        if (tmpCircle.airspaceType)
+        let airspaceTypeFix, radiusFix
+        if (tmpCircle.airspaceType) {
             airspaceTypeFix = 'airspaceType';
-        else
+            radiusFix = 'radius'
+        } else {
             airspaceTypeFix = 'airspace_type';
+            radiusFix = 'radius_of_flying'
+        }
+
         if (tmpCircle[airspaceTypeFix] == Global.airspaceType.circle && currentAirspaceIndex != i) {
+
             let coordinate = {};
             if (tmpCircle.center_point_of_flying) {
                 coordinate.latitude = tmpCircle.center_point_of_flying.lat;
@@ -38,14 +61,9 @@ function getCircles(airspaceInfos, setStyle, currentAirspaceIndex) {
                 coordinate.latitude = latLngDegreesToDecimal(tmpCircle.lat);
                 coordinate.longitude = latLngDegreesToDecimal(tmpCircle.lng);
             }
-            let radius = tmpCircle.radius_of_flying;
-            let circle = {
-                lineWidth: circleStyle.lineWidth ? circleStyle.lineWidth : Global.amapLineWidth,
-                strokeColor: circleStyle.strokeColor ? circleStyle.strokeColor : Global.amapStrokeColor,
-                fillColor: circleStyle.fillColor ? circleStyle.fillColor : Global.amapFillColor,
-                radius,
-                coordinate
-            }
+
+            let radius = tmpCircle[radiusFix];
+            let circle = {lineWidth, strokeColor, fillColor, radius, coordinate}
             circles.push(circle);
         }
     }
@@ -103,7 +121,7 @@ function getAirwayPoints(airway, pointBefore, pointAfter) {
 
     let pointTypeFix, pointsFix
 
-    if (points in airway) {
+    if ('points' in airway) {
         pointTypeFix = 'point_type';
         pointsFix = 'points'
     } else {
@@ -147,9 +165,7 @@ function getLinesRouter(lineProps, lineAndMarkerStyle) {
     let markers = new Array();
     let lines = []
 
-    let imageName = 'BA_oval'
-    let lineWidth =  Global.amapLineWidth
-    let strokeColor = Global.amapStrokeColor
+    let {imageName, lineWidth, strokeColor} = getDefaultStyle()
 
     if (lineAndMarkerStyle) {
         imageName = lineAndMarkerStyle.imageName
@@ -259,25 +275,32 @@ function getPolygon(polygonProps, polygonAndMarkerStyle) {
     let coordinates = new Array();
     let markers = new Array();
 
-    let pointsFix;
-    if (polygonProps.points)
+    let {imageName, lineWidth, strokeColor, fillColor} = getDefaultStyle()
+
+    if (polygonAndMarkerStyle) {
+        imageName = polygonAndMarkerStyle.imageName
+        lineWidth = polygonAndMarkerStyle.lineWidth
+        strokeCOlor = polygonAndMarkerStyle.strokeColor
+        fillColor = polygonAndMarkerStyle.fillColor
+    }
+
+    let pointsFix, dataType;
+    if (polygonProps.points) {
         pointsFix = 'points';
-    else
+        dataType = 2
+    } else {
         pointsFix = 'polygonPoints';
+        dataType = 1 // 驼峰模式
+    }
 
     if (Array.isArray(polygonProps[pointsFix])) {
         for (let obj of polygonProps[pointsFix]) {
-            coordinates.push(pointsFix == 'polygonPoints' ? drawLineConfig(obj.lat, obj.lng) : { latitude: obj.lat, longitude: obj.lng });
-            markers.push(pointsFix == 'polygonPoints' ? addOvalPointConfig(obj.lat, obj.lng, polygonAndMarkerStyle.imageName ? polygonAndMarkerStyle.imageName : 'BA_oval') : { coordinate: { latitude: obj.lat, longitude: obj.lng }, imageName: polygonAndMarkerStyle.imageName ? polygonAndMarkerStyle.imageName : 'BA_oval' });
+            coordinates.push(drawLineConfig(obj.lat, obj.lng, dataType));
+            markers.push(addOvalPointConfig(obj.lat, obj.lng, imageName, dataType));
         }
     }
 
-    let polygon = {
-        lineWidth: polygonAndMarkerStyle.lineWidth ? polygonAndMarkerStyle.lineWidth : Global.amapLineWidth,
-        strokeColor: polygonAndMarkerStyle.strokeColor ? polygonAndMarkerStyle.strokeColor : Global.amapStrokeColor,
-        fillColor: polygonAndMarkerStyle.fillColor ? polygonAndMarkerStyle.fillColor : Global.amapFillColor,
-        coordinates: coordinates
-    };
+    let polygon = {lineWidth, strokeColor, fillColor, coordinates};
 
     return { markers, polygon };
 
@@ -298,6 +321,7 @@ function getPolygonsAndMarkers(airspaceInfos, setStyle, currentAirspaceIndex) {
             airspaceTypeFix = 'airspaceType';
         else
             airspaceTypeFix = 'airspace_type';
+
         if (polygon[airspaceTypeFix] == Global.airspaceType.polygon && currentAirspaceIndex != i) {
             let retObj = getPolygon(polygon, polygonAndMarkerStyle);
             markers.push(...retObj.markers);
@@ -412,21 +436,15 @@ export function getShapesSelector(airspaceInfos, style, currentAirspaceIndex) {
         regionPoints
     }
 }
+
 //获取数组
 export function getShapes(airspaceInfos, style, currentAirspaceIndex) {
-    currentAirspaceIndex = currentAirspaceIndex ? currentAirspaceIndex : () => -1;
-    let circles = getCircles(airspaceInfos, setStyle(style), currentAirspaceIndex);
-    let lineAndMarkers = getLinesAndMarkers(airspaceInfos, setStyle(style), currentAirspaceIndex);
-    let lines = getLines(lineAndMarkers);
-    let polygonAndMarkers = getPolygonsAndMarkers(airspaceInfos, setStyle(style), currentAirspaceIndex);
-    let polygons = getPolygons(polygonAndMarkers);
-    let markers = getMarkers(polygonAndMarkers, lineAndMarkers);
-    let regionPoints = getRegionPoints(circles, lineAndMarkers, polygonAndMarkers);
+    let {markers, polygons, circles, lines, regionPoints} = getShapesSelector((airspaceInfos)=>airspaceInfos, style, currentAirspaceIndex)
     return {
-        markers,
-        circles,
-        lines,
-        polygons,
-        regionPoints
+        markers: markers(airspaceInfos),
+        circles: circles(airspaceInfos),
+        lines: lines(airspaceInfos),
+        polygons: polygons(airspaceInfos),
+        regionPoints: regionPoints(airspaceInfos)
     }
 }