LoginSignup
0
1

More than 5 years have passed since last update.

React-Leaflet

Last updated at Posted at 2018-04-06

React-Leaflet · ⚛️ React components for 🍃 Leaflet maps

webプロジェクト作成

package.jsonに追加

    "leaflet": "1.3.1",
    "@types/react-leaflet": "1.1.5",
    "@types/prop-types": "15.5.2",
    "react-leaflet": "1.9.1"

site.css

.leaflet-container {
    height: 400px;
    width: 100%;
}

boot.tsx

/ClientApp/boot.tsx

// leaflet css
import '../node_modules/leaflet/dist/leaflet.css';

routes.tsx

/ClientApp/routes.tsx

import { MapWanNavi } from './components/Map';

export const routes = <Layout>
    <Route path='/Map' component={MapWanNavi} />
</Layout>;

Map.tsx

/ClientApp/components/Map.tsx

import * as React from 'react';
import { RouteComponentProps } from 'react-router';
import { Map, TileLayer, Marker, Popup } from 'react-leaflet';
import { LatLngLiteral } from 'leaflet';


interface ReactLeafletState {
    lat: number;
    lng: number;
    zoom: number;
}


export class MapWanNavi extends React.Component<RouteComponentProps<{}>, ReactLeafletState> {
    constructor() {
        super();

        this.state = {
            lat: 51.505,
            lng: -0.09,
            zoom: 13,
        }
    }


    render() {
        var position: LatLngLiteral = { lat: this.state.lat, lng: this.state.lng};

        return (
            <Map center={ position }  zoom={this.state.zoom}>
                <TileLayer
                    attribution="&amp;copy <a href=&quot;http://osm.org/copyright&quot;>OpenStreetMap</a> contributors"
                    url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
                />
                <Marker position={ position }>
                    <Popup>
                        <span>
                            A pretty CSS3 popup. <br /> Easily customizable.
                        </span>
                    </Popup>
                </Marker>
            </Map>
        )
    }
}

result

image

link

Installation · React-Leaflet

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1