React-JavaScript:如何在Google Map顶部正确放置下拉菜单

时间:2020-01-27 17:36:28

标签: javascript html css node.js reactjs

我一直试图在我的Google地图的特定位置上放置一个dropdown菜单,以使其位于右上方(或者也可以位于左上方)。 以下是我当前获得的信息:

error

以下是预期的结果:

correct

编辑

在尝试margin-top: 100px;之后,combobx将向下移动,但是如果我键入margin-bottom: 500px;,则下拉列表将保留在中心(原点所在的位置)。 相同的是,如果键入margin-left: 500px,则下拉列表将在右侧移动,但是,如果键入margin-right: 500px,则下拉列表将不会移动至原点。 似乎唯一允许的移动是向下和向左移动,而不是向上和向右移动。 在屏幕下方:

anchor

我正在使用的代码如下:

GoogleMap.js

import React from 'react';
import styled from 'styled-components';
import GoogleMapReact from 'google-map-react';
import { Card, CardImg, CardText, CardBody, CardTitle, /*CardSubtitle,*/ Button } from 'reactstrap';
import ShipTracker from '../components/ShipTracker';

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;
    }
`;

var expanded = false;
function showCheckboxes() {
// show checkboxes
}

const BoatMap = () => (
    <div className="google-map">
        <GoogleMapReact
            bootstrapURLKeys={{ key: 'My_KEY' }}
            center={{
                lat: 42.4,
                lng: -71.1
            }}
            zoom={11}
        >
            {/* Insert components here */}
            <form>
                <div class="multiselect">
                    <div class="combo-companies" onClick={showCheckboxes}>
                        <select>
                            <option>Select an option</option>
                        </select>
                        <div class="overSelect" />
                    </div>
                    <div id="checkboxes">
                        <label for="one">
                            <input type="checkbox" id="CD" />Marker-1
                        </label>
                        <label for="two">
                            <input type="checkbox" id="DJ" />Marker-2
                        </label>
                        <label for="three">
                            <input type="checkbox" id="DT" />Marker-3
                        </label>
                    </div>
                </div>
            </form>
        </GoogleMapReact>
    </div>
);

GoogleMap.css

.combo-companies {
    position: absolute;
    height: 34px;
    width: 10%;
    font-size: 16px;
    border: 1px solid #111;
    border-radius: 3px;
    left: 15px;
    top: 15px;
    z-index: 2000;
}

.multiselect {
    width: 200px;
}

.selectBox {
    position: relative;
}

.selectBox select {
    width: 100%;
    font-weight: bold;
}

.overSelect {
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
}

#checkboxes {
    display: none;
    border: 1px #dadada solid;
}

我到目前为止所做的:

1)我使用CSS Grid Layout来确定Google地图应该在哪里。之后,再遍及整个文档,我确定了网格的位置。因此herehere应该很好。

2)我认为主要问题可以通过在阅读诸如this one和{{3}之类的帖子时通过在组件上使用relativeabsolute参数来确定}。 尽管很有用,但我无法找出问题所在。

3)我知道我已经接近解决方案,因为仅剩下的就是渲染而已this,并认为可能是我的解决方案,但事实并非如此。

感谢您指出正确的方向来解决此问题。

1 个答案:

答案 0 :(得分:1)

在GoogleMap.css中尝试

.multiselect{
width: 200px;
position: absolute; /*Also try relative if this doesn't work */
margin: 2vh 50vw auto auto;
}