[英]Google places autocomplete inside a react component
I am trying to build a google map component and everything is working fine with the Google Maps API v3 but not the Autocomplete functionality.我正在尝试构建一个谷歌地图组件,并且在谷歌地图 API v3 上一切正常,但没有自动完成功能。
This is the code I am using:这是我正在使用的代码:
The Google Map component谷歌地图组件
import React, {Component} from 'react';
import ReactDOM from 'react-dom';
const Marker = React.createClass({
componentDidMount: function() {
console.log("Marker on mount");
},
render: function() {
return false;
}
});
export default Marker;
const GoogleMap = React.createClass({
componentDidMount: function() {
var mapOptions = {
center: this.createLatLng(this.props.center),
zoom: this.props.zoom || 14
};
var map = new google.maps.Map(ReactDOM.findDOMNode(this), mapOptions);
//Render all the markers (children of this component)
React.Children.map(this.props.children, (child) => {
var markerOptions = {
position: this.createLatLng(child.props.position),
title: child.props.title || "",
animation: google.maps.Animation.DROP,
icon: child.props.icon || null,
map: map,
autocomplete:new google.maps.places.AutocompleteService()
};
var marker = new google.maps.Marker(markerOptions);
if(child.props.info) {
var infowindow = new google.maps.InfoWindow({
content: child.props.info
});
marker.addListener('click', function() {
infowindow.open(map, marker);
});
}
});
var input = this.refs.search;
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
var autocomplete = new google.maps.places.Autocomplete(input);
autocomplete.bindTo('bounds', map);
this.setState({map});
},
createLatLng: function(element) {
return new google.maps.LatLng(element.lat, element.lng);
},
render: function() {
return (
<div className="map">
<input ref="search"/>
</div>
)
}
});
export default GoogleMap;
And this is where I call the component这就是我调用组件的地方
import React, {Component} from 'react';
import GoogleMap from './GoogleMap';
import Marker from './GoogleMap';
import Geosuggest from 'react-geosuggest';
class DoodlesMap extends Component {
state = {
center: null,
marker: null,
directions: null
};
componentWillMount() {
navigator.geolocation.getCurrentPosition((position) => {
this.setState({
center: {
lat: position.coords.latitude,
lng: position.coords.longitude
},
marker: {
position: {
lat: position.coords.latitude,
lng: position.coords.longitude
}
}
});
});
}
renderYouAreHereMarker() {
return (
<Marker
position={this.state.center}
icon="../../img/you-are-here.png"
/>
)
}
render() {
if (!this.state.center) {
return (
<div>Loading...</div>
)
}
return (
<div>
<GoogleMap
center={this.state.center}
zoom={15}
>
{this.renderYouAreHereMarker()}
<Marker
position={{lat: 41.317334, lng: -72.922989}}
icon="../../img/marker.png"
info="Hola"
/>
<Marker
position={{lat: 41.309848, lng: -72.938234}}
icon="../../img/marker.png"
info="Epi"
/>
</GoogleMap>
</div>
);
}
}
export default DoodlesMap;
I do not receive any console error.我没有收到任何控制台错误。 The map is displayed correctly (with the markers as children) the input also, but does not make the autocomplete.
地图也正确显示(标记为子项)也是输入,但不会自动完成。
Thank you in advance!!提前谢谢你!!
I figure it out, and was very simple issue.我弄明白了,这是一个非常简单的问题。
The thing was that I did not "enable" the places API in my google developer console.问题是我没有在我的谷歌开发者控制台中“启用”地方 API。
Once I did this everything worked fine!!一旦我这样做了,一切都很好!!
It took me quite some time to get the google-places-autocomplete feature working nicely with a React Component.我花了很长时间才让 google-places-autocomplete 功能与 React 组件完美配合。 I did however manage to figure it out and wrote a short tutorial on it over here.
然而,我确实设法弄明白了,并在这里写了一个简短的教程。
TL;DR of tutorial: You have to use a library called react-scripts
to render the google-maps-places library after the component has mounted. TL; DR of tutorial:您必须使用名为
react-scripts
库在组件安装后呈现 google-maps-places 库。 Most of the time the reason that the autocomplete doesn't work is because the library did not load properly.大多数情况下,自动完成功能不起作用的原因是库没有正确加载。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.