GmapVue

repository·master·Indexed 41 days ago

https://github.com/diegoazh/gmap-vue

Vue-specific wrappers, components, and composables for the Google Maps JavaScript API. A maintained fork of vue2-google-maps designed to support modern Vue 3 features and provide typed entrypoints, while maintaining support for Vue 2.

Tokens
121.3K
Snippets
327
Records
473
Agent score
49%

What's inside GmapVue

  1. Overview of GmapVue packages

    master

    GmapVue provides Vue wrappers for the Google Maps JavaScript API. The project is split into two main versions:

    • @gmap-vue/v3: The actively developed package for Vue 3. It includes the plugin, components, composables, keys, interfaces, and types.
    • gmap-vue: The legacy package for Vue 2. This package is frozen and no longer actively maintained. It is recommended to migrate to @gmap-vue/v3 for new projects.
  2. Overview of GmvVue 3 components

    master

    GmapVue provides a suite of Vue 3 components to wrap Google Maps features. Start with GmvMap as the base, then add specific components based on your requirements:

    • Markers & Clusters: GmvMarker (Advanced Markers), GmvCluster (grouping markers).
    • Information & Search: GmvInfoWindow (contextual content), GmvAutocomplete (Places-powered search).
    • Shapes & Overlays: GmvCircle, GmvPolygon, GmvPolyline, GmvRectangle.
    • Layers: GmvKmlLayer (KML sources), GmvHeatmapLayer (density visualization).
    • Specialized Views: GmvStreetViewPanorama (standalone Street View), GmvDrawingManager (legacy Drawing Library support).
  3. Use the InfoWindow component

    master

    GmvInfoWindow renders a Google Maps InfoWindow on a GmvMap. It can be used to display contextual content either anchored to a marker or at a specific map position.

    When using the @gmap-vue/v3 package, you can import it as InfoWindow from @gmap-vue/v3/components. If you are using the plugin, it is registered globally as GmvInfoWindow.

    import { InfoWindow } from '@gmap-vue/v3/components';
  4. Understand the mapElement factory

    master

    The mapElement factory is an internal utility used to create Vue component options for Google Maps elements. It automates the lifecycle of a Google Maps object within a Vue component, including:

    1. Initialization: It waits for the $mapPromise to resolve before initializing the underlying Google Maps object.
    2. Property Mapping: It maps Vue props to the underlying Google Maps instance using mappedProps.
    3. Event Binding: It binds Google Maps events to Vue component methods via the events configuration.
    4. Lifecycle Hooks: It provides beforeCreate and afterCreate hooks to allow custom logic during the instantiation of the map element.
    5. Cleanup: It automatically calls .setMap(null) on the instance during the Vue destroyed lifecycle hook to prevent memory leaks, provided the instance supports it.

    Note: This is a low-level factory function. For standard usage, refer to the high-level component APIs provided by @gmap-vue/v3 or @gmap-vue/v2.

    // Example of the configuration object structure passed to mapElement
    const options = {
      name: 'myElement',
      ctr: () => MyGoogleMapsClass,
      ctrArgs: (options, props) => [/* args for constructor */],
      mappedProps: { /* mapping logic */ },
      events: { /* event bindings */ },
      props: { /* Vue props */ },
      beforeCreate: (options) => { /* logic before instantiation */ },
      afterCreate: (instance) => { /* logic after instantiation */ }
    };
  5. Understand the mapElement factory (Vue 2 version)

    master

    The mapElement factory is an internal utility used to create Vue components that wrap Google Maps elements. It automates the lifecycle of a Google Maps object within a Vue component, including:

    1. Initialization: It waits for the $mapPromise to resolve, then initializes the Google Maps object using a provided constructor (ctr).
    2. Property Mapping: It maps Vue props and mappedProps to the underlying Google Maps instance.
    3. Event Binding: It binds specified events to the Google Maps instance.
    4. Lifecycle Hooks: It supports beforeCreate (runs before the instance is created, can be asynchronous) and afterCreate (runs after the instance is created).
    5. Cleanup: It automatically calls .setMap(null) on the instance during the destroyed lifecycle hook to prevent memory leaks, provided the object supports it.

    Note: This is a low-level factory function. For specific API usage, refer to the documentation for mapElement utilities.

    // Example of the structure expected by mapElement
    const componentOptions = mapElement({
      name: 'myElement',
      ctr: () => google.maps.Marker,
      ctrArgs: (options) => [options],
      props: { /* Vue props */ },
      mappedProps: { /* Mapping logic */ },
      events: { /* Event listeners */ },
      beforeCreate: (options) => { /* Async setup */ },
      afterCreate: (instance) => { /* Post-creation setup */ }
    });
  6. How InfoWindow opening precedence works

    master

    When the opened prop is truthy, GmvInfoWindow determines its anchor based on the following precedence:

    1. A marker resolved from the markerKey prop.
    2. An explicit marker prop (google.maps.marker.AdvancedMarkerElement).
    3. A map-only open using the position prop.

    When opened is falsey, the component calls infoWindow.close(). Changing position or marker while the window is open will trigger a reopen with the new values.

  7. Handle map state changes without two-way binding

    master

    Since two-way binding is not supported in Vue 2.x, you must use events to react to changes in the map (e.g., zoom_changed).

    While the standard Google Maps API listener does not provide the new value in the event argument, gmap-vue enhances these events by providing the new value directly in the argument for convenience.

    To implement manual two-way binding for a property like zoom, use the property binding (:) and the change event (@) together.

    <!-- Manual two-way binding pattern -->
    <gmap-map :zoom="zoom" @zoom_changed="updateZoom($event)"></gmap-map>
  8. Understand the purpose of mapped-props-by-map-element

    master

    The mapped-props-by-map-element utility defines the mapping between GoogleMapsOptions and Vue component props. It identifies which properties are standard Google Maps options and which are 'extraneous' properties used specifically for the Vue.js API.

    When creating custom Vue components that wrap Google Maps elements, you must ensure that you define properties in your Vue component that match the names and values of the non-extraneous properties defined in this mapping.

  9. Use GmvMap slots to display content

    master

    The GmvMap component provides two distinct slots for placing components inside the map area. Choosing the correct slot depends on whether your component needs to be visible on top of the map or if it is a map-layer component that interacts directly with the Google Maps API.

    1. The Default Slot

    Components placed in the default slot are wrapped in a class that sets display: none;.

    • When to use: Use this for standard map components (like markers or polylines) that interact directly with the Google Maps object. These components are rendered by the Google Maps API itself and do not rely on the HTML visibility of the slot.
    • When to avoid: Do not use this for UI elements like toolboxes, search bars, or custom controls that you want the user to see.

    2. The "visible" Slot

    This slot is designed to display content within the responsive wrapper for the map.

    • When to use: Use this for custom controls, toolboxes, or any UI elements that should be visible on top of the map.
    • When to avoid: It is not required for standard map components whose UI is handled by the Google Maps API.
  10. Understanding autocompleteMappedProps for GoogleMapsOptions

    master

    In the Vue 2 version of gmap-vue, autocompleteMappedProps is a global constant used to define which GoogleMapsOptions should be exposed as props in a Vue component.

    These properties are structured in the format that the Google Maps API expects, but include extraneous properties required for the Vue.js API. When creating custom components that wrap Google Maps elements, you must define props in your Vue component that match the names and values of the non-extraneous properties defined in this list.