0.6.0 • Published 2 years ago

destiny-maps v0.6.0

Weekly downloads
4
License
MIT
Repository
github
Last release
2 years ago

npm bundle size

This is a lightweight Google Maps plugin for Vue.

:warning: This plugin is in development, so please let me know if you find any errors.

Credit

This library is originally forked from x5-maps which is a work done by Keagan Chisnall, you can checkout his tutorial creating a COVID Heatmap on medium and give him a clap.

Installation

# npm
npm install destiny-maps

Deployment

This plugin can be installed like any Vue plugin:

import destinyMaps from 'destiny-maps'
// Option 1: Just your key
Vue.use(destinyMaps, 'YOUR_GOOGLE_KEY')
// Option 2: With libraries
Vue.use(destinyMaps, { key: 'YOUR_GOOGLE_KEY', libraries: ['places'] })

new Vue({
  el: '#app',
  render: (h) => h(App),
})

:warning: This plugin is not transpiled! If you want it compatible with IE, Edge, and Safari, you need to add this to your vue.config.js file:

module.exports = {
  transpileDependencies: ['destiny-maps'],
}

Usage

<template>
  <gmaps-map>
    <gmaps-marker :position="{ lat: -27, lng: 153 }" />
  </gmaps-map>
</template>
import { gmapsMap, gmapsMarker } from 'destiny-maps'

export default {
  components: { gmapsMap, gmapsMarker },
}

Provided Components

Some pre-built components have been provided for general use, or as examples for those who wish to take them further.

Map

Map

Maps can take many options. zoom is defaulted to 12 and center is defaulted to Brisbane (as these options are required).

This component supports the following events:

  • @boundsChanged returns new bounds
  • @centerChanged returns new center
  • @zoomChanged returns new zoom level
  • @click returns event
  • @doubleClick returns event
  • @rightClick returns event
  • @mouseover returns event
  • @mouseout returns event
<template>
  <gmaps-map :options="mapOptions" />
</template>

<script>
  import { gmapsMap } from 'destiny-maps'

  export default {
    components: { gmapsMap },
    data: () => ({
      mapOptions: {
        center: { lat: -27.47, lng: 153.025 },
        zoom: 12,
      },
    }),
  }
</script>

Marker

Marker

Markers are placed within Maps and can take many options. A position option is required within the options prop or as its own prop.

This component supports the following events:

  • @move returns new position { lat, lng }
  • @click returns event
  • @doubleClick returns event
  • @rightClick returns event
  • @positionChanged (depreciated) returns new position
PropsTypeDefaultDescription
options*Object-An object of Google Maps Marker options
iconString-URL of the marker icon to be used
labelString-Marker label
opacityNumber1.0Opacity of the marker
positionObject-An object that has lat and lng properties
titleString-Marker title (shown on hover)
visibleBooleantrueIf marker is visible
zIndexNumber-Override position in DOM

* If you want to change values on the fly, use the named props instead of within the options prop. Changing named props will trigger an update.

<template>
  <gmaps-map>
    <gmaps-marker v-for="(item, i) in items" :key="i" :options="item.options" />
  </gmaps-map>
</template>

<script>
  import { gmapsMap, gmapsMarker } from 'destiny-maps'

  export default {
    components: { gmapsMap, gmapsMarker },
    data: () => ({
      items: [
        { options: { position: { lat: -27.41, lng: 153.01 } } },
        { options: { position: { lat: -27.42, lng: 153.02 } } },
        ...,
        { options: { position: { lat: -27.48, lng: 153.08 } } },
        { options: { position: { lat: -27.49, lng: 153.09 } } },
      ],
    }),
  }
</script>

InfoWindow

InfoWindow

InfoWindows are placed with Maps can take a few options. A position option is required.

They are used to put HTML in and have a close/dismiss button built-in.

This component only supports a @closed event (for when someone closes the window)

<template>
  <gmaps-map :options="mapOptions">
    <gmaps-info-window :options="options">
      <p>Example Text</p>
    </gmaps-info-window>
  </gmaps-map>
</template>

<script>
  import { gmapsMap, gmapsInfoWindow } from 'destiny-maps'

  export default {
    components: { gmapsMap, gmapsInfoWindow },
    data: () => ({
      options: {
        position: { lat: -27.46, lng: 153.02 },
      },
      mapOptions: {
        center: { lat: -27.47, lng: 153.025 },
        zoom: 12,
      },
    }),
  }
</script>

Popup

Popup

A Popup is a custom DOM Element. It is here primarily as an example of what is needed when creating your own map objects, but serves as a cleaner InfoWindow for Vue.

It takes the following props:

  • position (req'd)
  • background (style)
  • height (style)
  • width (style)

All events are registered from the markup/component you place inside it rather than the popup itself.

<template>
  <gmaps-map :options="mapOptions">
    <gmaps-popup :position="position" background="#BBF0FF">
      <span @click="doSomething()">Do Something</span>
    </gmaps-popup>
  </gmaps-map>
</template>

<script>
  import { gmapsMap, gmapsPopup } from 'destiny-maps'

  export default {
    components: { gmapsMap, gmapsPopup },
    data: () => ({
      position: { lat: -27.46, lng: 153.02 },
      mapOptions: {
        center: { lat: -27.47, lng: 153.025 },
        zoom: 12,
      },
    }),
  }
</script>

Heatmap

Heatmap

Heatmaps are placed within Maps and have several props which are derived from Google's Heatmap Options. Some are named differently as they have been enhanced/simplified.

PropsTypeDefaultDescription
itemsArray\requiredAn array of objects that has lat and lng properties
colorsArray\-An array of one or more colors to color heatmap e.g. 'red','#0F0','rgba(0,0,0,0)`
dissipatingBooleantrueSpecifies whether heatmaps dissipate on zoom
opacityNumber0.6Opacity of the heatmap
maxIntensityNumber-Number of points in one spot to reach "maximum heat" color
radiusNumber-The radius of influence for each data point, in pixels
weightPropString-The property of items that should be used as the weight (Numbers > 0)

This component does not have any events.

** Note require to include the "visualization" library as described in Deployment

<template>
  <gmaps-map>
    <gmaps-heatmap :data="items" :opacity="0.8" />
  </gmaps-map>
</template>

<script>
  import { gmapsMap, gmapsHeatmap } from 'destiny-maps'

  export default {
    components: { gmapsMap, gmapsHeatmap },
    data: () => ({
      items: [
        { lat: -27.41, lng: 153.01 },
        { lat: -27.42, lng: 153.02 },
        ...,
        { lat: -27.48, lng: 153.08 },
        { lat: -27.49, lng: 153.09 },
      ],
    }),
  }
</script>

Polylines / Polygons

Polyline Polygon

Polylines/polygons are placed within Maps and have several props which are derived from Google's Polyline Options and Polygon Options.

This component supports the following events:

PropsTypeDefaultDescription
clickableBooleantrueIndicates whether this Polyline handles mouse events
draggableBooleanfalseAllow the shape to be dragged over the map
editableBooleanfalseAllow editing the shape by dragging the control points
fillColorStringblack(Only polygons) The fill color ***
fillOpacityNumber0.3(Only polygons) The fill opacity between 0.0 and 1.0
geodesicBooleanfalseWhen true, lines will follow the curvature of the Earth
iconsArray[](Only polylines) Add icons along your path **
pathArrayrequiredPath points (objects with lat and lng properties)
strokeColorStringblackThe stroke color ***
strokePositionNumber0(Only polygons) The stroke position along the path (0 = CENTER / 1 = INSIDE / 2 = OUTSIDE)
strokeOpacityNumber1.0The stroke opacity between 0.0 and 1.0
strokeWeightNumber-The stroke width in pixels
visibleBooleantrueWhether this polyline is visible on the map
zIndexNumber0The zIndex compared to other polys

** Note this is one of those things you're surprised Google couldn't do right. It doesn't take images like all the rest of the icon properties of other components. Here's their example\ *** All CSS3 colors are supported except for extended named colors

<template>
  <gmaps-map>
    <gmaps-polygon :path="items" :strokeColor="blue" :fillColor="red" />
    <gmaps-polyline :path="items" :strokeColor="blue" />
  </gmaps-map>
</template>

<script>
  import { gmapsMap, gmapsPolyline, gmapsPolygon } from 'destiny-maps'

  export default {
    components: { gmapsMap, gmapsPolyline, gmapsPolygon },
    data: () => ({
      items: [
        { lat: -27.41, lng: 153.01 },
        { lat: -27.42, lng: 153.02 },
        ...,
        { lat: -27.48, lng: 153.08 },
        { lat: -27.49, lng: 153.09 },
      ],
    }),
  }
</script>

Rectangles / Circles

Rectangles/Circles

Rectangles/circles are placed within Maps and have several props which are derived from Google's Rectangle Options and Circle Options.

This component supports the following events:

PropsTypeDefaultDescription
boundsArrayrequired(Only rectangles) Position of your rectangle { east, north, south, west }
centerObjectrequired(Only circles) The center of the Circle (object with lat and lng properties)
radiusNumberrequired(Only circles) The radius in meters on the Earth's surface
clickableBooleantrueIndicates whether this Polyline handles mouse events
draggableBooleanfalseAllow the shape to be dragged over the map
editableBooleanfalseAllow editing the shape by dragging the control points
fillColorStringblackThe fill color ***
fillOpacityNumber0.3The fill opacity between 0.0 and 1.0
strokeColorStringblackThe stroke color ***
strokePositionNumber0The stroke position along the path (0 = CENTER / 1 = INSIDE / 2 = OUTSIDE)
strokeOpacityNumber1.0The stroke opacity between 0.0 and 1.0
strokeWeightNumber-The stroke width in pixels
visibleBooleantrueWhether this polyline is visible on the map
zIndexNumber0The zIndex compared to other polys

*** All CSS3 colors are supported except for extended named colors

<template>
  <gmaps-map>
    <gmaps-rectangle :bounds="bounds" :strokeColor="blue" :fillColor="red" />
    <gmaps-circle :center="center" :radius="radius" :strokeColor="green" :fillColor="yellow" />
  </gmaps-map>
</template>

<script>
  import { gmapsMap, gmapsRectangle, gmapsCircle } from 'destiny-maps'

  export default {
    components: { gmapsMap, gmapsPolyline, gmapsPolygon },
    data: () => ({
      bounds: {
        east: 153.12,
        north: -27.44,
        west: 153.0,
        south: -27.58,
      },
      center: { lat: -27.479, lng: 152.937 },
      radius: 5000,
    }),
  }
</script>

Google Places Library (and $GMaps())

As mentioned above, additional libraries can be used in conjunction with this package, and as an example, this is how you would include the Places Library.

// main.js
Vue.use(destinyMaps, { key: 'YOUR_GOOGLE_KEY', libraries: ['places'] })

:warning: This is an example taken from a project of mine; you may be able to find a more efficient way to do this. It is focused around using the AutocompleteService.

:information_source: $GMaps() is the little promise that returns the Google maps object once the Google Maps code has successfully loaded. This is the little trick with getting it to work with Vue and is what you need to access the maps object references in all of the Google Maps documentation.

<template>
  ...
</template>

<script>
  // I leave these as external variables so they can be used inside
  // my arrow functions without confusing the "this" context.
  let PlacesService
  let PlacesServiceOK

  export default {
    methods: {
      query(input) {
        return new Promise((resolve, reject) => {
          PlacesService.getPlacePredictions({ input }, (results, status) => {
            if (status !== PlacesServiceOK) reject(new Error(status))
            else resolve(results)
          })
        })
      },
    },
    // The `maps` object from Google is only available after the pages
    // has been loaded; which hopefully happens before mounted() but
    // that is not guaranteed. That is why I use the `$GMaps()` promise
    // which returns the `maps` object once the Google code has loaded.
    mounted() {
      this.$GMaps().then((maps) => {
        PlacesServiceOK = maps.places.PlacesServiceStatus.OK
        PlacesService = new maps.places.AutocompleteService()
      })
    },
  }
</script>

Custom map slots

While you shouldn't see these for too long while the map loads (if at all), there are two customisable slots: Loading and Error.

<template>
  <gmaps-map>
    <template v-slot:loading>
      <div>
        <span>This is now loading...</span>
      </div>
    </template>
    <template v-slot:error="{ error }">
      <div>
        <span>You broke it: {{ error }}</span>
      </div>
    </template>
  </gmaps-map>
</template>

Authors

License

This project is licensed under the MIT License - see the LICENSE.md file for details

0.6.0

2 years ago

0.5.0

4 years ago

0.4.4

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.4.3

4 years ago

0.4.2

4 years ago

0.3.23

4 years ago

0.3.22

4 years ago

0.3.21

4 years ago