简体   繁体   English

SVG(使用 React Simple Maps)未在移动设备上显示

[英]SVG (using React Simple Maps) not displaying on mobile

Hello (first time posting on Stack Overflow, so apologies for any breaches in etiquette...)您好(第一次在 Stack Overflow 上发帖,对于任何违反礼仪的行为,我们深表歉意……)

I'm currently building a React app that contains an interactive SVG map built using React Simple Maps .我目前正在构建一个 React 应用程序,其中包含使用React Simple Maps构建的交互式 SVG地图 Everything is working as expected in my desktop browser, however the map itself does not load on mobile devices (I've tested several) and I can't figure out why.在我的桌面浏览器中一切都按预期工作,但是地图本身无法在移动设备上加载(我已经测试了几个),我不知道为什么。 I've included the code from the component in question below (I've removed some irrelevant parts of the code):我已经包含了下面相关组件的代码(我已经删除了代码的一些不相关部分):

import React, { memo, Component } from 'react';
import { ZoomableGroup, ComposableMap, Geographies, Geography } from "react-simple-maps";
import geoUrl from "../data/topo.json";
import Country from './Country'
import { CSSTransition, SwitchTransition } from "react-transition-group";

class Map extends Component { 

    constructor(props){
        super(props);

        this.state = {
            country: "",
            dish: "",
            description: "",
            photo: "",
            recipe: "",
            selected: false,
        }

        this.handleBack = this.handleBack.bind(this);
        this.handleAbout = this.handleAbout.bind(this);
        this.handleList = this.handleList.bind(this);
    }

    handleEnter(country, dish, description, photo, recipe){
        this.setState({
            country: country,
            dish: dish,
            description: description,
            photo: photo,
            recipe: recipe
        })
    }
    
    render(){ 
    
        const { country, dish, description, photo, recipe, selected } = this.state;
        const countries = geoUrl.objects.ne_50m_admin_0_countries.geometries;

        return(
            <SwitchTransition>
                <CSSTransition
                    classNames="transition"
                    transitionAppearTimeout={50000}
                    key={ selected }
                    in={ selected }
                    unmountOnExit
                    appear
                >         
                    <> 
                        <section className="map">
                            <div className="container">
                                <ComposableMap width="1200" data-tip="" projectionConfig={{ scale: 200 }} >
                                    <ZoomableGroup>
                                        <Geographies geography={geoUrl}>
                                            {({ geographies }) =>
                                                geographies.map(geo =>
                                                    <a href="#country">
                                                        <Geography 
                                                            key={geo.rsmKey} 
                                                            geography={geo}
                                                            onMouseEnter={() => {
                                                                const { NAME } = geo.properties;
                                                                this.props.setTooltipContent(`${NAME}`);
                                                            }}
                                                            onMouseLeave={() => {
                                                                this.props.setTooltipContent("");
                                                            }}
                                                            onClick={() => {
                                                                const { NAME, DISH, DESCRIPTION, PHOTO, RECIPE } = geo.properties;
                                                                this.handleEnter(NAME, DISH, DESCRIPTION, PHOTO, RECIPE);
                                                            }}
                                                            fill="#44BBA4"
                                                            stroke="#E94F37"
                                                            strokeWidth="0.5"
                                                            style={{
                                                                default: {
                                                                    outline: 'none'
                                                                },
                                                                hover: {
                                                                    fill: "#E94F37",
                                                                    outline: 'none'
                                                                },
                                                                pressed: {
                                                                    outline: 'none'
                                                                }
                                                            }}
                                                        />
                                                    </a>
                                                )
                                            }
                                        </Geographies>
                                    </ZoomableGroup>
                                </ComposableMap>
                            </div>
                        </section>
                    </>
                </CSSTransition>
            </SwitchTransition>
        );
    }
}

export default memo(Map);

If anyone might have any insights that would be fantastic!如果有人可能有任何见解,那将是太棒了! Please let me know if you would like to see more code/screenshots etc. Thanks!如果您想查看更多代码/屏幕截图等,请告诉我。谢谢!

Thanks to @Maitham for helping me fine the answer - by adding a property of style={{width: "100%"}} to <ComposableMap> , the issue now seems to be resolved!感谢@Maitham 帮助我完善答案 - 通过向<ComposableMap>添加style={{width: "100%"}}属性,问题现在似乎已解决! This SHOULD NOT replace the width="1200" property (if you remove this the map will not render at all on mobile or desktop), but should be in addition to it:这不应该替换width="1200"属性(如果您删除它,地图将根本不会在移动设备或桌面上呈现),但应该补充它:

<ComposableMap width="1200" style={{ width: "100%" }} data-tip="" projectionConfig={{ scale: 200 }} >

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

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