简体   繁体   English

如何使用React JS将FusionTablesLayer放入Google Maps API中?

[英]How put FusionTablesLayer in Google Maps API using React JS?

I have a doubt, I'm using ReactJS to separate modules, and I have a class called InitialMap, that I export to app.js: 我有一个疑问,我正在使用ReactJS来分隔模块,并且有一个名为InitialMap的类,我将其导出到app.js:

import React from 'react';
import loadGoogleMapsAPI from 'load-google-maps-API';

const MAP_STYLES = {
    height: '450px',
    width: '100%'

}

const OPTIONS = {
    center: {
        lat: 41.850033,
        lng: -87.6500523
    },
    zoom: 13,

}

const API_CONFIG = {
    key:'My_Key xxxxxxx',
    language: 'en'
}

export default class InitialMap extends React.Component{
    componentWillUnmount() {

        const allScripts = document.getElementsByTagName( 'script' );

        [].filter.call(
            allScripts,
            ( scpt ) => scpt.src.indexOf( 'My_Key xxxxxxx' ) >= 0
        )[ 0 ].remove();

        window.google = {};


    }

    componentDidMount() {

        loadGoogleMapsAPI( API_CONFIG ).then( googleMaps => {
            new googleMaps.Map( this.refs.map, OPTIONS  );
        }).catch( err => {
            console.warning( 'Something went wrong loading the map', err );
        });

    }

    render() {
        return (
            <div id="page-wrapper">
                <div id= "page-header">
                    <div ref="map" style={MAP_STYLES}>

                    </div>
                </div>
            </div>
        )
    }
}

The map is being shown on the screen, but I need put the markers! 屏幕上正在显示地图,但是我需要放置标记!

The problem is that I don't know how to put the Fusion Tables layer to use the marker, to show red points for example by Google! 问题是我不知道如何放置Fusion Tables图层来使用标记,例如通过Google显示红点!

Using just the script file + the id in index.html <div id="map"><div> it is working but I need put it to use in specific React's module, and call it in app.js file. 仅使用脚本文件+ index.html <div id="map"><div>可以正常工作,但是我需要在特定的React模块中使用它,并在app.js文件中调用它。 In the documentation of Google is an example is this way: 在Google文档中的示例就是这种方式:

    function initMap() {
  var map = new google.maps.Map(document.getElementById('map'), {
    center: {lat: 41.850033, lng: -87.6500523},
    zoom: 11
  });

  var layer = new google.maps.FusionTablesLayer({
    query: {
      select: '\'Geocodable address\'',
      from: '1mZ53Z70NsChnBMm-qEYmSDOvLXgrreLTkQUvvg'
    }
  });
  layer.setMap(map);
}

(this var layer that I need to put !!!) in my code (我需要在其中添加此var层!!!)在我的代码中

If anyone can help I will appreciate it! 如果有人可以帮助,我将不胜感激! PS: I'm new in React JS! PS:我是React JS的新手!

If you loaded the API on the page it should work exactly the same way, just save your map instance in your class when you create it then you will be able to use it in the other calls. 如果您在页面上加载了API,则它应该以完全相同的方式工作,只需在创建地图实例时将其保存在您的类中,然后您便可以在其他调用中使用它。

    loadGoogleMapsAPI( API_CONFIG ).then( googleMaps => {
        this.map = new googleMaps.Map( this.refs.map, OPTIONS  );

        var layer = new google.maps.FusionTablesLayer({
          query: {
            select: '\'Geocodable address\'',
            from: '1mZ53Z70NsChnBMm-qEYmSDOvLXgrreLTkQUvvg'
          }
        });
        layer.setMap(this.map);
    }).catch( err => {
        console.warning( 'Something went wrong loading the map', err );
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM