简体   繁体   English

React.js动态地图方向渲染器

[英]React.js dynamic map directions renderer

I've been wondering how to make a dynamic map directions/route. 我一直想知道如何制作动态地图路线/路线。 I am using this plugin Directions Renderer but this is a static example. 我正在使用此插件Directions Renderer,但这是一个静态示例。 I just want to make a route using my input fields. 我只想使用我的输入字段进行路由。

Here's my code: 这是我的代码:

     /* global google */
       import { default as React, Component,} from "react";
       import { withGoogleMap, GoogleMap, DirectionsRenderer,} from "../../../lib";

const DirectionsExampleGoogleMap = withGoogleMap(props => (
  <GoogleMap
    defaultZoom={7}
    defaultCenter={props.center}
  >
    {props.directions && <DirectionsRenderer directions={props.directions} />}
  </GoogleMap>
));

export default class DirectionsExample extends Component {

 constructor(props) {
    super(props);
    this.state = {
        origin: '',
        destination: '',
    }

    this.handleOrigin = this.handleOrigin.bind(this);
    this.handleDestination = this.handleDestination.bind(this);
}

handleOrigin(event) {
        event.preventDefault()
        this.setState({origin: event.target.value});
    }

handleDestination(event) {
    event.preventDefault()
    this.setState({destination: event.target.value});
}

  componentDidMount() {
    const DirectionsService = new google.maps.DirectionsService();

    DirectionsService.route({
      origin: new google.maps.LatLng(this.state.origin),
      destination: new google.maps.LatLng(this.state.destination),
      travelMode: google.maps.TravelMode.DRIVING,
    }, (result, status) => {
      if (status === google.maps.DirectionsStatus.OK) {
        this.setState({
          directions: result,
        });
      } else {
        console.error(`error fetching directions ${result}`);
      }
    });
  }

  render() {
    return (
      <input type='text' value={this.state.origin} onChange=
         {this.handleOrigin} />
      <input type='text' value={this.state.destination} onChange2=
         {this.handleDestination}/>
      <DirectionsExampleGoogleMap
        containerElement={
          <div style={{ height: `100%` }} />
        }
        mapElement={
          <div style={{ height: `100%` }} />
        }
        center={this.state.origin}
        directions={this.state.directions}
      />
    );
  }
}

to shed some light on your issue, I personally use 为了阐明您的问题,我个人使用

    import PlacesAutocomplete from 'react-places-autocomplete';
    import { geocodeByAddress, geocodeByPlaceId } from 'react-places 
    autocomplete';

which creates a nice autocomplete field. 这会创建一个不错的自动填充字段。 And I also use these two npm packages below to display my maps. 我还使用下面的这两个npm包来显示我的地图。

    import Map, {GoogleApiWrapper} from 'google-maps-react'
    import Marker from 'google-maps-react'

Hopefully this helps in some way, let me know if you want some help using these packages and I can display some examples, cheers. 希望这可以以某种方式有所帮助,如果您需要使用这些软件包的帮助,请告诉我,我可以展示一些示例,欢呼。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM