React JSON ファイルを地理的な場所にバインド

    Ignite UI for React Map マップは、さまざまな種類のファイルからロードされた地理データをプロットできます。たとえば、JavaScript Object Notation (JSON) ファイルから地理的位置をロードできます。

    React JSON ファイルを地理的な場所にバインドの例

    EXAMPLE
    DATA
    TSX

    このサンプルが気に入りましたか? 完全な Ignite UI for Reactツールキットにアクセスして、すばやく独自のアプリの作成を開始します。無料でダウンロードできます。

    データ例

    JSON ファイルからのデータの例:

    [
       { "name": "Sydney Island", "lat": -16.68972, "lon": 139.45917 },
       { "name": "Sydney Creek",  "lat": -16.3,     "lon": 128.95 },
       { "name": "Mount Sydney",  "lat": -21.39864, "lon": 121.193 },
     // ...
    ]
    json
    Ignite UI for React | CTA Banner

    コード スニペット

    以下のコードは、マップコンポーネント内の IgrGeographicHighDensityScatterSeries を、ロードされた JSON ファイルから作成された地理的位置を含むオブジェクトの配列にバインドします。

    import { IgrGeographicSymbolSeries } from 'igniteui-react-maps';
    import { MarkerType } from 'igniteui-react-charts';
    // ...
    
    public componentDidMount() {
        // fetching JSON data with geographic locations from public folder
        fetch(url + "/Data/WorldCities.json")
            .then((response) => response.text())
            .then(data => this.onDataLoaded(data));
    }
    
    public onDataLoaded(jsonData: any[]) {
    
        const geoLocations: any[] = [];
        // parsing JSON data and using only cities that are capitals
        for (const jsonItem of jsonData) {
            if (jsonItem.cap) {
                const location = {
                    latitude: jsonItem.lat,
                    longitude: jsonItem.lon,
                    population: jsonItem.pop,
                    city: jsonItem.name,
                    country: jsonItem.country
                };
                geoLocations.push(location);
            }
        }
        // creating symbol series with loaded data
        const geoSeries = new IgrGeographicSymbolSeries( { name: "series" });
        geoSeries.dataSource = geoLocations;
        geoSeries.markerType = MarkerType.Circle;
        geoSeries.latitudeMemberPath  = "latitude";
        geoSeries.longitudeMemberPath = "longitude";
        geoSeries.markerBrush = "LightGray";
        geoSeries.markerOutline = "Black";
        // adding symbol series to the geographic amp
        this.geoMap.series.add(geoSeries);
    }
    ts

    API リファレンス