谷歌没有使用反应谷歌地图定义?

时间:2017-08-08 21:19:25

标签: javascript google-maps reactjs

我已经阅读了与此相关的其他问题,并尝试实现其中的答案。

他们建议添加const google = window.google; - 无效

接下来是添加new google.maps - 无效

我将发布以下代码和错误。

错误发生在调用/* global google */ import { default as React, Component } from 'react'; import raf from 'raf'; import canUseDOM from 'can-use-dom'; const google = window.google; import { withGoogleMap, GoogleMap, Circle, InfoWindow, Marker } from 'react-google-maps'; import withScriptjs from 'react-google-maps/lib/async/withScriptjs'; const googleMapURL = 'https://maps.googleapis.com/maps/api/js?v=3.27&libraries=places,geometry&key=AIzaSyA7XEFRxE4Lm28tAh44M_568fCLOP_On3k'; const geolocation = canUseDOM && navigator.geolocation ? navigator.geolocation : { getCurrentPosition(success, failure) { failure("Your browser doesn't support geolocation."); }, }; const GeolocationExampleGoogleMap = withScriptjs( withGoogleMap(props => <GoogleMap defaultZoom={8} center={props.center}> {props.center && <InfoWindow position={props.center}> <div>User's Location</div> </InfoWindow>} {props.center && <Circle center={props.center} radius={props.radius} options={{ fillColor: 'red', fillOpacity: 0.2, strokeColor: 'red', strokeOpacity: 1, strokeWeight: 1, }} />} > {props.markers.map((marker, index) => { const onClick = () => props.onMarkerClick(marker); const onCloseClick = () => props.onCloseClick(marker); return ( <Marker key={index} position={marker.position} title={(index + 1).toString()} onClick={onClick} > {marker.showInfo && <InfoWindow onCloseClick={onCloseClick}> <div> <strong> {marker.content} </strong> <br /> <em>The contents of this InfoWindow are actually ReactElements.</em> </div> </InfoWindow>} </Marker> ); })} </GoogleMap>, ), ); function generateInitialMarkers() { const southWest = new google.maps.LatLng(-31.203405, 125.244141); const northEast = new google.maps.LatLng(-25.363882, 131.044922); const lngSpan = northEast.lng() - southWest.lng(); const latSpan = northEast.lat() - southWest.lat(); const markers = []; for (let i = 0; i < 5; i++) { const position = new google.maps.LatLng( southWest.lat() + latSpan * Math.random(), southWest.lng() + lngSpan * Math.random(), ); markers.push({ position, content: 'This is the secret message'.split(' ')[i], showInfo: false, }); } return markers; } export default class GeolocationExample extends Component { constructor(props) { super(props); super(props); this.state = { center: null, content: null, radius: 6000, markers: generateInitialMarkers(), }; const isUnmounted = false; handleMarkerClick = this.handleMarkerClick.bind(this); handleCloseClick = this.handleCloseClick.bind(this); } handleMarkerClick(targetMarker) { this.setState({ markers: this.state.markers.map((marker) => { if (marker === targetMarker) { return { ...marker, showInfo: true, }; } return marker; }), }); } handleCloseClick(targetMarker) { this.setState({ markers: this.state.markers.map((marker) => { if (marker === targetMarker) { return { ...marker, showInfo: false, }; } return marker; }), }); } componentDidMount() { const tick = () => { if (this.isUnmounted) { return; } this.setState({ radius: Math.max(this.state.radius - 20, 0) }); if (this.state.radius > 200) { raf(tick); } }; geolocation.getCurrentPosition( (position) => { if (this.isUnmounted) { return; } this.setState({ center: { lat: position.coords.latitude, lng: position.coords.longitude, }, content: 'Location found using HTML5.', }); raf(tick); }, (reason) => { if (this.isUnmounted) { return; } this.setState({ center: { lat: 60, lng: 105, }, content: `Error: The Geolocation service failed (${reason}).`, }); }, ); } componentWillUnmount() { this.isUnmounted = true; } render() { return ( <GeolocationExampleGoogleMap googleMapURL={googleMapURL} loadingElement={<div style={{ height: '100%' }}>loading...</div>} containerElement={<div style={{ height: '100%' }} />} mapElement={<div style={{ height: '100%' }} />} center={this.state.center} content={this.state.content} radius={this.state.radius} onMarkerClick={this.handleMarkerClick} onCloseClick={this.handleCloseClick} markers={this.state.markers} /> ); } } 的地方

Map.js:

yarn install

1 个答案:

答案 0 :(得分:0)

您正在加载异步脚本。初始化谷歌常量时,没有谷歌对象。你可以通过对地图组件使用'ref'来解决这个问题。当谷歌对象存在时引发的引用回调。例如:

<GoogleMap defaultZoom={8} center={props.center} ref={()=>{props.onMapLoad}}>...</GoogleMap>
...
constructor(props) {
   ...
   this.onMapLoad = this.onMapLoad.bind(this)
}
...
onMapLoad() {
  /*init markers and all objects with "google" here*/
}
...   
<GeolocationExampleGoogleMap
    onMapLoad={this.onMapLoad}
    ...
    />

当然,不要初始化google const,而只使用google对象,