Giter Club home page Giter Club logo

Comments (3)

stefanhk31 avatar stefanhk31 commented on May 28, 2024 1

Thanks! It works now that I've imported DeckGL and GeoJSON Layer. Your Sandbox was helpful. If anyone else is interested, here's my Map.js file:

import React, { Component } from 'react';
import MapGL, { NavigationControl, Marker } from 'react-map-gl';
import DeckGL, { GeoJsonLayer } from "deck.gl";
import Geocoder from "react-map-gl-geocoder";

const token = process.env.REACT_APP_API_KEY;

class Map extends Component {
    constructor(props) {
        super(props);
        this.state = {
            viewport: {
                latitude: 35.9555072,
                longitude: -83.9901184,
                zoom: 12,
                width: 500,
                height: 500
            },
            searchResultLayer: null
        };
    }

    mapRef = React.createRef()

    handleOnResult = e => {
        this.setState({
            searchResultLayer: new GeoJsonLayer({
                id: "search-result",
                data: e.result.geometry,
                getFillColor: [255, 0, 0, 128],
                getRadius: 1000,
                pointRadiusMinPixels: 10,
                pointRadiusMaxPixels: 10
            })
        });
    };

    render() {
        const { viewport, searchResultLayer } = this.state;

        const updateViewport = (viewport) => {
            this.setState({ viewport })
        };

        return (
            <div className="map-container">
                <MapGL {...viewport} ref={this.mapRef} mapStyle='mapbox://styles/mapbox/streets-v9' mapboxApiAccessToken={token} onViewportChange={updateViewport}>
                    <div className="nav">
                        <NavigationControl onViewportChange={updateViewport} />
                    </div>
                    <div className="geocoder">
                        <Geocoder mapRef={this.mapRef} onResult={this.handleOnResult} onViewportChange={updateViewport} mapboxApiAccessToken={token} position="top-right" />
                    </div>
                    <Marker latitude={35.95} longitude={-83.99} offsetLeft={-20} offsetTop={-10}>
                        <div><i class="fa fa-map-marker" aria-hidden="true"></i></div>
                    </Marker>
                    <DeckGL {...viewport} layers={[searchResultLayer]} />
                </MapGL>
            </div>
        );

    }

}

export default Map;

from react-map-gl-geocoder.

SamSamskies avatar SamSamskies commented on May 28, 2024

On your MapGL component, mapRef prop should be ref.

https://reactjs.org/docs/refs-and-the-dom.html

from react-map-gl-geocoder.

SamSamskies avatar SamSamskies commented on May 28, 2024

You'll also need to update your updateViewport function to something like...

const updateViewport = viewport => {
  this.setState({ ...this.state.viewport, ...viewport })
}

from react-map-gl-geocoder.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.