[英]How to implement google route API in react.js?
我按照本教程并实现了一个谷歌地图。 我尝试使用Google Route API来获取地图上两个位置之间的方向。 但我不确定我做错了什么,我收到了这个错误
1 | 从“./Component”导入组件; 错误
2 | 从“反应”导入反应;
3 | 从“react-dom”导入 ReactDOM;
4 | import { compose, withProps, lifecycle } from "recompose";
这是我的代码
import Component from "./Component";
import React from "react";
import ReactDOM from "react-dom";
import { compose, withProps, lifecycle } from "recompose";
import {
withScriptjs,
withGoogleMap,
GoogleMap,
} from "react-google-maps";
import { DirectionsRenderer } from "react-google-maps";
const MapWithADirectionsRenderer = compose(
withProps({
googleMapURL:
"https://maps.googleapis.com/maps/api/js?key=AIzaSyAcQjrfAudzl6Ton7GA7D-gVqOINMFE7ns =3.exp&libraries=geometry,drawing,places",
loadingElement: <div style={{ height: `100%` }} />,
containerElement: <div style={{ height: `400px` }} />,
mapElement: <div style={{ height: `100%` }} />
}),
withScriptjs,
withGoogleMap,
lifecycle({
componentDidMount() {
const DirectionsService = new google.maps.DirectionsService();
DirectionsService.route(
{
origin: new google.maps.LatLng(41.85073, -87.65126),
destination: new google.maps.LatLng(41.85258, -87.65141),
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 defaultZoom={8} defaultCenter={{ lat: -34.397, lng: 150.644 }}>
{props.directions && <DirectionsRenderer directions={props.directions} />}
</GoogleMap>
));
ReactDOM.render(
< DirectionsRenderer directions />,
document.getElementById("root")
);
几个小时后发现出了什么问题! 我在 Google API 密钥之后丢失了&v
最初有"https://maps.googleapis.com/maps/api/js?key=AIzaSyAcQjrfAudzl6Ton7GA7D-gVqOINMFE7ns =3.exp&libraries=geometry,drawing,places"
但它应该是这样的:
"https://maps.googleapis.com/maps/api/js?key=AIzaSyAcQjrfAudzl6Ton7GA7D-gVqOINMFE7ns&v=3.exp&libraries=geometry,drawing,places"
我还添加将此行添加到我的文件顶部:/ global google / -> for disable ESLint
最终代码如下:
/*global google*/
import React from "react";
import ReactDOM from "react-dom";
//import { DirectionsRenderer } from "react-google-maps";
import { compose, withProps, lifecycle } from "recompose";
import {
withScriptjs,
withGoogleMap,
GoogleMap,
DirectionsRenderer,
} from "react-google-maps";
//const google=window.google
//import { DirectionsRenderer } from "react-google-maps";
const MapWithADirectionsRenderer = compose(
withProps({
googleMapURL:
"https://maps.googleapis.com/maps/api/js?key=AIzaSyAcQjrfAudzl6Ton7GA7D-gVqOINMFE7ns&v=3.exp&libraries=geometry,drawing,places",
loadingElement: <div style={{ height: `100%` }} />,
containerElement: <div style={{ height: `400px` }} />,
mapElement: <div style={{ height: `100%` }} />
}),
withScriptjs,
withGoogleMap,
lifecycle({
componentDidMount() {
const DirectionsService = new google.maps.DirectionsService();
DirectionsService.route(
{
origin: new google.maps.LatLng(40.712776, -74.005974),
destination: new google.maps.LatLng(34.052235, -118.243683),
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 defaultZoom={8} defaultCenter={{ lat: -34.397, lng: 150.644 }}>
{props.directions && <DirectionsRenderer directions={props.directions} />}
</GoogleMap>
));
ReactDOM.render(
< MapWithADirectionsRenderer directions />,
document.getElementById("root")
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.