Polygons #636
Replies: 6 comments 5 replies
-
We don't provide exports for those geometries from the library at the moment. However, we do have an example which does implement components like Polygon, Polyline and Circle using this library. Take a look here:
You can copy & paste the Polygon component to your own code and work with it that way. Hope that helps :) |
Beta Was this translation helpful? Give feedback.
-
How do I get the coordinates of a drawn polygon and update them when the polygon is edited? |
Beta Was this translation helpful? Give feedback.
-
import { forwardRef, useEffect, useState } from "react"; export type DrawingManagerProps = { export const CustomDrawingManager = forwardRef<
const [drawingManager, setDrawingManager] = // Expose the drawingManager to the parent through the ref useEffect(() => {
}, [drawingLibrary, map, createdPolygons]); useEffect(() => {
}, [drawableMode, drawingManager, createdPolygons]); return null; export default CustomDrawingManager; This code provides the coordinates of a polygon whenever the polygonComplete function is triggered, typically after a user completes drawing a polygon on a map interface. Would you like me to refine or expand on this? |
Beta Was this translation helpful? Give feedback.
-
/* eslint-disable complexity */
import {
forwardRef,
useContext,
useEffect,
useImperativeHandle,
useMemo,
useRef
} from 'react';
import {GoogleMapsContext, useMapsLibrary} from '@vis.gl/react-google-maps';
import type {Ref} from 'react';
type PolygonEventProps = {
onClick?: (e: google.maps.MapMouseEvent) => void;
onDrag?: (e: google.maps.MapMouseEvent) => void;
onDragStart?: (e: google.maps.MapMouseEvent) => void;
onDragEnd?: (e: google.maps.MapMouseEvent) => void;
onMouseOver?: (e: google.maps.MapMouseEvent) => void;
onMouseOut?: (e: google.maps.MapMouseEvent) => void;
};
type PolygonCustomProps = {
/**
* this is an encoded string for the path, will be decoded and used as a path
*/
encodedPaths?: string[];
};
export type PolygonProps = google.maps.PolygonOptions &
PolygonEventProps &
PolygonCustomProps;
export type PolygonRef = Ref<google.maps.Polygon | null>;
function usePolygon(props: PolygonProps) {
const {
onClick,
onDrag,
onDragStart,
onDragEnd,
onMouseOver,
onMouseOut,
encodedPaths,
...polygonOptions
} = props;
// This is here to avoid triggering the useEffect below when the callbacks change (which happen if the user didn't memoize them)
const callbacks = useRef<Record<string, (e: unknown) => void>>({});
Object.assign(callbacks.current, {
onClick,
onDrag,
onDragStart,
onDragEnd,
onMouseOver,
onMouseOut
});
const geometryLibrary = useMapsLibrary('geometry');
const polygon = useRef(new google.maps.Polygon()).current;
// update PolygonOptions (note the dependencies aren't properly checked
// here, we just assume that setOptions is smart enough to not waste a
// lot of time updating values that didn't change)
useMemo(() => {
polygon.setOptions(polygonOptions);
}, [polygon, polygonOptions]);
const map = useContext(GoogleMapsContext)?.map;
// update the path with the encodedPath
useMemo(() => {
if (!encodedPaths || !geometryLibrary) return;
const paths = encodedPaths.map(path =>
geometryLibrary.encoding.decodePath(path)
);
polygon.setPaths(paths);
}, [polygon, encodedPaths, geometryLibrary]);
// create polygon instance and add to the map once the map is available
useEffect(() => {
if (!map) {
if (map === undefined)
console.error('<Polygon> has to be inside a Map component.');
return;
}
polygon.setMap(map);
return () => {
polygon.setMap(null);
};
}, [map]);
// attach and re-attach event-handlers when any of the properties change
useEffect(() => {
if (!polygon) return;
// Add event listeners
const gme = google.maps.event;
[
['click', 'onClick'],
['drag', 'onDrag'],
['dragstart', 'onDragStart'],
['dragend', 'onDragEnd'],
['mouseover', 'onMouseOver'],
['mouseout', 'onMouseOut']
].forEach(([eventName, eventCallback]) => {
gme.addListener(polygon, eventName, (e: google.maps.MapMouseEvent) => {
const callback = callbacks.current[eventCallback];
if (callback) callback(e);
});
});
return () => {
gme.clearInstanceListeners(polygon);
};
}, [polygon]);
return polygon;
}
/**
* Component to render a polygon on a map
*/
export const Polygon = forwardRef((props: PolygonProps, ref: PolygonRef) => {
const polygon = usePolygon(props);
useImperativeHandle(ref, () => polygon, []);
return null;
}); Can the created polygon be editable, and can I retrieve the edited coordinates? |
Beta Was this translation helpful? Give feedback.
-
When Polygon object and Polyline will be in this library: |
Beta Was this translation helpful? Give feedback.
-
Given the amount of questions we get about this, we're going to prioritize adding it :) |
Beta Was this translation helpful? Give feedback.
-
There is an AdvancedMarker component, <AdvancedMarker position={{lat: 29.5, lng: -81.2}}> <Pin background={'#0f9d58'} borderColor={'#006425'} glyphColor={'#60d98f'} /> . Similar to this, does a Polygon component exist in @vis.gl/react-google-maps to display and edit polygons on the map, like in the previous Google Maps API?"
Beta Was this translation helpful? Give feedback.
All reactions