maps.util.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /* *
  2. * IFrame地图与外部操作桥接
  3. * @author BOONYACHENGDU@GMAIL.COM
  4. */
  5. (function() {
  6. window.MapsUtil = {};
  7. MapsUtil = {
  8. BASEPATH : null,
  9. MAP_OBJECT : null,
  10. MAP_CONSTANT : null,
  11. /**
  12. * 初始化地图
  13. */
  14. initMap : function(map_object,mapType,basePath) {
  15. this.BASEPATH = basePath;
  16. this.MAP_OBJECT = map_object;
  17. this.MAP_CONSTANT = map_object.CONSTANT;
  18. this.MAP_CONSTANT.CURRENT_MAP_TYPE = mapType || this.MAP_CONSTANT.CURRENT_MAP_TYPE;
  19. this.MAP_OBJECT.initOpenLayersMap();
  20. },
  21. initHistoricalTrackMap : function(map_object,mapType,basePath) {
  22. this.BASEPATH = basePath;
  23. this.MAP_OBJECT = map_object;
  24. this.MAP_CONSTANT = map_object.CONSTANT;
  25. this.MAP_CONSTANT.CURRENT_MAP_TYPE = mapType || "openStreetMap";
  26. this.MAP_OBJECT.initHistoricalTrackMap();
  27. },
  28. /**
  29. * 获取坐标点Point
  30. */
  31. getPoint : function(lat,lng) {
  32. return this.MAP_OBJECT.getPoint(lat,lng);
  33. },
  34. /**
  35. * 设置地图居中
  36. */
  37. setCenterAndZoom : function(point,zoom) {
  38. this.MAP_OBJECT.setCenterAndZoom(point,zoom);
  39. },
  40. /**
  41. * 点越界处理
  42. */
  43. ifOutBoundsPan : function(point) {
  44. this.MAP_OBJECT.ifOutBoundsPantoCenter(point);
  45. },
  46. /**
  47. * 得到一个图标
  48. */
  49. getIcon : function(imei,iconName,iconType,isOnline) {
  50. var id = imei+(isOnline?"_on":"_off");
  51. var scale = 1;
  52. var basePath = MapsUtil.BASEPATH;
  53. var iconPath = basePath+"/web/resource/images/trackerIcon/point1.png";
  54. if(iconType ==1){
  55. iconPath = basePath+"/web/resource/images/trackerIcon";
  56. iconPath += (isOnline?"/":"/off/")+iconName;
  57. }else if(iconType ==2){
  58. iconPath = basePath+"/web/getImage/trackerIcon/";
  59. iconPath += iconName+(isOnline?"":"?off=true");
  60. scale = 0.5;
  61. }
  62. return this.MAP_OBJECT.getIcon(id,iconPath,scale);
  63. },
  64. /**
  65. * 得到一个标记图标
  66. */
  67. getMarker : function(id,point,icon) {
  68. return this.MAP_OBJECT.getMarker(id,point,icon);
  69. },
  70. /**
  71. * 设置标记图标
  72. */
  73. setMarker : function(id, point, iconStyle) {
  74. return this.MAP_OBJECT.setMarker(id, point, iconStyle);
  75. },
  76. isShowCircle : function(id, isShow) {
  77. id = id + 'circle';
  78. return this.MAP_OBJECT.isShowCircle(id, isShow);
  79. },
  80. getCircle : function(id,point,radius) {
  81. id = id + 'circle';
  82. return this.MAP_OBJECT.getCircle(id,point,radius);
  83. },
  84. setCircle : function(id, point, radius) {
  85. id = id + 'circle';
  86. return this.MAP_OBJECT.setCircle(id, point, radius);
  87. },
  88. removeCircle : function(id) {
  89. id = id + 'circle';
  90. this.MAP_OBJECT.removeCircle(id);
  91. },
  92. /**
  93. * 移除一个标记
  94. */
  95. removeMarker : function(id) {
  96. this.MAP_OBJECT.removeMarke(id);
  97. this.MAP_OBJECT.removeInfoWindow(id);
  98. this.removeCircle(id);
  99. },
  100. /**
  101. * 移除所有标记
  102. */
  103. removeAllMarker : function() {
  104. if(this.MAP_OBJECT != null){
  105. this.MAP_OBJECT.removeAllMarke();
  106. this.MAP_OBJECT.removeAllInfoWindow();
  107. }
  108. },
  109. /**
  110. *
  111. */
  112. getInfoWindow : function(id,point,htmlContent) {
  113. this.MAP_OBJECT.getInfoWindow(id,point,htmlContent);
  114. },
  115. /**
  116. *
  117. */
  118. setInfoWindow : function(id,point,htmlContent) {
  119. this.MAP_OBJECT.setInfoWindow(id,point,htmlContent);
  120. },
  121. /**
  122. *
  123. */
  124. geocoderToAddress : function(lat,lng,callback) {
  125. // this.MAP_OBJECT.getGeocoderAddressByPoint(lat,lng,callback);
  126. MapsUtil.api.getGeocoderAddress(lat,lng,callback);
  127. },
  128. analysisAddress : function(lat,lng,isChina,callback){
  129. if(isChina){
  130. MapsUtil.service.jiupian(lat, lng, function(latlng) {
  131. var jiupian_lat = latlng[0];
  132. var jiupian_lng = latlng[1];
  133. //geocoder解析地址
  134. MapsUtil.geocoderToAddress(jiupian_lat, jiupian_lng, function(address) {
  135. callback(address);
  136. });
  137. });
  138. }else{
  139. //geocoder解析地址
  140. MapsUtil.geocoderToAddress(lat, lng, function(address) {
  141. callback(address);
  142. });
  143. }
  144. },
  145. viewExtent : function(coordinates){
  146. this.MAP_OBJECT.viewExtent(coordinates);
  147. }
  148. };
  149. MapsUtil.service={
  150. getLocalAddress : function(lat, lng, callback){
  151. var mapType = MapsUtil.MAP_CONSTANT.CURRENT_MAP_TYPE;
  152. $.ajax({
  153. url: MapsUtil.BASEPATH+"/web/getLocalAddress.do",
  154. datatype:'json',
  155. type: "Post",
  156. data: "mapType="+mapType+"&lat="+lat+"&lng="+lng,
  157. success: function(jsonData){
  158. //alert(JSON.stringify(jsonData));
  159. if(jsonData.code == 10000){
  160. var address = jsonData.data["address"];
  161. callback(address);
  162. }
  163. }
  164. });
  165. },
  166. saveLocalAddress : function (lat, lng, address){
  167. var mapType = MapsUtil.MAP_CONSTANT.CURRENT_MAP_TYPE;
  168. $.ajax({
  169. url: MapsUtil.BASEPATH+"/web/saveLocalAddress.do",
  170. datatype:'json',
  171. type: "Post",
  172. data: "mapType="+mapType+"&lat="+lat+"&lng="+lng+"&address="+address,
  173. success: function(data){
  174. }
  175. });
  176. },
  177. updateLocalAddress : function (lat, lng, address, callback){
  178. var mapType = MapsUtil.MAP_CONSTANT.CURRENT_MAP_TYPE;
  179. $.ajax({
  180. url: MapsUtil.BASEPATH+"/web/updateLocalAddress.do",
  181. datatype:'json',
  182. type: "Post",
  183. data: "mapType="+mapType+"&lat="+lat+"&lng="+lng+"&address="+address,
  184. success: function(data){
  185. callback(data);
  186. }
  187. });
  188. },
  189. jiupian : function (lat, lng, callback){
  190. var jiupianType = MapsUtil.MAP_CONSTANT.CURRENT_MAP_JIUPIAN;
  191. $.ajax({
  192. url: MapsUtil.BASEPATH+"/web/jiupian.json",
  193. datatype:'json',
  194. type: "Post",
  195. data: "lat="+lat+"&lng="+lng+"&jiupianType="+jiupianType,
  196. success: function(jsonData){
  197. if(jsonData.code == 10000){
  198. var latlng = jsonData.data["latlng"];
  199. callback(latlng);
  200. }
  201. }
  202. });
  203. }
  204. }
  205. MapsUtil.api={
  206. /**
  207. * 反地址解析
  208. */
  209. getGeocoderAddress : function(lat, lng, callback) {
  210. var mapType = MapsUtil.MAP_CONSTANT.CURRENT_MAP_TYPE;
  211. if (mapType == 'googleMap') {
  212. var mapKey = window.parent.googleKey;
  213. //result_type=street_address 表示精确的街道地址。精确的街道地址有可能不存在
  214. var url = 'https://maps.google.com/maps/api/geocode/json?sensor=false'
  215. +'&key='+mapKey+'&latlng='+lat+','+lng;
  216. $.ajax({
  217. type : 'GET',
  218. url : url,
  219. success : function(data) {
  220. //alert("==="+JSON.stringify(data));
  221. if (data["status"] == "OK") {
  222. var address = data["results"][0].formatted_address;
  223. //alert("address=="+address);
  224. callback(address);
  225. }
  226. }
  227. });
  228. } else if (mapType == 'baiduMap') {
  229. // http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding
  230. //var baiduKey = 'dGeD3fnmUH6W1RlVSvEHi2oOmsppaHQ4';
  231. var mapKey = window.parent.baiduKey;
  232. var url = 'http://api.map.baidu.com/geocoder/v2/?output=json&ak='
  233. + mapKey + '&location=' + lat + ',' + lng;
  234. $.ajax({
  235. type : 'GET',
  236. dataType : 'JSONP',
  237. url : url,
  238. success : function(data) {
  239. // alert("==="+JSON.stringify(data));
  240. if (data["status"] == 0) {
  241. var address = data["result"].formatted_address;
  242. //alert("baidu=="+address+",距离:"+data["result"].sematic_description);
  243. callback(address);
  244. }
  245. }
  246. });
  247. } else if (mapType == 'gaodeMap') {
  248. // http://lbs.amap.com/api/webservice/reference/georegeo/#t6
  249. //var gaodeKey = '5b12e9009f9ac5956f00a42947f3f994';
  250. var mapKey = window.parent.gaodeKey;
  251. var url = 'http://restapi.amap.com/v3/geocode/regeo?output=json&key='
  252. + mapKey + '&location=' + lng + ',' + lat;
  253. $.ajax({
  254. url : url,
  255. type : 'get',
  256. success : function(data) {
  257. // alert("==="+JSON.stringify(data));
  258. if (data["status"] == 1) {
  259. var address = data["regeocode"].formatted_address;
  260. callback(address);
  261. }
  262. },
  263. })
  264. } else if (mapType == 'openStreetMap') {
  265. // http://wiki.openstreetmap.org/wiki/Zh-hans:Nominatim
  266. var url = 'https://nominatim.openstreetmap.org/reverse?format=json&lat='
  267. + lat + '&lon=' + lng + '&zoom=18&addressdetails=1';
  268. $.ajax({
  269. url : url,
  270. type : 'get',
  271. success : function(data) {
  272. // alert("openStreetMap==="+JSON.stringify(data));
  273. if (data["display_name"] != "undefined") {
  274. var address = data["display_name"];
  275. callback(address);
  276. }
  277. },
  278. })
  279. }
  280. },
  281. /**
  282. * 地址解析
  283. */
  284. getGeocoderPointByAddress : function(address, callback) {
  285. var GOOGLEKEY = 'AIzaSyDY0kkJiTPVd2U7aTOAwhc9ySH6oHxOIYM';
  286. // http://maps.google.com/maps/api/geocode/json?address=&sensor=false
  287. $.ajax('https://maps.googleapis.com/maps/api/geocode/json?address=${address}&key=${GOOGLEKEY}',{
  288. beforeSend : function() {
  289. window.googleStart = new Date().getTime();
  290. },
  291. success : function(data) {
  292. // $('#google').empty();
  293. // for (var res of data.results) {
  294. // $('#google').append('<li>${res.formatted_address}</li>')
  295. // }
  296. // $('#google').append('<li>took ${new
  297. // Date().getTime() -
  298. // window.googleStart}ms</li>');
  299. }
  300. });
  301. }
  302. }
  303. MapsUtil.tools={
  304. /**
  305. * 画多边形、圆
  306. */
  307. addDraw : function(){
  308. this.MAP_OBJECT.tools.addDraw();
  309. }
  310. }
  311. MapsUtil.event={
  312. /**
  313. * 添加监听事件
  314. */
  315. addListener:function(mapObject,type,htmlContent,callback){
  316. if(type=="click"){//点击标注事件
  317. return this.MAP_OBJECT.event.addListener_singleclick(mapObject,htmlContent);
  318. }else if(type=="mousemove"){
  319. }
  320. },
  321. };
  322. })();