Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document how to center a marker on map #76

Merged
merged 2 commits into from
Jul 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,65 @@ export default {
</script>
```

## Center markers in the middle of the map

If you want to zoom in on the map as far as you can, but still see all markers, reference this code snippet.

```vue
<script setup lang="ts">
import { onMounted, ref, watch } from 'vue';

const markers = ref([
{
lat: 47.8557578,
lng: 12.1133382,
},
{
lat: 47.8570908,
lng: 12.1199985,
},
{
lat: 47.8568879,
lng: 12.1270439,
},
]);

const googleMapRef = ref();

/**
* Centers the map on the markers and zooms to fit the bounds.
*/
async function fitMarkerBounds() {
const googleMapInstance = await googleMapRef.value.$mapPromise;

const bounds = new window.google.maps.LatLngBounds();

markers.value.forEach((marker) => {
bounds.extend(marker);
});

googleMapInstance.fitBounds(bounds);
}

// center markers on first render
onMounted(fitMarkerBounds);

// center markers, if they are adjusted
watch(markers, fitMarkerBounds);
</script>

<template>
<GMapMap ref="googleMapRef">
<GMapMarker
v-for="(marker, index) in markers"
:key="`map-marker-${index}`"
:position="marker"
/>
</GMapMap>
</template>

```

### Cluster

If you have too many markers, it is helpful to cluster markers. You can easily cluster markers by wrapping your markers with `GMapCluster` component.
Expand Down