我正在使用react-google-maps包https://github.com/tomchentw/react-google-maps向Google地图javascript API路线服务https://developers.google.com/maps/documentation/javascript/examples/directions-simple发出请求。我按预期从API获取响应,并可以将其记录到浏览器,但我无法弄清楚如何在地图组件中渲染折线。
这行代码
directionsDisplay.setMap(map);
正在返回错误
“InvalidValueError:setMap:不是地图方向的实例”。
所以我评论了它,因为似乎通过refs将handleMapLoad()
传递给GoogleMap组件来设置地图。关于如何显示折线的一些指导将非常感激。
/*global google*/
import React, { Component } from "react";
import { withGoogleMap, GoogleMap } from "react-google-maps";
const GettingStartedGoogleMap = withGoogleMap(props => (
<GoogleMap
ref={props.onMapLoad}
defaultZoom={5}
defaultCenter={{lat: 41.85, lng: -117.65}}
>
</GoogleMap>
));
export default class GettingStartedExample extends Component {
handleMapLoad = this.handleMapLoad.bind(this);
handleMapLoad(map) {
this.mapComponent = map;
if (map) {
console.log(map.getZoom());
}
const directionsService = new google.maps.DirectionsService();
const directionsDisplay = new google.maps.DirectionsRenderer();
// directionsDisplay.setMap(map);
const makeRequest = function() {
calculateAndDisplayRoute(directionsService, directionsDisplay);
};
function calculateAndDisplayRoute(directionsService, directionsDisplay) {
directionsService.route({
origin: 'San Francisco',
destination: 'Portland',
travelMode: 'DRIVING'
}, function(response, status) {
if (status === 'OK') {
directionsDisplay.setDirections(response);
console.log(response)
} else {
window.alert('Directions request failed due to ' + status);
}
});
}
makeRequest();
}
render() {
return (
<div style={{height: `500px`}}>
<GettingStartedGoogleMap
containerElement={<div style={{ height: `100%` }} />}
mapElement={<div style={{ height: `100%` }} />}
onMapLoad={this.handleMapLoad}
/>
</div>
);
}
}
答案 0 :(得分:1)
您可以使用react-google-maps Polyline
组件来呈现方向。它为我们提供了其他样式支持options
。与使用DirectionRender
组件相比,这是一种更好的方法。您必须按照以下步骤在Google地图上以Polyline
的形式显示路线:
overview_path
;
overview_path
是包含Lat
和Lng
的对象数组。...
DirectionsService.route({
origin: 'San Francisco',
destination: 'Portland',
travelMode: google.maps.TravelMode.DRIVING,
},
(result, status) => {
if (status === google.maps.DirectionsStatus.OK) {
const overViewCoords = result.routes[0].overview_path;
this.setState({
lineCoordinates: overViewCoords,
});
} else {
console.warn(`error fetching directions ${status}`);
}
});
path
组件的<Polyline />
道具传递; ...
<GoogleMap
ref={props.onMapLoad}
defaultZoom={5}
defaultCenter={{lat: 41.85, lng: -117.65}}
>
<Polyline
path={this.state.lineCoordinates}
geodesic={false}
options={{
strokeColor: '#38B44F',
strokeOpacity: 1,
strokeWeight: 7,
}}
/>
</GoogleMap>
PS。首先从Polyline
导入react-google-maps
:
import { Polyline } from 'react-google-maps';
答案 1 :(得分:1)
一种非常简单直接的方法,也可以使用航点在React中呈现路线
import React from 'react';
import logo from './logo.svg';
import './App.css';
import { withScriptjs } from "react-google-maps";
import Map from './components/Map';
function App() {
const MapLoader = withScriptjs(Map);
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
</header>
<MapLoader
googleMapURL="https://maps.googleapis.com/maps/api/js?key=Key"
loadingElement={<div style={{ height: `100%` }} />}
/>
</div>
);
}
export default App;
在您的Map.js文件中
/*global google*/
import React, { Component } from "react";
import {
withGoogleMap,
withScriptjs,
GoogleMap,
DirectionsRenderer
} from "react-google-maps";
class Map extends Component {
state = {
directions: null,
};
componentDidMount() {
const directionsService = new google.maps.DirectionsService();
const origin = { lat: 6.5244, lng: 3.3792 };
const destination = { lat: 6.4667, lng: 3.4500};
directionsService.route(
{
origin: origin,
destination: destination,
travelMode: google.maps.TravelMode.DRIVING,
waypoints: [
{
location: new google.maps.LatLng(6.4698, 3.5852)
},
{
location: new google.maps.LatLng(6.6018,3.3515)
}
]
},
(result, status) => {
if (status === google.maps.DirectionsStatus.OK) {
console.log(result)
this.setState({
directions: result
});
} else {
console.error(`error fetching directions ${result}`);
}
}
);
}
render() {
const GoogleMapExample = withGoogleMap(props => (
<GoogleMap
defaultCenter={{ lat: 6.5244, lng: 3.3792 }}
defaultZoom={13}
>
<DirectionsRenderer
directions={this.state.directions}
/>
</GoogleMap>
));
return (
<div>
<GoogleMapExample
containerElement={<div style={{ height: `500px`, width: "500px" }} />}
mapElement={<div style={{ height: `100%` }} />}
/>
</div>
);
}
}
export default Map;