reactjs - 如何使用react-google-maps包通过放大 react 来聚焦于标记位置

标签 reactjs google-maps react-google-maps

如何通过缩放来对焦到标记位置。一旦标记位置更改为不同位置,我需要手动放大和缩小并转到市场位置。

从字面上看,我需要手动滚动并放大到标记的位置,这很困难。

我正在使用 https://github.com/tomchentw/react-google-maps

以下代码的示例输出。

Output Gif Image Link

import React from 'react';
import { compose, withStateHandlers } from "recompose";
import { InfoWindow, withGoogleMap, withScriptjs, GoogleMap, Marker } from 'react-google-maps';


const Map = compose(
    withGoogleMap
)
    (props =>
        <GoogleMap
            defaultZoom={8}
            defaultCenter={props.markerPosition}
        >
            <Marker position={props.markerPosition} />

        </GoogleMap>
    )

export default class MapContainer extends React.Component {
    state = {
      list: [
        { lat: 57.340204, lng: 41.069438 },
        { lat: 12.991792, lng: 77.566020 },
        { lat: -29.556185, lng: 22.508060 },
        { lat: 35.010270, lng: -88.409909 },
      ],
      coordinates: { lat: -34.397, lng: 150.644 }
    }

    onBtnClick() {
      const min = 1, max = 4;
      const no = Math.floor(Math.random() * (max - min + 1) ) + min;
      this.setState({
        coordinates: this.state.list[no-1]
      });
    }

    render() {
        return (
            <div style={{ height: '100%' }}>
                <input
                  type="button" value="Change Marker Position"
                  style={{ marginBottom: "20px" }}
                  onClick={(e) => this.onBtnClick()}
                />
                <Map
                    markerPosition={this.state.coordinates}
                    googleMapURL="https://maps.googleapis.com/maps/api/js?key=AIzaSyAvcDy5ZYc2ujCS6TTtI3RYX5QmuoV8Ffw"
                    loadingElement={<div style={{ height: `100%` }} />}
                    containerElement={<div style={{ height: `400px` }} />}
                    mapElement={<div style={{ height: `100%` }} />}
                />
            </div>
        )
    }
}

最佳答案

通过将 defaultZoom 更改为 zoom 并将 defaultCenter 更改为 center 来修复此问题

关于reactjs - 如何使用react-google-maps包通过放大 react 来聚焦于标记位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53693145/

相关文章:

node.js - create-react-app 无法正常工作并且需要很长时间,有什么建议吗?

Rgooglemaps 没有在 map 上绘制数据

html - 如何使我的 HTML 组件与 CSS 对齐?

javascript - 使用 react 创建带有用户位置的谷歌地图

javascript - 在 NextJs 的 getInitialProps() 中设置缓存控制 header

javascript - '$' 未定义 - React 登录表单

css - 如何使 React CSS 导入组件范围?

google-maps - 谷歌地图 map block 内的交互

reactjs - React-google-maps 点击 map 获取坐标

javascript - 我如何将 React 与 Google Places API 结合使用,以在 Google map 上显示地点标记?