Skip to content

Commit

Permalink
Map
Browse files Browse the repository at this point in the history
  • Loading branch information
JserWang committed Jul 28, 2018
0 parents commit ba31f55
Show file tree
Hide file tree
Showing 27 changed files with 10,798 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# See https://help.github.com/ignore-files/ for more about ignoring files.

# dependencies
/node_modules

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
2,452 changes: 2,452 additions & 0 deletions README.md

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions images.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module '*.svg'
declare module '*.png'
declare module '*.jpg'
25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "rc-bmap",
"version": "0.1.0",
"private": true,
"dependencies": {
"prop-types": "^15.6.2",
"react": "^16.4.1",
"react-dom": "^16.4.1",
"react-scripts-ts": "2.16.0",
"tslint-eslint-rules": "^5.3.1"
},
"scripts": {
"start": "react-scripts-ts start",
"build": "react-scripts-ts build",
"test": "react-scripts-ts test --env=jsdom",
"eject": "react-scripts-ts eject"
},
"devDependencies": {
"@types/jest": "^23.3.1",
"@types/node": "^10.5.3",
"@types/react": "^16.4.7",
"@types/react-dom": "^16.0.6",
"typescript": "^2.9.2"
}
}
Binary file added public/favicon.ico
Binary file not shown.
46 changes: 46 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<!--
manifest.json provides metadata used when your web app is added to the
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
<style>
html, body, #root {
height: 100%;
width: 100%;
}
</style>
</head>
<body>
<noscript>
You need to enable JavaScript to run this app.
</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
15 changes: 15 additions & 0 deletions public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
}
],
"start_url": "./index.html",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
28 changes: 28 additions & 0 deletions src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
.App {
text-align: center;
}

.App-logo {
animation: App-logo-spin infinite 20s linear;
height: 80px;
}

.App-header {
background-color: #222;
height: 150px;
padding: 20px;
color: white;
}

.App-title {
font-size: 1.5em;
}

.App-intro {
font-size: large;
}

@keyframes App-logo-spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
9 changes: 9 additions & 0 deletions src/App.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import App from './App';

it('renders without crashing', () => {
const div = document.createElement('div');
ReactDOM.render(<App />, div);
ReactDOM.unmountComponentAtNode(div);
});
22 changes: 22 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as React from 'react';
import './App.css';

import logo from './logo.svg';

class App extends React.Component {
public render() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="App-title">Welcome to React</h1>
</header>
<p className="App-intro">
To get started, edit <code>src/App.tsx</code> and save to reload.
</p>
</div>
);
}
}

export default App;
4 changes: 4 additions & 0 deletions src/components/Map/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.mapContainer {
width: 100%;
height: 100%;
}
195 changes: 195 additions & 0 deletions src/components/Map/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
import * as React from 'react';
import './index.css';
import PropTypes from 'prop-types';
import { MAP_SET_OPTIONS, MAP_BOOLEAN_OPTIONS } from '../_base/options';
import { replaceInitialToUpper, getPoint, isPoint } from '../_base/util';
import { MAP_EVENTS } from '../_base/events';

const top: any = window || global;
(top.BMap as Object) = Object.create(null);

export interface IMapProps {
children?: React.ReactNode;
ak: string;
placeHolder?: string | React.ReactNode;
minZoom?: number;
maxZoom?: number;
defaultCursor?: string;
draggingCursor?: string;
mapStyle?: any;
center?: any;
mapType?: any;
zoom?: number;
highResolution?: boolean;
autoResize?: boolean;
mapClick?: boolean;
renderCallBack?: Function;
dragging?: boolean;
scrollWheelZoom?: boolean;
doubleClickZoom?: boolean;
keyboard?: boolean;
inertialDragging?: boolean;
continuousZoom?: boolean;
pinchToZoom?: boolean;
events?: any;
}

export default class Map extends React.Component<IMapProps, any> {

static defaultProps = {
placeHolder: '地图加载中...',
// 与官方文档保持一致
dragging: true,
scrollWheelZoom: false,
doubleClickZoom: true,
keyboard: false,
inertialDragging: false,
continuousZoom: true,
pinchToZoom: true,
autoResize: true,
highResolution: true,
mapClick: true,
center: { lng: 116.404, lat: 39.915 },
zoom: 15,
};

static propTypes = {
placeHolder: PropTypes.oneOfType([
PropTypes.string,
PropTypes.object,
]),
};

map: any;
mapContainerRef: any;
mapContainer: HTMLElement;

constructor(props: any) {
super(props);

// React 16
if (React.createRef) {
this.mapContainerRef = React.createRef();
} else {
this.mapContainerRef = (ref: any) => {
this.mapContainer = ref;
};
}
}

init = (BMap: any) => {
const { highResolution, autoResize, mapClick, renderCallBack, ...resetProps } = this.props;
this.mapContainer = this.mapContainer || this.mapContainerRef.current;
const map = this.map = new BMap.Map(this.mapContainer, {
enableHighResolution: highResolution,
enableAutoResize: autoResize,
enableMapClick: mapClick,
});
top.bMapInstance = map;
this.processMapOptions(resetProps);
this.bindEvents();
if (renderCallBack) {
renderCallBack();
}
}

processMapOptions = (props: any) => {
const { map } = this;
MAP_SET_OPTIONS.forEach((key: string) => {
if (props[key]) {
const upKey = replaceInitialToUpper(key);
map[`set${upKey}`](props[key]);
}
});

MAP_BOOLEAN_OPTIONS.forEach((key: string) => {
const upKey = replaceInitialToUpper(key);
let prefix = 'disable';
if (props[key]) {
prefix = 'enable';
}
map[`${prefix}${upKey}`]();
});

if (props.center) {
let center = props.center;
if (isPoint(center)) {
center = getPoint(center.lng, center.lat);
map.centerAndZoom(center, props.zoom);
}
}

if (props.mapType) {
map.setMapType(top[props.mapType]);
}
}

bindEvents = () => {
const { map } = this;
const { events } = this.props;

MAP_EVENTS.forEach((eventName: string) => {
if (events[eventName]) {
map.addEventListener(eventName, (...args: any[]) => {
events[eventName].apply(null, args);
});
}
});
}

getMapScript = () => {
const { ak } = this.props;
if (Object.keys(top.BMap).length === 0) {
top.BMap._preloader = new Promise((resolve, reject) => {
top._initBaiduMap = function initBaiduMap() {
resolve(top.BMap);
top.document.body.removeChild($script);
top.BMap._preloader = null;
top._initBaiduMap = null;
};

const $script = document.createElement('script');
top.document.body.appendChild($script);
$script.src = `https://api.map.baidu.com/api?v=3.0&ak=${ak}&callback=_initBaiduMap`;
});

return top.BMap._preloader;
} else if (!top.BMap._preloader) {
return Promise.resolve(top.BMap);
}
return top.BMap._preloader;
}

componentDidMount() {
this.getMapScript().then(this.init);
}

componentWillReceiveProps(nextState: Object, nextProps: Object) {
this.processMapOptions(nextProps);
}

renderChildren = () => {
const { children } = this.props;
if (!this.map || !children) {
return null;
}
return React.Children.map(children, (child: any) => {
if (child) {
return React.cloneElement(child);
}
return null;
});
}

render() {
const { placeHolder } = this.props;
return (
<div style={{ width: '100%', height: '100%' }}>
<div ref={this.mapContainerRef} className="mapContainer">
{placeHolder}
</div>
{this.renderChildren()}
</div>
);
}
}
Loading

0 comments on commit ba31f55

Please sign in to comment.