Fix center maps not working properly
This commit is contained in:
@@ -6,8 +6,8 @@
|
|||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { Loader } from "../utils/load-google-maps";
|
import { Loader } from "../utils/load-google-maps";
|
||||||
import { ref, provide, inject } from "vue";
|
import { fitMapToMarkers } from "../utils/center-markers";
|
||||||
import {fitMapToMarkers} from "../utils/center-markers";
|
import { ref, provide, watch, inject } from "vue";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
props: {
|
props: {
|
||||||
@@ -21,22 +21,22 @@ export default {
|
|||||||
mapTypeId: {
|
mapTypeId: {
|
||||||
default: "terrain"
|
default: "terrain"
|
||||||
},
|
},
|
||||||
style: {
|
mapStyles: {
|
||||||
default: []
|
type: Array,
|
||||||
|
required: true
|
||||||
},
|
},
|
||||||
disableDefaultUI: {
|
centerGeoCoordinates: {
|
||||||
type: Boolean,
|
type: Array,
|
||||||
},
|
required: false
|
||||||
geoCoordinates: {
|
|
||||||
default: [],
|
|
||||||
},
|
},
|
||||||
options: {
|
options: {
|
||||||
zoomControl: true,
|
zoomControl: true,
|
||||||
mapTypeControl: true,
|
mapTypeControl: false,
|
||||||
scaleControl: true,
|
scaleControl: false,
|
||||||
streetViewControl: true,
|
streetViewControl: false,
|
||||||
rotateControl: true,
|
rotateControl: false,
|
||||||
fullscreenControl: true,
|
fullscreenControl: true,
|
||||||
|
disableDefaultUI: false
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
setup: function(props) {
|
setup: function(props) {
|
||||||
@@ -46,9 +46,14 @@ export default {
|
|||||||
"apiKey"
|
"apiKey"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const mapIds = inject(
|
||||||
|
"mapIds"
|
||||||
|
);
|
||||||
|
|
||||||
const mapsLoader = new Loader({
|
const mapsLoader = new Loader({
|
||||||
apiKey: apiKey,
|
apiKey: apiKey,
|
||||||
version: "weekly",
|
version: "weekly",
|
||||||
|
mapIds: mapIds,
|
||||||
libraries: [
|
libraries: [
|
||||||
"places",
|
"places",
|
||||||
"drawing",
|
"drawing",
|
||||||
@@ -58,35 +63,36 @@ export default {
|
|||||||
]
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
let uiOptions = {}
|
const disableDefaultUi = props?.options?.disableDefaultUI ? true : false
|
||||||
|
|
||||||
if(props.disableDefaultUI) {
|
|
||||||
uiOptions = {
|
|
||||||
disableDefaultUI: true
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
uiOptions = props.options
|
|
||||||
}
|
|
||||||
|
|
||||||
const additionalOptions = {...props.options};
|
|
||||||
|
|
||||||
const mapPromise = mapsLoader.load().then(() => {
|
const mapPromise = mapsLoader.load().then(() => {
|
||||||
const map = new google.maps.Map(mapContainer.value, {
|
const map = new google.maps.Map(mapContainer.value, {
|
||||||
zoom: props.zoom,
|
zoom: props.zoom,
|
||||||
style: props.style,
|
mapId: "889b7f2cfa338388",
|
||||||
center: new google.maps.LatLng(38.423733, 27.142826),
|
center: new google.maps.LatLng(51.2228924, 6.788524),
|
||||||
mapTypeId: props.mapTypeId,
|
disableDefaultUI: disableDefaultUi,
|
||||||
...uiOptions
|
gestureHandling: "auto",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (props.centerGeoCoordinates && props.centerGeoCoordinates.length) {
|
||||||
|
fitMapToMarkers(props.centerGeoCoordinates, map);
|
||||||
|
}
|
||||||
|
|
||||||
return map;
|
return map;
|
||||||
});
|
});
|
||||||
|
|
||||||
provide("mapPromise", mapPromise);
|
provide("mapPromise", mapPromise);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
mapContainer
|
mapContainer,
|
||||||
|
mapPromise
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
#map {
|
||||||
|
height: 80vh;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|||||||
@@ -1,30 +1,20 @@
|
|||||||
<template>
|
<template>
|
||||||
<div>
|
<div>marker</div>
|
||||||
<slot>marker</slot>
|
|
||||||
<div ref="commentContainerRef" v-if="defaultSlot" :is="defaultSlot">
|
|
||||||
<component :is="defaultSlot"></component>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { inject, ref } from "vue";
|
import { inject } from "vue";
|
||||||
import {fitMapToMarkers} from "@fawmi/vue-google-maps/utils/center-markers";
|
|
||||||
|
|
||||||
const commentContainerRef = ref(null);
|
|
||||||
export default {
|
export default {
|
||||||
props: {
|
props: {
|
||||||
geoCoordinates: {
|
location: {
|
||||||
required: true,
|
required: true
|
||||||
default: []
|
|
||||||
},
|
},
|
||||||
centerAutomatically: {
|
icon: {
|
||||||
required: false,
|
required: false
|
||||||
default: false
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
setup(props, { slots }) {
|
setup(props) {
|
||||||
const mapPromise = inject(
|
const mapPromise = inject(
|
||||||
"mapPromise"
|
"mapPromise"
|
||||||
);
|
);
|
||||||
@@ -32,33 +22,31 @@ export default {
|
|||||||
if (mapPromise) {
|
if (mapPromise) {
|
||||||
mapPromise.then((googleMap) => {
|
mapPromise.then((googleMap) => {
|
||||||
const infoWindow = new google.maps.InfoWindow();
|
const infoWindow = new google.maps.InfoWindow();
|
||||||
props.geoCoordinates.forEach( geoCoordinate => {
|
|
||||||
let marker = new google.maps.Marker({
|
|
||||||
position: new google.maps.LatLng(
|
|
||||||
geoCoordinate.lat,
|
|
||||||
geoCoordinate.lng
|
|
||||||
),
|
|
||||||
map: googleMap
|
|
||||||
});
|
|
||||||
|
|
||||||
marker.addListener("click", (event) => {
|
const options = {
|
||||||
if (slots.default) {
|
position: new google.maps.LatLng(
|
||||||
infoWindow.setContent(commentContainerRef.value.innerHTML);
|
props.location.lat,
|
||||||
infoWindow.open(googleMap, marker);
|
props.location.lng
|
||||||
}
|
),
|
||||||
});
|
map: googleMap
|
||||||
|
};
|
||||||
|
|
||||||
|
if (props.icon) {
|
||||||
|
options.icon = props.icon
|
||||||
|
}
|
||||||
|
|
||||||
|
const marker = new google.maps.Marker({
|
||||||
|
...options,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (props.centerAutomatically) {
|
marker.addListener("click", (event) => {
|
||||||
fitMapToMarkers(props.geoCoordinates, googleMap)
|
infoWindow.setContent(`hallo`);
|
||||||
}
|
infoWindow.open(googleMap, marker);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {};
|
||||||
defaultSlot: slots.default,
|
|
||||||
commentContainerRef
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
36
docs/src/components/map.md
Executable file → Normal file
36
docs/src/components/map.md
Executable file → Normal file
@@ -38,4 +38,40 @@ You can also disable specific UI components
|
|||||||
fullscreenControl: true,
|
fullscreenControl: true,
|
||||||
}"
|
}"
|
||||||
/>
|
/>
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
## Access google maps instance
|
||||||
|
You can easily access Map instance by injecting it in your component.
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
const loadMap = inject(
|
||||||
|
"mapPromise"
|
||||||
|
);
|
||||||
|
|
||||||
|
loadMap.then(map=> {
|
||||||
|
console.log( {map})
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
## Add custom button
|
||||||
|
You can use the map instance to add custom buttons to your map
|
||||||
|
```js
|
||||||
|
export function addMyButton(map) {
|
||||||
|
const controlDiv = document.createElement("div");
|
||||||
|
const controlUI = document.createElement("button");
|
||||||
|
controlUI.title = "Click to zoom the map";
|
||||||
|
controlDiv.appendChild(controlUI);
|
||||||
|
const controlText = document.createElement("div");
|
||||||
|
controlText.innerHTML = `Center`;
|
||||||
|
controlUI.appendChild(controlText);
|
||||||
|
controlUI.style.position = "relative"
|
||||||
|
controlUI.style.bottom = "80px"
|
||||||
|
controlUI.addEventListener("click", () => {
|
||||||
|
map.setZoom(map.getZoom() + 1);
|
||||||
|
});
|
||||||
|
|
||||||
|
map.controls[google.maps.ControlPosition.RIGHT_BOTTOM].push(controlDiv); // eslint-disable-line no-undef
|
||||||
|
}
|
||||||
```
|
```
|
||||||
5
docs/src/components/marker.md
Executable file → Normal file
5
docs/src/components/marker.md
Executable file → Normal file
@@ -19,7 +19,8 @@ With marker you can show specific locations on the map
|
|||||||
## Center markers automatically
|
## Center markers automatically
|
||||||
To center markers so that all the markers are visible, use:
|
To center markers so that all the markers are visible, use:
|
||||||
```vue
|
```vue
|
||||||
<GoogleMap>
|
<GoogleMap
|
||||||
|
:centerGeoCoordinates="geoCoordinates">
|
||||||
<Marker
|
<Marker
|
||||||
:centerAutomatically="false"
|
:centerAutomatically="false"
|
||||||
:geoCoordinates="[
|
:geoCoordinates="[
|
||||||
@@ -30,4 +31,4 @@ To center markers so that all the markers are visible, use:
|
|||||||
]"
|
]"
|
||||||
/>
|
/>
|
||||||
</GoogleMap>
|
</GoogleMap>
|
||||||
```
|
```
|
||||||
7
docs/src/docs/getting-started.md
Executable file → Normal file
7
docs/src/docs/getting-started.md
Executable file → Normal file
@@ -29,3 +29,10 @@ app.use(googleMap, googleMapOption)
|
|||||||
app.mount('#app')
|
app.mount('#app')
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Options
|
||||||
|
apiKey is required option to load maps, you may provide following additional options if you want.
|
||||||
|
|
||||||
|
###`mapIds`
|
||||||
|
Allows you to style the map using google cloud map styling
|
||||||
|
|
||||||
|
|||||||
1
index.js
1
index.js
@@ -12,5 +12,6 @@ export default {
|
|||||||
app.component('Polygon', Polygon)
|
app.component('Polygon', Polygon)
|
||||||
app.component('Rectangle', Rectangle)
|
app.component('Rectangle', Rectangle)
|
||||||
app.provide('apiKey', options.apiKey)
|
app.provide('apiKey', options.apiKey)
|
||||||
|
app.provide('mapIds', options.mapIds)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6,7 +6,8 @@ export function fitMapToMarkers(geoCoordinates, mapInstance) {
|
|||||||
mapInstance.setZoom(16);
|
mapInstance.setZoom(16);
|
||||||
} else if (geoCoordinates.length > 0) {
|
} else if (geoCoordinates.length > 0) {
|
||||||
geoCoordinates.forEach(geoCoordinate => {
|
geoCoordinates.forEach(geoCoordinate => {
|
||||||
bounds.extend({lat: geoCoordinate.position.lat, lng: geoCoordinate.position.lng});
|
if (geoCoordinate.lat && geoCoordinate.lng)
|
||||||
|
bounds.extend({lat: geoCoordinate.lat, lng: geoCoordinate.lng});
|
||||||
});
|
});
|
||||||
mapInstance.fitBounds(bounds);
|
mapInstance.fitBounds(bounds);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user