npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@uiw/react-baidu-map-info-window

v2.7.2

Published

Baidu Map info-window Components for React.

Downloads

9,691

Readme

InfoWindow 信息窗口

Buy me a coffee npm version Downloads

表示地图上包含信息的窗口。

import { InfoWindow, useInfoWindow } from '@uiw/react-baidu-map';
// 或者单独安装使用
import InfoWindow, { useInfoWindow } from '@uiw/react-baidu-map-info-window';

基本用法

import React, { useState } from 'react';
import { Map, InfoWindow, APILoader } from '@uiw/react-baidu-map';

const Example = () => {
  const [visiable, setVisiable] = useState(true);
  const [ isOpen, setIsOpen ] = useState(true);
  const [ content, setContent ] = useState('上海市 <del>青浦区</del> 徐泾镇盈港东路');

  function infoWindowRef(props) {
    if (props && props.infoWindow) {
      console.log('infoWindow:', props.infoWindow, props.map, props.BMap);
    }
  }
  return (
    <>
      <button onClick={() => setIsOpen(!isOpen)}>{isOpen ? '关闭' : '打开'} isOpen={String(isOpen)}</button>
      <button onClick={() => setVisiable(!visiable)}>{visiable ? '隐藏' : '显示'}visiable={String(visiable)}</button>
      <input value={content} onChange={(e) => setContent(e.target.value)} />
      <Map zoom={13} center={{ lng: 121.460977, lat: 31.227906 }}>
        <InfoWindow
          ref={infoWindowRef}
          visiable={visiable}
          isOpen={isOpen}
          onClose={() => {
            console.log(':onClose');
          }}
          position={{ lng: 121.501365, lat: 31.224942 }}
          content={content}
          title="地址信息一"
        />
      </Map>
    </>
  );
}

const Demo = () => (
  <div style={{ width: '100%', height: '350px' }}>
    <APILoader akay="eYpCTECSntZmw0WyoQ7zFpCRR9cpgHFG">
      <Example />
    </APILoader>
  </div>
);
export default Demo;

~~content~~ -> children 支持 JSX.Element

通过 children 支持 JSX.Element 的方式展现内容,因为窗口信息内容通过 content 展示内容,它支持 string/HTMLElement 添加事件并不方便。

🚧 如果同时设置了 content 属性且有 childrencontent 将失效被忽略。

import React, { useState } from 'react';
import { Map, InfoWindow, APILoader } from '@uiw/react-baidu-map';

const Example = () => {
  const [visiable, setVisiable] = useState(true);
  const [ isOpen, setIsOpen ] = useState(true);
  const [ count, setCount ] = useState(0);
  return (
    <>
      <button onClick={() => setIsOpen(!isOpen)}>{isOpen ? '关闭' : '打开'} isOpen={String(isOpen)}</button>
      <button onClick={() => setVisiable(!visiable)}>{visiable ? '隐藏' : '显示'}visiable={String(visiable)}</button>
      <Map zoom={13} center={{ lng: 121.460977, lat: 31.227906 }}>
        <InfoWindow
          visiable={visiable}
          isOpen={isOpen}
          onClose={() => {
            console.log(':onClose');
          }}
          position={{ lng: 121.501365, lat: 31.224942 }}
          title={<div>地址信息二</div>}
          content="test"
        >
          <div>
            上海市 <del>青浦区</del> 徐泾镇盈港东路 Good!
            <br/>
            <button onClick={() => setCount(count+1)}>{count} Count={String(count)}</button>
          </div>
        </InfoWindow>
      </Map>
    </>
  );
}

const Demo = () => (
  <div style={{ width: '100%', height: '350px' }}>
    <APILoader akay="eYpCTECSntZmw0WyoQ7zFpCRR9cpgHFG">
      <Example />
    </APILoader>
  </div>
);
export default Demo;

使用 hooks

infoWindow, setInfoWindow, isOpen, setIsOpen

import React, { useRef, useState, useEffect } from 'react';
import { Map, Provider, useMap, InfoWindow, useInfoWindow, APILoader } from '@uiw/react-baidu-map';

const Example = () => {
  const divElm = useRef(null);
  const { setContainer, map } = useMap({
    zoom: 13, center: { lng: 121.460977, lat: 31.227906 },
    widget: ['GeolocationControl', 'NavigationControl']
  });

  const [title, setTitle] = useState('地址信息二');
  const position = { lng: 121.501365, lat: 31.224942 };
  const { infoWindow, isOpen, setIsOpen, Portal, PortalTitle } = useInfoWindow({
    position, enableCloseOnClick: false, isOpen: true,
    // content: '上海市 <del>青浦区</del> 徐泾镇盈港东路',
    // title,
    onClose: () => {
      console.log('onClose:');
    }
  });
  useEffect(() => {
    if (divElm.current) {
      setContainer(divElm.current);
    }
  });
  return (
    <>
      <button onClick={() => setIsOpen(!isOpen)}>{isOpen ? '显示' : '隐藏'}</button>
      <input value={title} onChange={(e) => setTitle(e.target.value)} />
      <div ref={divElm} style={{ height: 350 }} />
      <Portal>上海市 <del>青浦区</del> 徐泾镇盈港东路</Portal>
      <PortalTitle> {title} </PortalTitle>
    </>
  )
}

const Demo = () => {
  return (
    <div style={{ width: '100%' }}>
      <APILoader akay="eYpCTECSntZmw0WyoQ7zFpCRR9cpgHFG">
        <Provider>
          <Example />
        </Provider>
      </APILoader>
    </div>
  )
};
export default Demo;

Props

| 参数 | 说明 | 类型 | 默认值 | | ----- | ----- | ----- | ----- | | position | 必填 指定的经度和纬度创建一个地理点坐标 | Point | - | | content | 展示文本内容,支持 HTML 内容字符串 | string | - | | children | 展示文本内容,🚧 添加 children 的时候 content 将失效。 | JSX.Element | - | | isOpen | 窗口是否打开 | Point | - | | visiable | 覆盖物是否可见。此属性来自继承 Overlay 实例对象。 | boolean | - | | width | 信息窗宽度,单位像素。取值范围:0, 220 - 730。如果您指定宽度为 0,则信息窗口的宽度将按照其内容自动调整 | number | true | | height | 信息窗高度,单位像素。取值范围:0, 60 - 650。如果您指定宽度为 0,则信息窗口的宽度将按照其内容自动调整 | number | - | | maxWidth | 信息窗最大化时的宽度,单位像素。取值范围:220 - 730 | number | - | | offset | 信息窗位置偏移值。默认情况下在地图上打开的信息窗底端的尖角将指向其地理坐标,在标注上打开的信息窗底端尖角的位置取决于标注所用图标的 infoWindowOffset 属性值,您可以为信息窗添加偏移量来改变默认位置 | Size | - | | maxContent | 信息窗口最大化时所显示内容,支持HTML内容 | string | - | | title | 信息窗标题文字,支持 HTML 内容 | string | - | | message | 自定义部分的短信内容,可选项。完整的短信内容包括:自定义部分+位置链接,不设置时,显示默认短信内容。短信内容最长为140个字 | string | - | | enableAutoPan | 是否开启信息窗口打开时地图自动移动(默认开启) | boolean | - | | enableCloseOnClick | 是否开启点击地图关闭信息窗口(默认开启) | boolean | - | | enableMaximize | 启用窗口最大化功能。需要设置最大化后信息窗口里的内容,该接口才生效 | boolean | - |

事件

| 参数 | 说明 | 类型 | | ----- | ----- | ----- | | onClose | 信息窗口被关闭时触发此事件 | (event: { type: string, target: any, point: Point }) => void; | | onOpen | 信息窗口被打开时触发此事件 | (event: { type: string, target: any, point: Point }) => void; | | onMaximize | 信息窗口最大化后触发此事件 | (event: { type: string, target: any }) => void; | | onRestore | 信息窗口还原时触发此事件 | (event: { type: string, target: any }) => void; | | onClickclose | 点击信息窗口的关闭按钮时触发此事件 | (event: { type: string, target: any }) => void; |