# vue3-google-map ![Build Status](https://github.com/inocan-group/vue3-google-map/actions/workflows/build.yml/badge.svg) [![License](https://img.shields.io/github/license/inocan-group/vue3-google-map)](https://github.com/inocan-group/vue3-google-map/blob/develop/LICENSE) > Composable components for easy use of Google Maps with Vue 3 `vue3-google-map` offers a set of composable components for easy use of Google Maps in your Vue 3 projects. Note: Please refer to the [documentation site](https://vue3-google-map.netlify.app/) for rendered examples. ## Table of Contents - [Getting Started](#getting-started) - [Installation](#installation) - [Your First Map](#your-first-map) - [Components](#components) - [Marker](#marker) - [Polyline](#polyline) - [Polygon](#polygon) - [Rectangle](#rectangle) - [Circle](#circle) - [Info Window](#info-window) - [Custom Marker](#custom-marker) - [Custom Control](#custom-control) - [Marker Cluster](#marker-cluster) - [Heatmap Layer](#heatmap-layer) - [Advanced Usage](#advanced-usage) - [Contribution](#contribution) - [License](#license) ## Getting Started ### Installation #### NPM ```bash npm install vue3-google-map # OR pnpm add vue3-google-map ``` #### CDN Include the following script tag in your `index.html` (make sure to include it after Vue 3's global build). ```html ``` All the map components are available on the `Vue3GoogleMap` global variable. [Codepen demo](https://codepen.io/husamibrahim/pen/poQXZbR) ### Your First Map To construct a map using `vue3-google-map` you'll need to use the base `GoogleMap` component which receives your [Google Maps API key](https://developers.google.com/maps/documentation/javascript/get-api-key), styles (e.g. setting width and height), and any [MapOptions](https://developers.google.com/maps/documentation/javascript/reference/map#MapOptions) to configure your map ([see this](https://github.com/inocan-group/vue3-google-map/blob/develop/src/components/GoogleMap.vue#L57-L218) for all the supported `MapOptions`). Other map features can be added to your map by passing map subcomponents ([Marker](#marker), [Polyline](#polyline), [Polygon](#polygon), [Rectangle](#rectangle), [Circle](#circle), [InfoWindow](#info-window), [CustomMarker](#custom-marker), [CustomControl](#custom-control), or [MarkerCluster](#marker-cluster)) to the default slot of the `GoogleMap` component. ```vue ``` ## Components This library is intended to be used in a _composable_ fashion and therefore you will find yourself using nested components to build your map rather than just a complicated _inline_ format. The main mapping component is `GoogleMap`, however the following components are available at your disposal: - [Marker](#marker) - [Polyline](#polyline) - [Polygon](#polygon) - [Rectangle](#rectangle) - [Circle](#circle) - [InfoWindow](#info-window) - [CustomMarker](#custom-marker) - [CustomControl](#custom-control) - [MarkerCluster](#marker-cluster) ### Marker Use the `Marker` component to draw markers, drop pins or any custom icons on a map. #### Options You can pass a [MarkerOptions](https://developers.google.com/maps/documentation/javascript/reference/marker#MarkerOptions) object to the `options` prop to configure your marker. ```vue ``` #### Events You can listen for [the following events](https://developers.google.com/maps/documentation/javascript/reference/marker#Marker-Events) on the `Marker` component. ### Polyline Use the `Polyline` component to draw paths and arbitrary shapes on a map. #### Options You can pass a [PolylineOptions](https://developers.google.com/maps/documentation/javascript/reference/polygon#PolylineOptions) object to the `options` prop to configure your polyline. ```vue ``` #### Events You can listen for [the following events](https://developers.google.com/maps/documentation/javascript/reference/polygon#Polyline-Events) on the `Polyline` component. ### Polygon Use the `Polygon` component to draw polgons (arbitrary number of sides) on a map. #### Options You can pass a [PolylgonOptions](https://developers.google.com/maps/documentation/javascript/reference/polygon#PolygonOptions) object to the `options` prop to configure your polyline. ```vue ``` #### Events You can listen for [the following events](https://developers.google.com/maps/documentation/javascript/reference/polygon#Polygon-Events) on the `Polygon` component. ### Rectangle Use the `Rectangle` component to draw simple rectangles on a map. #### Options You can pass a [RectangleOptions](https://developers.google.com/maps/documentation/javascript/reference/polygon#RectangleOptions) object to the `options` prop to configure your rectangle. ```vue ``` #### Events You can listen for [the following events](https://developers.google.com/maps/documentation/javascript/reference/polygon#Rectangle-Events) on the `Rectangle` component. ### Circle Use the `Circle` component to draw circles on a map. #### Options You can pass a [CircleOptions](https://developers.google.com/maps/documentation/javascript/reference/polygon#CircleOptions) object to the `options` prop to configure your circle. ```vue ``` #### Events You can listen for [the following events](https://developers.google.com/maps/documentation/javascript/reference/polygon#Circle-Events) on the `Circle` component. ### Info Window Use the `InfoWindow` component to display content in a popup window above the map, at a given location. #### Options You can pass an [InfoWindowOptions](https://developers.google.com/maps/documentation/javascript/reference#InfoWindowOptions) object to the `options` prop to configure your info window. Note that you can optionally pass your content to the default slot of the `InfoWindow` component. ```vue ``` #### Use with Marker You can nest the `InfoWindow` component inside the `Marker` component to display an info window when the marker is clicked. ```vue ``` #### Open and close the Info Window You can use `v-model` to manage the state of the info window programmatically or to know whether it's open or closed ```vue ``` #### Events You can listen for [the following events](https://developers.google.com/maps/documentation/javascript/reference/info-window#InfoWindow-Events) on the `InfoWindow` component. ### Custom Marker Regular markers can be customized a great deal but if you need to you can use the `CustomMarker` component and provide your own custom markup through it's `default` slot. #### Options | Parameter | Type | Description | | :-------- | :------- | :------------------------- | | `position` | `{ lat: number, lng: number}` | Sets the marker position. | | `anchorPoint` | `'CENTER' \| 'TOP_CENTER' \|'BOTTOM_CENTER' \| 'LEFT_CENTER' \| 'RIGHT_CENTER' \| 'TOP_LEFT' \| 'TOP_RIGHT' \| 'BOTTOM_LEFT' \| 'BOTTOM_RIGHT'` | Sets how the marker is anchored relative to it's `position` point. Default is `CENTER`. | | `offsetX` | `number` | Horizontal offset from the `position` point. | | `offsetY` | `number` | Vertical offset from the `position` point. | | `zIndex` | `number` | `z-index` value of the marker. | ```vue ``` ### Custom Control Use the `CustomControl` component to add custom buttons/controls to your map. #### Usage You can define the markup of your custom control in the `default` slot of the `CustomControl` component. The component itself takes two props: - `position`: Defines the position of your custom control on the map. Its value must be one of the [ControlPosition](https://developers.google.com/maps/documentation/javascript/reference/control#ControlPosition) constants. - `index` (optional): Controls the order of placement for custom controls that occupy the same position. Refer to the [Google Maps documentation](https://developers.google.com/maps/documentation/javascript/controls#CustomControls) on custom controls positioning. ```vue ``` ### Marker Cluster Use the `MarkerCluster` component to display a large number of markers on a map. It will combine markers of close proximity into clusters, and simplify the display of markers on the map. Can be used with the `Marker` or `CustomMarker` components. #### Usage Simply pass your `Marker`/`CustomMarker`(s) in the `default` slot of the `MarkerCluster` component. ```vue ``` #### Options `MarkerCluster` accepts an `options` prop (an object) where you can configure `algorithm`, `onClusterClick`, and `renderer` from the [MarkerClustererOptions](https://googlemaps.github.io/js-markerclusterer/interfaces/MarkerClustererOptions.html) interface. Note that all these options are completely optional but non-reactive. #### Events You can listen for [the following events](https://googlemaps.github.io/js-markerclusterer/enums/MarkerClustererEvents.html) on the `MarkerCluster` component. ### Heatmap Layer Use the `HeatmapLayer` component to depict the intensity of data at geographical points on the map. Make sure to include the `visualization` library in the `libraries` prop of the `GoogleMap` component. #### Options You can pass a [HeatmapLayerOptions](https://developers.google.com/maps/documentation/javascript/reference/visualization#HeatmapLayerOptions) object to the `options` prop to configure your heatmap layer. Note that for convenience you can use [LatLngLiteral](https://developers.google.com/maps/documentation/javascript/reference/coordinates#LatLngLiteral)s if you wish for the locations. ```vue ``` ## Advanced Usage The basic components that `vue3-google-map` provides are fully reactive and will get you pretty far. Should you need to access the Google Maps API, however, the `GoogleMaps` component exposes the following: - `ready`: A boolean indicating when the Google Maps script has been loaded. By this point the map instance has been created, the API is ready for use and event listeners have been set up on the map. - `map`: The [Map](https://developers.google.com/maps/documentation/javascript/reference/map#Map) class instance. - `api`: The [Google Maps API](https://developers.google.com/maps/documentation/javascript/reference). - `mapTilesLoaded`: A boolean indicating when the map tiles have been fully loaded. Some usage patterns: ```vue ``` Example: ```vue ``` In addition, most of the subcomponents expose their instance should you need it: - `Marker` exposes `marker` (a [Marker](https://developers.google.com/maps/documentation/javascript/reference/marker#Marker) class instance). - `Polyline` exposes `polyline` (a [Polyline](https://developers.google.com/maps/documentation/javascript/reference/polygon#Polyline) class instance). - `Polygon` exposes `polygon` (a [Polygon](https://developers.google.com/maps/documentation/javascript/reference/polygon#Polygon) class instance). - `Rectangle` exposes `rectangle` (a [Rectangle](https://developers.google.com/maps/documentation/javascript/reference/polygon#Rectangle) class instance). - `Circle` exposes `circle` (a [Circle](https://developers.google.com/maps/documentation/javascript/reference/polygon#Circle) class instance). - `InfoWindow` exposes `infoWindow` (an [InfoWindow](https://developers.google.com/maps/documentation/javascript/reference/info-window#InfoWindow) class instance). - `MarkerCluster` exposes `markerCluster` (a [MarkerClusterer](https://googlemaps.github.io/js-markerclusterer/classes/MarkerClusterer.html) class instance). ## Contribution All contributions are welcome. Before submitting a PR though it would be nice if you created an issue explaining what you want to acheive and why. ## License [MIT](http://opensource.org/licenses/MIT)