从地址获取lat和lang以将其传递给反应传单

时间:2016-12-11 07:28:54

标签: javascript google-maps reactjs react-leaflet

反应传单需要lat和lng以在地图中显示所需的位置。如何从给定地址计算lat和lng?我需要两个地址的lat和lng。

我是否必须使用地理编码器并在其中包装renderMap函数?在我的代码中,我想要lat和lng of cityOrigen和cityDestino。

function renderMap(cityOrigen, cityDestino) {
  return <ReactMap crityOrigen={cityOrigen} cityDestino={cityDestino} />;
}

const ReactMap = ({ cityOrigen, cityDestino }) => {
  const position = [51.505, -0.09];
  return (
    <Map center={position} zoom={13} style={{ height: 500 }}>
      <TileLayer
        url='https://mt{s}.google.com/vt/lyrs=m&x={x}&y={y}&z={z}'
      />
      <Marker position={position}>
        <Popup>
          <span>A pretty CSS3 popup.</span>
        </Popup>
      </Marker>
    </Map>
  );
};

class CarResult extends Component {
  render() {
    const { carResult, origen, destino } = this.props;
    const cityOrigen = origen && origen.label.split(', ')[0];
    const cityDestino = destino && destino.label.split(', ')[0];
    const carResultMap = renderMap(cityOrigen, cityDestino);
    if (!carResult.fetching) {
      return (
        <div>
          Hemos encontrado {carResultList.length} ofertas para ti.
          <div className="car-result-container">
            <Grid fluid>
              <Row>
                <Col xs={12} sm={12} md={6}>
                  {carResultList}
                </Col>
                <Col x={12} sm={12} md={6}>
                  {carResultMap}
                </Col>
              </Row>
            </Grid>
          </div>
        </div>
      );
    }
    }

1 个答案:

答案 0 :(得分:0)

一些事情:

    <{1}}中的
  • 您有renderMap并且正在cityOrigen传递ReactMap组件。我确定这只是一个错字。

  • 您可以使用Leaflet Geocoder插件,例如this one。您可以将其放入crityOrigen函数中以解析位置(我假设在地址中为renderMapcityDestino)。