나는 매우 간단한 반응 응용 프로그램을 사용하여 https://github.com/tomchentw/react-google-maps을 사용하고 있지만 현재지도 나 참조하는 방법을 이해하는 데 어려움이 있습니다. google.maps.Map
객체에 액세스하는 방법 맞춤 구성 요소에서반응 Google지도로 google.maps.Map 객체에 액세스하는 방법
나는 repo에서 this을 찾았지만 게시물을 읽은 후에 나는 여전히 약간 혼란 스럽습니다.
DirectionsRenderer 예제를 기반으로 응용 프로그램을 시작하고 있습니다.
내가하고 싶은 것은 시작 지점을 선택하고 Google지도 자동 완성 API를 사용하기위한 맞춤 구성 요소를 추가하는 것입니다.
예, 패키지에 이미 해당 구성 요소가 포함되어 있음을 알고 있지만 은지도에서 위치를 검색하는 것 이상을 수행해야합니다.
내가
const autocomplete = new google.maps.places.Autocomplete(node);
autocomplete.bindTo('bounds', map);
node
같은 것을 할 것입니다 내 요구를 달성하기 위해 내가 자동 완성 기능을 결합하고있어 map
이 google.maps.Map
객체의 인스턴스 인 요소입니다. 지금까지
내 응용 프로그램 :
App.jsx
const App = ({ store }) => (
<Provider store={store}>
<div>
<Sidebar>
<StartingPoint defaultText="Choose starting point…" />
</Sidebar>
<GoogleApiWrapper />
</div>
</Provider>
);
GoogleApiWrapper
const GoogleMapHOC = compose(
withProps({
googleMapURL: 'https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=geometry,drawing,places&key=__GAPI_KEY',
loadingElement: <div style={{ height: '100vw' }} />,
containerElement: <div style={{ height: '100vh' }} />,
mapElement: <div style={{ height: '100%' }} />,
}),
withScriptjs,
withGoogleMap,
lifecycle({
componentDidMount() {
const DirectionsService = new google.maps.DirectionsService();
// make google object available to other components
this.props.onLoad(google);
DirectionsService.route({
origin: new google.maps.LatLng(41.8507300, -87.6512600),
destination: new google.maps.LatLng(41.8525800, -87.6514100),
travelMode: google.maps.TravelMode.DRIVING,
}, (result, status) => {
if (status === google.maps.DirectionsStatus.OK) {
this.setState({
directions: result,
});
} else {
console.error(`error fetching directions ${result}`);
}
});
},
}),
)(props => (
<GoogleMap
ref={props.onMapMounted}
defaultZoom={13}
defaultCenter={new google.maps.LatLng(37.771336, -122.446615)}
>
{props.directions && <DirectionsRenderer directions={props.directions} />}
</GoogleMap>
));
내가 래퍼 I의 외부 google.maps.Map
개체에 액세스 할 수 없습니다 해요 경우 양자 택일로 elemen에 대한 참조에 액세스하고 싶다. t는지도가 포함되어 있으므로 인스턴스를 만들 수 있습니다. new google.maps.Map(ref_to_elem, options);
도움이 될만한 정보가 있으면 감사하겠습니다.