Giter VIP home page Giter VIP logo

Comments (9)

JasonLaux avatar JasonLaux commented on May 29, 2024

Also how to add an api key when using webpack? Thank you!

from echarts-extension-gmap.

plainheart avatar plainheart commented on May 29, 2024

Can this extension implement polygons and polylines on Google Map?

Yes, you can refer to the official example about Baidu Map.

Also how to add an api key when using webpack?

You can add a script to your HTML template or use a google-map-loader in your js code. For example, https://github.com/davidkudera/google-maps-loader.

Or wrap simply a script loader like this.

const mapScriptURL = 'https://maps.googleapis.com/maps/api/js?key=your api key from google map platform'

function isGoogleMapAPILoaded() {
    return typeof google !== 'undefined' && typeof google.maps !== 'undefined'
}

function loadGoogleMapScript() {
    return new Promise((resolve, reject) => {
      // if google map API script has loaded, no need to insert it again
      if (isGoogleMapAPILoaded()) {
        resolve(google)
      } else {
        const scriptNode = document.createElement('script')
        scriptNode.setAttribute('type', 'text/javascript')
        scriptNode.setAttribute('charset', 'utf-8')
        scriptNode.setAttribute('src', mapScriptURL)
        scriptNode.setAttribute('id', 'google-map-api-script')
        scriptNode.onload = () => {
          if (isGoogleMapAPILoaded()) {
            resolve(google)
          } else {
            reject('failed to load google map API script')
          }
        }
        scriptNode.onerror = e => {
          reject(e)
        }
        // check again before inserting script
        if (isGoogleMapAPILoaded()) {
          resolve(google)
        } else {
          document.head.appendChild(scriptNode)
        }
      }
    })
}

Then you can use the loader in your code,

loadGoogleMapScript().then(google => {
   console.log('google map script loaded', google)
   // to create echarts instance and get google map instance
   const chart = echarts.init(document.getElementById('chart-container'));
   chart.setOption(option);
   // get google map instance
   const gmap = chart.getModel().getComponent('gmap').getGoogleMap();
   // to add a marker
   const marker = new google.maps.Marker({ position: gmap.getCenter() });
   marker.setMap(gmap);
})

from echarts-extension-gmap.

JasonLaux avatar JasonLaux commented on May 29, 2024

Thanks! I'm really appreciated. Currently I'm using your extension under vue-echarts environment. I loaded the map correctly but it failed to display scatter chart on the map using your demo code. Also when I add another tooltip component, the map is just crashed which I don't know why.

from echarts-extension-gmap.

plainheart avatar plainheart commented on May 29, 2024

Could I know if you are assigning the ECharts instance object with Vue ref? If so, I'd like to suggest using a normal variable without any wrapper.

let chart;
chart = echarts.init(document.getElementById('chart-container'));

from echarts-extension-gmap.

JasonLaux avatar JasonLaux commented on May 29, 2024

No I didn't use ref. It's just <v-chart :option="this.option"></v-chart>. All components and series can be set in option.

from echarts-extension-gmap.

plainheart avatar plainheart commented on May 29, 2024

I tried the example from vue-echarts, it works.

<template>
  <v-chart class="chart" :option="option" />
</template>

<script>
import { use } from "echarts/core";
import { CanvasRenderer } from "echarts/renderers";
import { ScatterChart } from "echarts/charts";
import {
  TitleComponent,
  TooltipComponent
} from "echarts/components";
// import google map extension
import "echarts-extension-gmap";
import VChart from "vue-echarts";
import { ref, defineComponent, onMounted } from "vue";

use([
  CanvasRenderer,
  ScatterChart,
  TitleComponent,
  TooltipComponent
]);

function isGoogleMapAPILoaded() {
    return typeof google !== 'undefined' && typeof google.maps !== 'undefined'
}

function loadGoogleMapScript() {
    const mapScriptURL = 'https://maps.googleapis.com/maps/api/js?key=you key';
    return new Promise((resolve, reject) => {
      // if google map API script has loaded, no need to insert it again
      if (isGoogleMapAPILoaded()) {
        resolve()
      } else {
        const scriptNode = document.createElement('script')
        scriptNode.setAttribute('type', 'text/javascript')
        scriptNode.setAttribute('charset', 'utf-8')
        scriptNode.setAttribute('src', mapScriptURL)
        scriptNode.setAttribute('id', 'google-map-api-script')
        scriptNode.onload = () => {
          if (isGoogleMapAPILoaded()) {
            resolve()
          } else {
            reject('failed to load google map API script')
          }
        }
        scriptNode.onerror = e => {
          reject(e)
        }
        // check again before inserting script
        if (isGoogleMapAPILoaded()) {
          resolve()
        } else {
          document.head.appendChild(scriptNode)
        }
      }
    })
}

export default defineComponent({
  name: "HelloWorld",
  components: {
    VChart
  },
  setup: () => {
    const option = ref();

    onMounted(async () => {
      // load api
      await loadGoogleMapScript()

      option.value = {
        title: {
          text: "Traffic Sources",
          left: "center"
        },
        tooltip: {
          trigger: "item"
        },
        legend: {
          orient: "vertical",
          left: "left",
          data: ["Direct", "Email", "Ad Networks", "Video Ads", "Search Engines"]
        },
        gmap: {
          center: [108.39, 39.9],
          zoom: 4
        },
        series: [
          {
            name: "Scatter",
            type: "scatter",
            coordinateSystem: "gmap",
            data: [
              [120, 30, 50]
            ],
            encode: {
              value: 2
            }
          }
        ]
      }
    })

    return { option };
  }
});
</script>

<style scoped>
.chart {
  height: 100vh;
}
</style>

<style>
body {
  margin: 0;
}
</style>

from echarts-extension-gmap.

JasonLaux avatar JasonLaux commented on May 29, 2024

Actually I use Vue2 and just tag script in index.html to get permission like this:
<script async src="https://maps.googleapis.com/maps/api/js?key=..."> </script>
Also I think I didn't define the async vue component like you do.
Is that the potential reason?

from echarts-extension-gmap.

JasonLaux avatar JasonLaux commented on May 29, 2024

I think I found where is wrong. Map needs to be specified height, that is:
<v-chart class="chart" :option="option" />
<style scoped> .chart { height: 100vh; } </style>
Otherwise scatter or other overlays won't be displayed.

from echarts-extension-gmap.

plainheart avatar plainheart commented on May 29, 2024

That's great. Glad to see your problem solved.

from echarts-extension-gmap.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.