<template> </template>
<script>
import $ from "jquery";
export default {
  name: "fviz-mapzoom",
  methods: {
    startup(map) {
      debugger;
      this.map = map;
      this.address = "";
      this.projectNum = "";
      this.projectName = "";
      var self = this;
      //视图变化时响应事件
      this.map.addEventListener(FwebEarth.EventType.ZOOM_CHANGE, function() {
        self.request();
        self.review();
      });
      //页面首次定位
      setTimeout(function() {
        debugger;
        self.request();
      }, 5000);
    },
    request() {
      var self = this;
      var position = this.map.getCenter();
      if (position.lng === 0) {
        position = {
          lng: 105.91954954765768,
          lat: 35.21410101,
          alt: 0
        };
      }
      $.ajax({
        url:
          "http://api.map.baidu.com/geocoder/v2/?ak=wWYw0yCb8ntXmSgTxTx40vKR&callback=renderReverse&location=" +
          position.lat +
          "," +
          position.lng +
          "&output=json&pois=1",
        type: "get",
        dataType: "jsonp",
        jsonp: "callback",
        success: function(data) {
          self.showAreaInfo(data);
        },
        error: function() {}
      });
    },
    showAreaInfo(data) {
      //获取国家，省份，城市与工程
      var self = this;
      var zoom = this.map.camera.positionCartographic.height;
      if (zoom > 3000000) {
        self.address = data.result.addressComponent.country; //国家
        console.log("国家：" + self.address);
      } else if (zoom > 2000000) {
        self.address = data.result.addressComponent.province; //省份
        console.log("省份：" + self.address);
      } else {
        self.address = data.result.addressComponent.city; //城市
        console.log("城市：" + self.address);
      }
      // if (this.projectNum == 1) {
      //   $("#" + this.options.container).html(this.projectName);
      // } else if (this.projectNum == 0) {
      //   $("#" + this.options.container).html(this.address);
      // } else {
      //   $("#" + this.options.container).html(
      //     this.address + " 工程数量" + this.projectNum
      //   );
      // }
    },
    review() {
      var extent = this.map.getRectangle(50, 50);
      if (extent === undefined) return;
      var view = [extent.lt, extent.rt, extent.rb, extent.lb];
      this.getProjectNum(view);
    },
    getProjectNum(view) {
      
      if (this.$parent.objVal.proSource.rows) {
        var dataset = this.$parent.objVal.proSource.rows;
        var result = [];
        for (var i = 0; i < dataset.length; i++) {
          var data = dataset[i];
          var isView = FwebEarth.MathUtils.withinPloygon(view, {
            lng: data.lng,
            lat: data.lat
          });
          if (isView) {
            result.push(data);
          }
        }
        if (result.length == 1) {
          //显示工程名称
          console.log("工程名称："+result[0].name);
          this.projectName = result[0].name;
        } else {
          //显示工程数量
          console.log("工程数量：" + result.length);
        }
        this.projectNum = result.length;
        this.$parent.$emit('gis-project',this.address+","+this.projectName+","+result.length);
      }
    }
  },
  mounted() {}
};
</script>
