javascript - 使用 Google map 将用户限制为一次只能访问一个多边形

标签 javascript reactjs google-maps react-google-maps

我有一个 React-Google-Maps 组件,它允许用户绘制多边形并通过回调将这些多边形的关联 GeoJSON 传递给父组件。

我想做的就是限制用户一次只能使用一个多边形;这意味着,当用户完成多边形时,所有先前渲染的多边形都将被删除。

我已经看到了一些关于如何使用 vanilla JS、jQuery 和 Angular 2 执行此操作的示例,但在 React 上却没有看到。

我的组件:

import React, { Component } from 'react';
const { compose, withProps } = require('recompose');
const { withScriptjs, withGoogleMap, GoogleMap } = require('react-google-maps');
const {
    DrawingManager
} = require('react-google-maps/lib/components/drawing/DrawingManager');

const editTrack = polygon => {
    let GeoJSON = {
        type: 'Feature',
        geometry: {
            type: 'Polygon',
            coordinates: []
        },
        properties: {}
    };
    for (let point of polygon.getPath().getArray()) {
        GeoJSON.geometry.coordinates.push([point.lng(), point.lat()]);
    }
    return GeoJSON;
};

const PlotMap = compose(
    withProps({
        googleMapURL:
            'https://maps.googleapis.com/maps/api/js?key=mykey&v=3.exp&libraries=geometry,drawing,places',
        loadingElement: <div style={{ height: `100%` }} />,
        containerElement: <div style={{ height: `400px` }} />,
        mapElement: <div style={{ height: `100%` }} />
    }),
    withScriptjs,
    withGoogleMap
)(props => (
    <GoogleMap
        defaultZoom={8}
        defaultCenter={new google.maps.LatLng(32.095, 35.398)}>
        <DrawingManager
            onPolygonComplete={polygon => {
                polygon.setEditable(true);
                props.getGeoJSON(editTrack(polygon));
                google.maps.event.addListener(polygon.getPath(), 'insert_at', function(
                    index,
                    obj
                ) {
                    props.getGeoJSON(editTrack(polygon));
                });
                google.maps.event.addListener(polygon.getPath(), 'set_at', function(
                    index,
                    obj
                ) {
                    props.getGeoJSON(editTrack(polygon));
                });
            }}
            defaultDrawingMode={google.maps.drawing.OverlayType.POLYGON}
            defaultOptions={{
                drawingControl: true,
                drawingControlOptions: {
                    style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
                    position: google.maps.ControlPosition.TOP_CENTER,
                    drawingModes: [google.maps.drawing.OverlayType.POLYGON]
                }
            }}
        />
    </GoogleMap>
));

export default PlotMap;

最佳答案

好的,明白了。

import React, { Component } from 'react';
const { compose, withProps } = require('recompose');
const { withScriptjs, withGoogleMap, GoogleMap } = require('react-google-maps');
const {
    DrawingManager
} = require('react-google-maps/lib/components/drawing/DrawingManager');

const editTrack = polygon => {
    let GeoJSON = {
        type: 'Feature',
        geometry: {
            type: 'Polygon',
            coordinates: [[]]
        },
        properties: {}
    };
    for (let point of polygon.getPath().getArray()) {
        GeoJSON.geometry.coordinates[0].push([point.lng(), point.lat()]);
    }
    GeoJSON.geometry.coordinates[0].push(GeoJSON.geometry.coordinates[0][0]);
    return GeoJSON;
};
 //this is where we will keep our polygon when it is drawn
let latestPolygon;

const PlotMap = compose(
    withProps({
        googleMapURL:
            'https://maps.googleapis.com/maps/api/js?key=mykey&v=3.exp&libraries=geometry,drawing,places',
        loadingElement: <div style={{ height: `100%`, width: `100%` }} />,
        containerElement: <div style={{ height: `400px`, width: `100%` }} />,
        mapElement: <div style={{ height: `100%`, width: `100%` }} />
    }),
    withScriptjs,
    withGoogleMap
)(props => (
    <GoogleMap
        defaultZoom={8}
        defaultCenter={new google.maps.LatLng(32.095, 35.398)}>
        <DrawingManager
            onPolygonComplete={polygon => {
//if we have a polygon on the map, delete it now
                latestPolygon && latestPolygon.setMap(null); 
                polygon.setEditable(true);
                props.getGeoJSON(editTrack(polygon));
                google.maps.event.addListener(polygon.getPath(), 'insert_at', function(
                    index,
                    obj
                ) {
                    props.getGeoJSON(editTrack(polygon));
                });
                google.maps.event.addListener(polygon.getPath(), 'set_at', function(
                    index,
                    obj
                ) {
                    props.getGeoJSON(editTrack(polygon));
                });
//now we set the storage polygon to be the one we just drew
                latestPolygon = polygon;
            }}
            defaultDrawingMode={google.maps.drawing.OverlayType.POLYGON}
            defaultOptions={{
                drawingControl: true,
                drawingControlOptions: {
                    style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
                    position: google.maps.ControlPosition.TOP_CENTER,
                    drawingModes: [google.maps.drawing.OverlayType.POLYGON]
                }
            }}
        />
    </GoogleMap>
));

export default PlotMap;

关于javascript - 使用 Google map 将用户限制为一次只能访问一个多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47200886/

相关文章:

javascript - 在 javascript 中该值变为 NaN

javascript - 使上下文菜单显示在顶部

javascript - jQuery:迭代中的索引切片函数

javascript - 如何在angularjs中将base64编码的数据解码为ascii

java - Google Oauth 登录窗口无法正常工作

reactjs - 无法在 enzyme 测试中设置上下文

javascript - 重构三元运算符

javascript - 使用 Google Maps V3 用非重复 map 填充容器

jquery - 谷歌地图,基于 Zoom 的比例圆

google-maps - ionic 2 map 加载 "Uncaught (in promise): [object PositionError]"