当前位置:  开发笔记 > 编程语言 > 正文

MapboxGL标记在React中保留地图容器

如何解决《MapboxGL标记在React中保留地图容器》经验,为你挑选了0个好方法。

我正在研究一个React/MapboxGL应用程序,每当我移动地图时,我的标记都会出现问题而离开指定地图区域的边界.我觉得可能还有一些其他更大的底层React渲染主体,我不完全理解这对我造成了这个问题.任何帮助将非常感激.谢谢!

另外,每当我点击地图时,我都注意到地图上有一个蓝色的可突出边框,这在我见过的任何教程/示例中都没有.这让我进一步相信这就是我将mapbox-gl与React集成的方式.

以下是我正在使用的组件 -

主要部分

import React from 'react';
import ReactDOM from 'react-dom';
import List from './List.jsx';
import MapComponent from './Map.jsx';
import Header from './Header.jsx';

const appConstants = require('../constants/appConstants');
const host = 'http://localhost:8020';
const socket = io(host);

let key = 0;

class MainComponent extends React.Component {

  constructor() {
     super();
     this.state = {
        events: []
    };
  }

componentWillMount() {
    socket.on('new event', (event) => this._handleStateChange(event));
}

render() {
    return (
        
Tabs on Tabs
); } _handleStateChange(event) { let newEvent = { browserName: event.new_val.browserName, location: event.new_val.location, osName: event.new_val.osName, key: key, eventName: event.new_val.event, interval: event.new_val.interval }; key++; this.state.events.unshift(newEvent); let events = this.state.events.slice(0, 10); this.setState({events}); } } ReactDOM.render(, document.getElementById('main'));

地图组件

class MapComponent extends React.Component {

    constructor() {
        super();
    };

    componentDidMount() {
        /* On first load - center */
        map = new mapboxgl.Map({
            container: 'map',
            style: mapConstants.style,
            center: mapConstants.hqCoordinates,
            zoom: 11
        });

        /* on initial load setup the location + icon */
        map.on('load', function () {

            const el = document.createElement('img');
            el.className = 'marker';
            el.style.backgroundImage = 'url(./assets/images/banana.png)';
            el.style.width = mapConstants.markerWidth;
            el.style.height = mapConstants.markerHeight;

            new mapboxgl.Marker(el)
               .setLngLat(mapConstants.hqCoordinates)
               .addTo(map);
        });

   }

    /* called when the props are updated */
    componentWillUpdate() {
       this._handleEventChange(this.props.events)
    }


    /* helper functions */
    _handleEventChange(events) {
        /* get most recent event and fly there */
        const mostRecentEvent = events[0];

        /* map box coordinates: [lng, lat ] */
        const mostRecentLocation = mostRecentEvent.location.split(',').reverse();

        map.flyTo({
           center: mostRecentLocation,
           zoom: 11
        });

        /* check if data source has been added */
        const existingEvent = map.getSource('mostRecentEvent');

        /* if data source exists, update the data */
        if (existingEvent) {
            existingEvent.setData({
                "type": "Feature",
                "geometry": {
                   "type": 'Point',
                   "coordinates": mostRecentLocation
               },
               "properties": {
                   "title":helperService.getShortEventName(mostRecentEvent.eventName),
                   "icon": mostRecentEvent.browserName.toLowerCase()
                }
            })
        } else {
            /* otherwise this is the first event and we need to add a source & layer */
            map.addSource('mostRecentEvent', {
                "type": "geojson",
                "data": {
                    "type": "Feature",
                    "geometry": {
                         "type": 'Point',
                         "coordinates": mostRecentLocation
                     },
                    "properties": {
                       "title": helperService.getShortEventName(mostRecentEvent.eventName),
                        "icon": mostRecentEvent.browserName.toLowerCase()
                    }
                }
            });

            map.addLayer({
                "id": "mostRecentEvent",
                "type": "symbol",
                "source": "mostRecentEvent",
                "layout": {
                    "icon-image": "{icon}",
                    "text-field": "{title}",
                    "text-font": ["Open Sans Semibold", "Arial Unicode MS Bold"],
                    "text-offset": [0, 0.6],
                    "text-anchor": "top",
                    "text-allow-overlap": true,
                    "text-size": 20
                },
                "paint": {
                    "text-color": '#FF0080'
                }
             });
        }
    }

    render() {
        return (
{this.props.heading}
); } } let styles = { map: { height: '500', width: '100%', zIndex: 0 } }; module.exports = MapComponent;

还有几个问题的图像

中心

移动地图后

推荐阅读
黄晓敏3023
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有