【问题标题】:How to focus on the marker position with zoom in react using react-google-maps package如何使用 react-google-maps 包通过放大反应来关注标记位置
【发布时间】:2019-05-10 14:44:07
【问题描述】:

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

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

我正在使用 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>
        )
    }
}

【问题讨论】:

    标签: reactjs google-maps react-google-maps


    【解决方案1】:

    通过将 defaultZoom 更改为 zoomdefaultCenter 更改为 center 来修复它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-13
      • 2018-12-20
      • 2021-09-27
      • 1970-01-01
      • 2014-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多