javascript - 谷歌地图 react

标签 javascript google-maps reactjs

我们尝试在我们的应用程序中嵌入谷歌地图,但没有成功。自动完成字段没有问题,但我们尝试以相同的方式插入 map ,但出了点问题。

我们使用 React 0.12 并为自动完成字段创建一个组件。

var Geocomplete = React.createClass({
   componentDidMount: function() {
       var inputOptions = {componentRestrictions: {country: 'it'}};

       new google.maps.places.Autocomplete(
           document.getElementById('searchTextField'),
           inputOptions);

   },
   buttonClick: function() {
       alert(this.refs.searchField.getDOMNode().value);
   },

   //         <div id="map-canvas"></div>
   render: function() {
       return (
           <div>
               <label htmlFor="searchTextField">
               Please Insert an address:
               </label>
               <br/>
               <input ref='searchField' id="searchTextField" type="text" size="50"/>
               <br/>
               <button onClick={this.buttonClick}>Submit</button>
               <br />
           </div>
           );
   }
});


module.exports = Geocomplete;

最佳答案

试试这段代码:

组件:

    var GoogleMap = React.createClass({
    getDefaultProps: function () {
        return {
            initialZoom: 6,
            mapCenterLat: 53.5333,
            mapCenterLng: -113.4073126
        };
    },
    componentDidMount: function (rootNode) {
        var mapOptions = {
                center: this.mapCenterLatLng(),
                zoom: this.props.initialZoom
            },
            map = new google.maps.Map(this.getDOMNode(), mapOptions);
        var marker = new google.maps.Marker({position: this.mapCenterLatLng(), title: 'Hi', map: map});
        this.setState({map: map});
    },
    mapCenterLatLng: function () {
        var props = this.props;

        return new google.maps.LatLng(props.mapCenterLat, props.mapCenterLng);
    },
    render: function () {

        return (
            <div className='map-gic'></div>
            );
    }
});

module.exports = GoogleMap;

然后在页面中显示它:

   <GoogleMap mlat="55.0000" mlong="-113.0000"/>

CSS:

.map-gic {
  height: 300px;
  width: 100%;
}

这对我有用

关于javascript - 谷歌地图 react ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29532356/

相关文章:

javascript - 在 document.write 中运行函数,其中 document.write 在 window.open 中

javascript - 使用 math.random() 在 jquery 中为 div 设置动画;

jquery - Google Maps InfoWindow,使用 jQuery 撤消子元素的样式

javascript - 有哪些常见的 Redux Toolkit 的 CreateAsyncThunk 用例

reactjs - 如何在React Native应用程序中订阅Supabase的实时数据?

javascript - 向服务器发出 AJAX 请求时出现错误代码 415

javascript - 基于父窗口更改 iframe 中的 css 属性

javascript - 谷歌地图 : How to redirect to other points in the map?

ios - GMSAutocompleteViewController 仅显示 5 个结果

javascript - 不必要的转义字符 :\` no-useless-escape