简体   繁体   English

如何在React Google Maps中旋转标记图标(SVG图像)

[英]How to rotate marker icon (svg image) in react google maps

For my React app, I'm implementing Google Maps. 对于我的React应用,我正在实现Google Maps。 I'm using react-google-maps library for maps. 我正在将react-google-maps库用于地图。 I want to rotate my customize marker icon . 我想旋转我的自定义标记图标

I have an SVG file (which has multiple paths and polygon) but Google Maps required in string type only (I think they will create SVG after giving path to icon) same problem as link . 我有一个SVG文件(具有多个路径和多边形),但是Google Maps仅需要字符串类型(我认为它们会在提供图标的路径后创建SVG)与link相同的问题。

I followed same ans but I'm implementing in React so I'm unable to convert that code in React. 我遵循相同的ans,但是我在React中实现,因此无法在React中转换该代码。

Working code in JS JS中的工作代码

var measle = new google.maps.Marker({
    position: map.getCenter(),
    map: map,
    optimized: false,
    icon: {
      url: "https://maps.gstatic.com/intl/en_us/mapfiles/markers2/measle.png",
      size: new google.maps.Size(7, 7),
      anchor: new google.maps.Point(3.5, 3.5)
    }
  })

var rotationAngle = 10;
  google.maps.event.addListenerOnce(map, 'idle', function() {
    setInterval(function() {
    console.log("transform: rotate(" + rotationAngle + 'deg)');  $('img[src="http://www.geocodezip.com/mapIcons/SO_20170925_multiplePaths_mod.svg"]').css({
        'transform': 'rotate(' + rotationAngle + 'deg)',
        'transform-origin': '39px 60px'
      });
      rotationAngle += 10;
      rotationAngle %= 360;
    }, 1000);
  });
}

My code in React (in React Google Maps) 我在React中的代码(在React Google Maps中)

componentWillUpdate(){
      var rotationAngle = 10;
      if(this.props.refs.map){
        setInterval(() => {
                let img = React.createElement("img", {
                src: "../../../public/images/amb.svg",
                style: {transform: "rotate(" + rotationAngle + "deg)", transformOrigin: "39px 60px"},
                ref: image => {
                  // this.handleSize(image);
                }
              });
              rotationAngle += 10;
              rotationAngle %= 360;
            }, 1000);

      }
    }

    ---------------------------------------------
    <GoogleMap
        defaultCenter={props.MapCenter}
        defaultZoom={15}
        ref={props.onMapMounted}
        onCenterChanged={props.onCenterChanged}
        defaultOptions={{streetViewControl: false,mapTypeControl: false,
        styles:[
      {
        "featureType": "poi.business",
        "stylers": [
          {
            "visibility": "off"
          }
        ]
      },
      {
        "featureType": "poi.park",
        "elementType": "labels.text",
        }}
      >
      {(BStatus !== 'picked' || BStatus !== 'dropped' || BStatus !== 'completed') && props.directions  && <DirectionsRenderer directions={props.directions} options={{suppressMarkers: true}} />}
        {props.pickupLocation && 
           <Marker 
           position={props.pickupLocation}
           visible={props.isShowPickMarker ? true : false}
           icon={require('../../../public/images/pick.png')}
         >
         </Marker>
         }  
         {props.dropoffLocation && <Marker
           position={props.dropoffLocation}
           visible={props.isShowDropMarker ? true : false} 
           icon={require('../../../public/images/drop.png')}
         >
            </Marker>
           }
        { props.waypoints && <Marker
            position={props.waypoints}
            visible={props.isShowDropMarker ? true : false} 
            icon = {{
                  url: require('../../../public/images/amb.svg'),
                }}
            >
            </Marker>
           }
      </GoogleMap>

My problem is I'm unable to set rotationAngle value to map icon . 我的问题是我无法将rotationAngle值设置为map icon please help me out, I have been stuck for a long time. 请帮帮我,我已经被困了很长时间了。

You can achieve the same with document WEB API. 您可以使用文档WEB API实现相同的目的。

document.querySelector('[src*="../../../public/images/amb.svg"]').style.transform = 'rotate(' + rotationAngle + 'deg)';
document.querySelector('[src*="../../../public/images/amb.svg"]').style.transition = 'transform 1s linear';

Please make sure your src is correct. 请确保您的src是正确的。 Further you don't need to add 100Kbs in your app size by adding Jquery. 此外,您不需要通过添加Jquery在您的应用程序大小中添加100Kbs。 You can skip it by this approach. 您可以通过这种方法跳过它。

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

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