简体   繁体   English

在谷歌地图上平移时如何保持下拉菜单和按钮固定

[英]How to keep dropdowns and buttons fixed while panning on a google-map

I just finished to set a small user interface containing:我刚刚完成设置一个包含以下内容的小用户界面:

1) dropdown menus 1) dropdown菜单

2) 1 button 2) 1 个button

3) 1 progress-bar 3) 1 个progress-bar

As shown in the image below:如下图所示:

地图1

The problem I have is that when I drag the map, for example, to see what is on the right, all the components listed are moving together with the map.我遇到的问题是,例如,当我拖动地图以查看右侧的内容时,列出的所有组件都与地图一起移动。 But I want them to be fixed while moving the cursor.但我希望它们在移动光标时被固定。

As soon as I release the mouse click, the components go back to the original position.我一松开鼠标,组件就会回到原来的位置。

See the problem below during dragging the map:拖拽地图时出现如下问题:

拖

Below the snippet of GoogleMap.js :GoogleMap.js的片段下方:

import React from 'react';
import styled from 'styled-components';
import GoogleMapReact from 'google-map-react';

const MapContainer = styled.div`
    display: grid;
    grid-template-columns: 2fr 1fr;
    grid-template-rows: 1fr 200px;
    grid-gap: 10px;
    height: 100vh;
    grid-template-areas: "google-map   sidebar" "ship-tracker sidebar";

    .google-map {
        background: #424242;
        grid-area: google-map;
        position: relative;
        height: 100%;
        width: 100%;
    }
    .map-sidebar {
        background: #9dc183;
        grid-area: sidebar;
    }
    .ship-tracker {
        grid-area: ship-tracker;
    }
`;

const BoatMap = () => (
    <div className="google-map">
        <GoogleMapReact
            bootstrapURLKeys={{ key: 'My_KEY' }}
            center={{
                lat: 42.4,
                lng: -71.1
            }}
            zoom={11}
        >

            <select className="combo-A">
                <option>All</option>
                <option>Marker-A</option>
                <option>Marker-B</option>
            </select>
            <div class="progress-circle p0">
                <span>0%</span>
                <div class="left-half-clipper">
                    <div class="first50-bar" />
                    <div class="value-bar" />
                </div>
            </div>
            <button className="btn-next-request" onClick="">
                Time to Next API Request
            </button>
        </GoogleMapReact>
    </div>
);

Below the snippet of GoogleMap.css .GoogleMap.css的片段下方。 For this file I am including only one component as the others are structured similarly:对于这个文件,我只包含一个组件,因为其他组件的结构类似:

.combo-A {
    position: fixed;
    height: 34px;
    font-size: 16px;
    border-radius: 3px;
    left: 15px;
    top: 15px;
    margin-right: 500px;
    width: 200px;
    margin: -40vh 50vw auto -50vh;
    background: var(--offWhite);
    padding: 0.1rem 0.9rem;
    border: 3px solid var(--shadowGreen);
    color: var(--primaryGreen);
    font-weight: bold;
}

What I have done so far:到目前为止我做了什么:

1) I consulted the following source and it seems that if a components has to be fixed, the best approach would be to use position: sticky however the result was not the one expected as shown below: 1)我查阅了以下来源,似乎如果必须固定组件,最好的方法是使用position: sticky但结果不是预期的,如下所示:

黏

2) If I change from position: fixed; 2)如果我改变position: fixed; to position: absolute; position: absolute; nothing changes.没有什么改变。

3) If I change from position: fixed; 3)如果我改变position: fixed; to position: relative; position: relative; I get the same result as point 1)我得到与第 1 点相同的结果)

4) The last thing I tried was changing the position of the map on the GoogleMap.js file as fixed as explained below: 4)我试着改变了在地图上的位置的最后一件事GoogleMap.js文件作为fixed ,如下所述:

.google-map {
    background: #424242;
    grid-area: google-map;
    position: fixed;
    height: 100%;
    width: 100%;
}

That also didn't cause any changes.这也没有引起任何变化。

I am not sure at this point what the error could be in order to keep all the components fixed in their position during paning or dragging on the map.我现在不确定是什么错误,以便在地图上平移或拖动期间将所有组件固定在其位置上。

You could create a custom MapControl class like the following, found ongithub您可以创建一个自定义MapControl类,如下所示,在github 上找到

<MapControl map={this.map || null}
    controlPosition={this.maps ? this.maps.ControlPosition.TOP_LEFT : null}
>
    <YourControlButtonComponentHere />
</MapControl>
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { render } from 'react-dom'

class MapControl extends Component {
  static propTypes = {
    controlPosition: PropTypes.number,
  }

  shouldComponentUpdate(nextProps, nextState) {
    return !this.props.map && nextProps.map
  }

  componentDidMount() {
    this._render()
  }

  componentDidUpdate(prevProps, prevState) {
    this._render()
  }

  componentWillUnmount() {
        const { props } = this
    if (!props.map) return
        const index = props.map.controls[props.controlPosition].getArray().indexOf(this.el)
        props.map.controls[props.controlPosition].removeAt(index)
    }

  _render() {
    const { props } = this
    if (!props.map || !props.controlPosition) return
    render(
      <div ref={el => {
        if (!this.renderedOnce) {
          this.el = el
          props.map.controls[props.controlPosition].push(el)
        } else if (el && this.el && el !== this.el) {
          this.el.innerHTML = '';
          [].slice.call(el.childNodes).forEach(child => this.el.appendChild(child))
        }
        this.renderedOnce = true
      }}>
        {props.children}
      </div>,
      document.createElement('div')
    )
  }

  render() {
    return <noscript/>
  }
}

export default MapControl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM