gribnoysup / react-yandex-maps

Yandex Maps API bindings for React
MIT License
327 stars 116 forks source link

Build route from coordinates #14

Closed binchik closed 7 years ago

binchik commented 7 years ago

Hi, @gribnoysup, awesome library. Is there a way to draw a route from point A to point B?

gribnoysup commented 7 years ago

Hi, @binchik! Thank you 😸 Could you be a little more specific? Maybe a demo in Yandex Maps Sandbox, that I can look at?

binchik commented 7 years ago

@gribnoysup, a user can build routes using RouteEditor control. Is it possible to draw routes programmatically? Here is a demo on jsfiddle:

http://jsfiddle.net/xhwom6aw/

gribnoysup commented 7 years ago

@binchik right now there are no react elements for route, but it is certainly worth adding to the library!

Although it is not possible with some kind of react elements, there is a possible solution, as you always have an access to yandex.maps Map (and any other) instance, the solution could look something like this:

import React from 'react'
import { YMaps, Map } from 'react-yandex-maps'

const mapState = {
  center: [55.751574, 37.573856],
  zoom: 9,
  controls: []
}

class Example extends React.Component {
  constructor() {
    super()
    this.map = null
  }

  onApiAvaliable(ymaps) {
    ymaps.route([
      'ΠšΠΎΡ€ΠΎΠ»Π΅Π²',
      { type: 'viaPoint', point: 'ΠœΡ‹Ρ‚ΠΈΡ‰ΠΈ' },
      'Π₯ΠΈΠΌΠΊΠΈ',
      { type: 'wayPoint', point: [55.811511, 37.312518] }
    ], {
      mapStateAutoApply: true
    }).then((route) => {
      route.getPaths().options.set({
        // Π² Π±Π°Π»ΡƒΠ½Π΅ Π²Ρ‹Π²ΠΎΠ΄ΠΈΠΌ Ρ‚ΠΎΠ»ΡŒΠΊΠΎ ΠΈΠ½Ρ„ΠΎΡ€ΠΌΠ°Ρ†ΠΈΡŽ ΠΎ Π²Ρ€Π΅ΠΌΠ΅Π½ΠΈ двиТСния с ΡƒΡ‡Π΅Ρ‚ΠΎΠΌ ΠΏΡ€ΠΎΠ±ΠΎΠΊ
        balloonContentBodyLayout: ymaps.templateLayoutFactory.createClass('$[properties.humanJamsTime]'),
        // ΠΌΠΎΠΆΠ½ΠΎ Π²Ρ‹ΡΡ‚Π°Π²ΠΈΡ‚ΡŒ настройки Π³Ρ€Π°Ρ„ΠΈΠΊΠΈ ΠΌΠ°Ρ€ΡˆΡ€ΡƒΡ‚Ρƒ
        strokeColor: '0000ffff',
        opacity: 0.9
      });

      // добавляСм ΠΌΠ°Ρ€ΡˆΡ€ΡƒΡ‚ Π½Π° ΠΊΠ°Ρ€Ρ‚Ρƒ
      this.map.geoObjects.add(route);
    });
  }

  render() {
    return (
      <YMaps onApiAvaliable={(ymaps) => this.onApiAvaliable(ymaps)}>
        <Map 
          state={mapState} 
          instanceRef={(ref) => this.map = ref}
        />
      </YMaps>
    )
  }
}

export default Example

Here is a webpackbin with working example

binchik commented 7 years ago

@gribnoysup, that is super helpful. Thanks!

namazbaev commented 3 years ago

How to drawing Routing A and B points?