|
@@ -2,24 +2,21 @@ var _extends = Object.assign || function (target) { for (var i = 1; i < argument
|
|
|
|
|
|
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
|
|
|
|
|
|
-import forOwn from 'lodash/forOwn';
|
|
|
-import isEqual from 'lodash/isEqual';
|
|
|
-import isNil from 'lodash/isNil';
|
|
|
-import isEmpty from 'lodash/isEmpty';
|
|
|
+import { isEmpty, isNil, isEqual, forOwn } from "lodash-es";
|
|
|
//import loadjscssfile from './loadScript';
|
|
|
-import APILoader from './APILoader';
|
|
|
+import APILoader from "./APILoader";
|
|
|
|
|
|
var xdebug = console.log;
|
|
|
// const xdebug = () => {};
|
|
|
|
|
|
export var loadApi = function loadApi() {
|
|
|
- var key = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '0325e3d6d69cd56de4980b4f28906fd8';
|
|
|
+ var key = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "0325e3d6d69cd56de4980b4f28906fd8";
|
|
|
|
|
|
return new APILoader({
|
|
|
key: key,
|
|
|
useAMapUI: true,
|
|
|
- version: '1.4.7',
|
|
|
- protocol: 'https'
|
|
|
+ version: "1.4.7",
|
|
|
+ protocol: "https"
|
|
|
}).load();
|
|
|
};
|
|
|
export var loadMap = function loadMap(key) {
|
|
@@ -31,10 +28,10 @@ export var loadMap = function loadMap(key) {
|
|
|
if (window.AMap) {
|
|
|
resolve(window.AMap);
|
|
|
} else {
|
|
|
- reject(new Error('window.AMap不存在!'));
|
|
|
+ reject(new Error("window.AMap不存在!"));
|
|
|
}
|
|
|
}).catch(function (error) {
|
|
|
- reject(new Error('加载地图错误!' + error.message));
|
|
|
+ reject(new Error("加载地图错误!" + error.message));
|
|
|
});
|
|
|
});
|
|
|
};
|
|
@@ -80,7 +77,7 @@ export var loadPlugin = function loadPlugin(name) {
|
|
|
});
|
|
|
//是否有加载失败的情况,如果加载失败,怎么reject?
|
|
|
} else {
|
|
|
- reject(new Error('地图还未加载!'));
|
|
|
+ reject(new Error("地图还未加载!"));
|
|
|
}
|
|
|
});
|
|
|
};
|
|
@@ -89,11 +86,11 @@ export var loadPlugin = function loadPlugin(name) {
|
|
|
// 工具方法
|
|
|
////////////////////////////////////////////////////////////
|
|
|
var commonUpdate = function commonUpdate(entity, newOptions, newEvents, oldOptions, oldEvents, operators) {
|
|
|
- var __func__ = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : 'commonUpdate';
|
|
|
+ var __func__ = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : "commonUpdate";
|
|
|
|
|
|
// const __func__ = 'commonUpdate';
|
|
|
if (!entity) {
|
|
|
- xdebug(__func__, 'fail! no entity!');
|
|
|
+ xdebug(__func__, "fail! no entity!");
|
|
|
return false;
|
|
|
}
|
|
|
|
|
@@ -165,11 +162,11 @@ var commonUpdate = function commonUpdate(entity, newOptions, newEvents, oldOptio
|
|
|
func(value);
|
|
|
} else {
|
|
|
// ignore properties can not set.
|
|
|
- xdebug(__func__, 'warning! no setter! can not update ' + key);
|
|
|
+ xdebug(__func__, "warning! no setter! can not update " + key);
|
|
|
}
|
|
|
} else {
|
|
|
// key removed, not support!
|
|
|
- xdebug(__func__, 'warning! remove prop not support! key=' + key);
|
|
|
+ xdebug(__func__, "warning! remove prop not support! key=" + key);
|
|
|
}
|
|
|
});
|
|
|
forOwn(events, function (value, key) {
|
|
@@ -182,7 +179,7 @@ var commonUpdate = function commonUpdate(entity, newOptions, newEvents, oldOptio
|
|
|
}
|
|
|
});
|
|
|
if (!isEmpty(props) || !isEmpty(events)) {
|
|
|
- xdebug(__func__, 'update:', entity, props, events
|
|
|
+ xdebug(__func__, "update:", entity, props, events
|
|
|
// newOptions,
|
|
|
// newEvents,
|
|
|
// oldOptions,
|
|
@@ -196,21 +193,21 @@ var commonUpdate = function commonUpdate(entity, newOptions, newEvents, oldOptio
|
|
|
// Map
|
|
|
////////////////////////////////////////////////////////////
|
|
|
export var createMap = function createMap(dom, options, events) {
|
|
|
- var __func__ = 'createMap';
|
|
|
+ var __func__ = "createMap";
|
|
|
if (!window.AMap) {
|
|
|
- xdebug(__func__, 'fail! no window.AMap!');
|
|
|
+ xdebug(__func__, "fail! no window.AMap!");
|
|
|
return null;
|
|
|
}
|
|
|
if (!dom) {
|
|
|
- xdebug(__func__, 'fail! no dom!');
|
|
|
+ xdebug(__func__, "fail! no dom!");
|
|
|
return null;
|
|
|
}
|
|
|
var map = new window.AMap.Map(dom, _extends({}, options || {}));
|
|
|
forOwn(events, function (value, key) {
|
|
|
- xdebug(__func__, 'event on ' + key);
|
|
|
+ xdebug(__func__, "event on " + key);
|
|
|
map.on(key, value);
|
|
|
});
|
|
|
- xdebug(__func__, 'ok!');
|
|
|
+ xdebug(__func__, "ok!");
|
|
|
return map;
|
|
|
};
|
|
|
|
|
@@ -279,7 +276,7 @@ export var updateMap = function updateMap(map, newOptions, newEvents, oldOptions
|
|
|
skyColor: null,
|
|
|
preloadMode: null
|
|
|
};
|
|
|
- return commonUpdate(map, newOptions, newEvents, oldOptions, oldEvents, operators, 'updateMap');
|
|
|
+ return commonUpdate(map, newOptions, newEvents, oldOptions, oldEvents, operators, "updateMap");
|
|
|
};
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
@@ -293,17 +290,17 @@ export var updateMap = function updateMap(map, newOptions, newEvents, oldOptions
|
|
|
* @param {*} events
|
|
|
*/
|
|
|
export var createMarker = function createMarker(options, events) {
|
|
|
- var __func__ = 'createMarker';
|
|
|
+ var __func__ = "createMarker";
|
|
|
if (!window.AMap) {
|
|
|
- xdebug(__func__, 'fail! no window.AMap!');
|
|
|
+ xdebug(__func__, "fail! no window.AMap!");
|
|
|
return null;
|
|
|
}
|
|
|
if (!options) {
|
|
|
- xdebug(__func__, 'fail! no options!');
|
|
|
+ xdebug(__func__, "fail! no options!");
|
|
|
return null;
|
|
|
}
|
|
|
if (!options.map) {
|
|
|
- xdebug(__func__, 'fail! no options.map!');
|
|
|
+ xdebug(__func__, "fail! no options.map!");
|
|
|
return null;
|
|
|
}
|
|
|
// let marker = new AMap.Marker({
|
|
@@ -315,7 +312,7 @@ export var createMarker = function createMarker(options, events) {
|
|
|
forOwn(events, function (value, key) {
|
|
|
entity.on(key, value);
|
|
|
});
|
|
|
- xdebug(__func__, 'ok!');
|
|
|
+ xdebug(__func__, "ok!");
|
|
|
return entity;
|
|
|
};
|
|
|
|
|
@@ -378,7 +375,7 @@ export var updateMarker = function updateMarker(entity, newOptions, newEvents, o
|
|
|
}
|
|
|
};
|
|
|
|
|
|
- return commonUpdate(entity, newOptions, newEvents, oldOptions, oldEvents, operators, 'updateMarker');
|
|
|
+ return commonUpdate(entity, newOptions, newEvents, oldOptions, oldEvents, operators, "updateMarker");
|
|
|
};
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
@@ -392,21 +389,21 @@ export var updateMarker = function updateMarker(entity, newOptions, newEvents, o
|
|
|
* @param {*} events
|
|
|
*/
|
|
|
var createTraffic = function createTraffic(options, events) {
|
|
|
- var __func__ = 'createTraffic';
|
|
|
+ var __func__ = "createTraffic";
|
|
|
if (!window.AMap || !options || !options.map) {
|
|
|
- xdebug(__func__, 'fail! parameters!', 'window.AMap:' + !!window.AMap, 'options:' + !!options, 'options.map:' + !!(options && options.map));
|
|
|
+ xdebug(__func__, "fail! parameters!", "window.AMap:" + !!window.AMap, "options:" + !!options, "options.map:" + !!(options && options.map));
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
var map = options.map,
|
|
|
- restOpts = _objectWithoutProperties(options, ['map']);
|
|
|
+ restOpts = _objectWithoutProperties(options, ["map"]);
|
|
|
|
|
|
var entity = new window.AMap.TileLayer.Traffic(restOpts);
|
|
|
forOwn(events, function (value, key) {
|
|
|
entity.on(key, value);
|
|
|
});
|
|
|
entity.setMap(map);
|
|
|
- xdebug(__func__, 'ok!');
|
|
|
+ xdebug(__func__, "ok!");
|
|
|
return entity;
|
|
|
};
|
|
|
|
|
@@ -431,7 +428,7 @@ export var updateTraffic = function updateTraffic(entity, newOptions, newEvents,
|
|
|
} // not in options.
|
|
|
};
|
|
|
|
|
|
- return commonUpdate(entity, newOptions, newEvents, oldOptions, oldEvents, operators, 'updateTraffic');
|
|
|
+ return commonUpdate(entity, newOptions, newEvents, oldOptions, oldEvents, operators, "updateTraffic");
|
|
|
};
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
@@ -445,23 +442,25 @@ export var updateTraffic = function updateTraffic(entity, newOptions, newEvents,
|
|
|
* @param {*} events
|
|
|
*/
|
|
|
var createMassMarks = function createMassMarks(options, events) {
|
|
|
- var __func__ = 'createMassMarks';
|
|
|
+ var __func__ = "createMassMarks";
|
|
|
if (!window.AMap || !options || !options.map) {
|
|
|
- xdebug(__func__, 'fail! parameters!', 'window.AMap:' + !!window.AMap, 'options:' + !!options, 'options.map:' + !!(options && options.map));
|
|
|
+ xdebug(__func__, "fail! parameters!", "window.AMap:" + !!window.AMap, "options:" + !!options, "options.map:" + !!(options && options.map));
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
var map = options.map,
|
|
|
data = options.data,
|
|
|
style = options.style,
|
|
|
- restOpts = _objectWithoutProperties(options, ['map', 'data', 'style']);
|
|
|
+ restOpts = _objectWithoutProperties(options, ["map", "data", "style"]);
|
|
|
|
|
|
- var entity = new window.AMap.MassMarks(data || [], _extends({}, restOpts, { style: style || [] }));
|
|
|
+ var entity = new window.AMap.MassMarks(data || [], _extends({}, restOpts, {
|
|
|
+ style: style || []
|
|
|
+ }));
|
|
|
forOwn(events, function (value, key) {
|
|
|
entity.on(key, value);
|
|
|
});
|
|
|
entity.setMap(map);
|
|
|
- xdebug(__func__, 'ok!', map, 'layers:', map.getLayers());
|
|
|
+ xdebug(__func__, "ok!", map, "layers:", map.getLayers());
|
|
|
return entity;
|
|
|
};
|
|
|
|
|
@@ -481,16 +480,16 @@ export var updateMassMarks = function updateMassMarks(entity, newOptions, newEve
|
|
|
return entity.setStyle(v);
|
|
|
},
|
|
|
map: function map(v) {
|
|
|
- xdebug('updateMassMarks', 'setMap', v, 'layers:', v && v.getLayers());
|
|
|
+ xdebug("updateMassMarks", "setMap", v, "layers:", v && v.getLayers());
|
|
|
entity.setMap(v);
|
|
|
},
|
|
|
data: function data(v) {
|
|
|
entity.setData(v);
|
|
|
}
|
|
|
};
|
|
|
- xdebug('updateMassMarks', 'mapOld:', oldOptions && oldOptions.map && oldOptions.map.getLayers(), 'mapNew:', newOptions && newOptions.map && newOptions.map.getLayers());
|
|
|
+ xdebug("updateMassMarks", "mapOld:", oldOptions && oldOptions.map && oldOptions.map.getLayers(), "mapNew:", newOptions && newOptions.map && newOptions.map.getLayers());
|
|
|
|
|
|
- return commonUpdate(entity, newOptions, newEvents, oldOptions, oldEvents, operators, 'updateMassMarks');
|
|
|
+ return commonUpdate(entity, newOptions, newEvents, oldOptions, oldEvents, operators, "updateMassMarks");
|
|
|
};
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
@@ -504,16 +503,16 @@ export var updateMassMarks = function updateMassMarks(entity, newOptions, newEve
|
|
|
* @param {*} events
|
|
|
*/
|
|
|
export var createPolygon = function createPolygon(options, events) {
|
|
|
- var __func__ = 'createPolygon';
|
|
|
+ var __func__ = "createPolygon";
|
|
|
if (!window.AMap || !options || !options.map) {
|
|
|
- xdebug(__func__, 'fail! parameters!', 'window.AMap:' + !!window.AMap, 'options:' + !!options, 'options.map:' + !!(options && options.map));
|
|
|
+ xdebug(__func__, "fail! parameters!", "window.AMap:" + !!window.AMap, "options:" + !!options, "options.map:" + !!(options && options.map));
|
|
|
return null;
|
|
|
}
|
|
|
var entity = new window.AMap.Polygon(options);
|
|
|
forOwn(events, function (value, key) {
|
|
|
entity.on(key, value);
|
|
|
});
|
|
|
- xdebug(__func__, 'ok!');
|
|
|
+ xdebug(__func__, "ok!");
|
|
|
return entity;
|
|
|
};
|
|
|
|
|
@@ -546,7 +545,7 @@ export var updatePolygon = function updatePolygon(entity, newOptions, newEvents,
|
|
|
}
|
|
|
};
|
|
|
|
|
|
- return commonUpdate(entity, newOptions, newEvents, oldOptions, oldEvents, operators, 'updatePolygon');
|
|
|
+ return commonUpdate(entity, newOptions, newEvents, oldOptions, oldEvents, operators, "updatePolygon");
|
|
|
};
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
@@ -560,16 +559,16 @@ export var updatePolygon = function updatePolygon(entity, newOptions, newEvents,
|
|
|
* @param {*} events
|
|
|
*/
|
|
|
export var createCircle = function createCircle(options, events) {
|
|
|
- var __func__ = 'createCircle';
|
|
|
+ var __func__ = "createCircle";
|
|
|
if (!window.AMap || !options || !options.map) {
|
|
|
- xdebug(__func__, 'fail! parameters!', 'window.AMap:' + !!window.AMap, 'options:' + !!options, 'options.map:' + !!(options && options.map));
|
|
|
+ xdebug(__func__, "fail! parameters!", "window.AMap:" + !!window.AMap, "options:" + !!options, "options.map:" + !!(options && options.map));
|
|
|
return null;
|
|
|
}
|
|
|
var entity = new window.AMap.Circle(options);
|
|
|
forOwn(events, function (value, key) {
|
|
|
entity.on(key, value);
|
|
|
});
|
|
|
- xdebug(__func__, 'ok!');
|
|
|
+ xdebug(__func__, "ok!");
|
|
|
return entity;
|
|
|
};
|
|
|
|
|
@@ -604,7 +603,7 @@ export var updateCircle = function updateCircle(entity, newOptions, newEvents, o
|
|
|
}
|
|
|
};
|
|
|
|
|
|
- return commonUpdate(entity, newOptions, newEvents, oldOptions, oldEvents, operators, 'updateCircle');
|
|
|
+ return commonUpdate(entity, newOptions, newEvents, oldOptions, oldEvents, operators, "updateCircle");
|
|
|
};
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
@@ -618,16 +617,16 @@ export var updateCircle = function updateCircle(entity, newOptions, newEvents, o
|
|
|
* @param {*} events
|
|
|
*/
|
|
|
export var createPolyline = function createPolyline(options, events) {
|
|
|
- var __func__ = 'createPolyline';
|
|
|
+ var __func__ = "createPolyline";
|
|
|
if (!window.AMap || !options || !options.map) {
|
|
|
- xdebug(__func__, 'fail! parameters!', 'window.AMap:' + !!window.AMap, 'options:' + !!options, 'options.map:' + !!(options && options.map));
|
|
|
+ xdebug(__func__, "fail! parameters!", "window.AMap:" + !!window.AMap, "options:" + !!options, "options.map:" + !!(options && options.map));
|
|
|
return null;
|
|
|
}
|
|
|
var entity = new window.AMap.Polyline(options);
|
|
|
forOwn(events, function (value, key) {
|
|
|
entity.on(key, value);
|
|
|
});
|
|
|
- xdebug(__func__, 'ok!');
|
|
|
+ xdebug(__func__, "ok!");
|
|
|
return entity;
|
|
|
};
|
|
|
|
|
@@ -665,7 +664,7 @@ export var updatePolyline = function updatePolyline(entity, newOptions, newEvent
|
|
|
}
|
|
|
};
|
|
|
|
|
|
- return commonUpdate(entity, newOptions, newEvents, oldOptions, oldEvents, operators, 'updatePolyline');
|
|
|
+ return commonUpdate(entity, newOptions, newEvents, oldOptions, oldEvents, operators, "updatePolyline");
|
|
|
};
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
@@ -679,16 +678,16 @@ export var updatePolyline = function updatePolyline(entity, newOptions, newEvent
|
|
|
* @param {*} events
|
|
|
*/
|
|
|
export var createInfoWindow = function createInfoWindow(options, events) {
|
|
|
- var __func__ = 'createInfoWindow';
|
|
|
+ var __func__ = "createInfoWindow";
|
|
|
if (!window.AMap || !options || !options.map) {
|
|
|
- xdebug(__func__, 'fail! parameters!', 'window.AMap:' + !!window.AMap, 'options:' + !!options, 'options.map:' + !!(options && options.map));
|
|
|
+ xdebug(__func__, "fail! parameters!", "window.AMap:" + !!window.AMap, "options:" + !!options, "options.map:" + !!(options && options.map));
|
|
|
return null;
|
|
|
}
|
|
|
var entity = new window.AMap.InfoWindow(options);
|
|
|
forOwn(events, function (value, key) {
|
|
|
entity.on(key, value);
|
|
|
});
|
|
|
- xdebug(__func__, 'ok!');
|
|
|
+ xdebug(__func__, "ok!");
|
|
|
return entity;
|
|
|
};
|
|
|
|
|
@@ -710,5 +709,47 @@ export var updateInfoWindow = function updateInfoWindow(entity, newOptions, newE
|
|
|
showShadow: null
|
|
|
};
|
|
|
|
|
|
- return commonUpdate(entity, newOptions, newEvents, oldOptions, oldEvents, operators, 'updateInfoWindow');
|
|
|
+ return commonUpdate(entity, newOptions, newEvents, oldOptions, oldEvents, operators, "updateInfoWindow");
|
|
|
+};
|
|
|
+
|
|
|
+////////////////////////////////////////////////////////////
|
|
|
+// Geolocation
|
|
|
+////////////////////////////////////////////////////////////
|
|
|
+
|
|
|
+export var getLocation = function getLocation(options, events) {
|
|
|
+ xdebug("准备获取地理位置");
|
|
|
+ /***************************************
|
|
|
+ 由于Chrome、IOS10等已不再支持非安全域的浏览器定位请求,为保证定位成功率和精度,请尽快升级您的站点到HTTPS。
|
|
|
+ ***************************************/
|
|
|
+ var map = options.map;
|
|
|
+ if (!map) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ var geolocation = void 0;
|
|
|
+
|
|
|
+ var defaultOptions = {
|
|
|
+ enableHighAccuracy: true, //是否使用高精度定位,默认:true
|
|
|
+ timeout: 10000, //超过10秒后停止定位,默认:无穷大
|
|
|
+ buttonOffset: new window.AMap.Pixel(10, 20), //定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
|
|
|
+ zoomToAccuracy: true, //定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false
|
|
|
+ buttonPosition: "RB"
|
|
|
+ };
|
|
|
+
|
|
|
+ var opts = _extends({}, defaultOptions, options);
|
|
|
+ xdebug("准备加载定位插件");
|
|
|
+
|
|
|
+ map.plugin("AMap.Geolocation", function () {
|
|
|
+ xdebug("加载定位插件成功");
|
|
|
+
|
|
|
+ geolocation = new window.AMap.Geolocation(opts);
|
|
|
+ map.addControl(geolocation);
|
|
|
+ geolocation.getCurrentPosition();
|
|
|
+ if (events.onComplete) {
|
|
|
+ window.AMap.event.addListener(geolocation, "complete", events.onComplete); //返回定位信息
|
|
|
+ }
|
|
|
+ if (events.onError) {
|
|
|
+ window.AMap.event.addListener(geolocation, "error", events.onError); //返回定位出错信息
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return true;
|
|
|
};
|