Anchor and rotation for DOM marker

瘦欲@ 提交于 2020-01-06 08:11:15

问题


I can set the rotation to the css of the DOM marker in here map api. However, I can't set the anchor. That would lead to the origin of the spin deviate from the coordination of the marker. See the picture below:

How to resolve it?

The relevant code is like the following:

import { renderToStaticMarkup } from 'react-dom/server';
import { PositionToLatLng } from './utils';

export class DomMarker {
  constructor(map, options) {
    const { position, children, angle, ...others } = options;
    this.marker = new H.map.DomMarker(PositionToLatLng(position), {
      icon: new H.map.DomIcon(renderToStaticMarkup(
        <div>
          <div
            style={{
              transform: `rotate(${angle}deg)`,
              cursor: 'pointer'
            }}
          >
            {children}
          </div>
        </div>
      )),
    });
    this.map = map;
    this.map.addObject(this.marker);
  }

  remove() {
    this.map.removeObject(this.marker);
  }
}

It works with "renderToStaticMarkup". But it renders nothing if I use "renderToDiv".

import ReactDOM from 'react-dom';

export function renderToDiv(element) {
  const content = document.createElement('div');
  ReactDOM.render(element, content);
  return content;
}

I print the return value of "renderToDiv", it looks like the following:


回答1:


The anchor parameters only works for H.map.Icon. Use CSS styles to center an H.map.DomIcon.

Assuming your icon is 24px width and height:

<div style="left: -12px; top: -12px;">
   // icon content
</div>


来源:https://stackoverflow.com/questions/58786541/anchor-and-rotation-for-dom-marker

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!