How to focus on the marker position with zoom in react using react-google-maps package

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-02 09:38:08

问题


How to focus with zoom to the marker position. Once the marker position changes to different location I need to manually zoom in and out and go to the market location.

Literally I need to manually scroll and zoom into the position of the marker which is difficult.

I am using https://github.com/tomchentw/react-google-maps package

Sample output of below code.

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>
        )
    }
}

回答1:


fixed it by changing defaultZoom to zoom and defaultCenter to center



来源:https://stackoverflow.com/questions/53693145/how-to-focus-on-the-marker-position-with-zoom-in-react-using-react-google-maps-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!