Comments (9)
Also how to add an api key when using webpack? Thank you!
from echarts-extension-gmap.
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.
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.
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.
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.
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.
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.
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.
That's great. Glad to see your problem solved.
from echarts-extension-gmap.
Related Issues (12)
- Lines problems HOT 13
- if i import or require this package how to set gmap key HOT 1
- Size gmap HOT 8
- Is it possible to combine GeoJSON with this extension ?
- The echart/zrender canvas is not resized when Google Map goes fullscreen. HOT 3
- Echarts not reflow
- can not support graph? HOT 2
- TypeError: echarts.init is not a function HOT 3
- brush selection HOT 1
- Angular 2+ support? HOT 1
- Is it workds with dataset? Could you show an example? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from echarts-extension-gmap.