map.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. import common from '../../utils/util';
  2. import main from '../../utils/main';
  3. import constant from '../../utils/constant';
  4. const app = getApp();
  5. var mapSetting = {
  6. latitude: 31.232116445550176,
  7. longitude: 121.47281159374998,
  8. scale: 9,
  9. };
  10. Page({
  11. data: {
  12. latitude: mapSetting.latitude,
  13. longitude: mapSetting.longitude,
  14. scale: mapSetting.scale,
  15. markers: [],
  16. SchoolType: constant.arrSchoolType,
  17. },
  18. onReady: function (e) {
  19. this.mapCtx = wx.createMapContext('myMap');
  20. },
  21. onLoad: function (options) {
  22. var that = this;
  23. that.setData({
  24. Containnerheight: main.getWindowHeight(),
  25. IsShow:app.globalData.userInfo.IsShow,
  26. PageType: options.PageType,
  27. SchoolTypeIndex: -1,
  28. Options: options,
  29. School: [],
  30. });
  31. var title = "";
  32. if (options.PageType == 1) {
  33. title = "校址·" + options.SchoolShortName;
  34. for (var j = 0; j < that.data.SchoolType.length; j++) {
  35. that.data.SchoolType[j].CSS = "";
  36. }
  37. that.setData({
  38. SchoolType: that.data.SchoolType,
  39. SchoolName:options.SchoolShortName,
  40. });
  41. } else if (options.PageType == 2) {
  42. title = "地图·" + options.DistrictName;
  43. that.setData({
  44. SchoolTypeIndex: 0,
  45. DistrictName:options.DistrictName,
  46. });
  47. } else if (options.PageType == 3) {
  48. for (var j = 0; j < that.data.SchoolType.length; j++) {
  49. that.data.SchoolType[j].CSS = "Selected";
  50. }
  51. that.setData({
  52. SchoolType: that.data.SchoolType
  53. });
  54. title = "高中校址地图";
  55. that.setData({
  56. SchoolTypeIndex: 0,
  57. });
  58. }
  59. wx.setNavigationBarTitle({
  60. title: title,
  61. });
  62. that.setMap();
  63. },
  64. mapChange: function (e) {
  65. if (e.type == "end") {
  66. mapSetting = {
  67. latitude: e.detail.centerLocation.latitude,
  68. longitude: e.detail.centerLocation.longitude,
  69. scale: e.detail.scale,
  70. };
  71. }
  72. },
  73. onSelect: function (event) {
  74. var that = this;
  75. main.onSelect(that, event, function (obj, e, result) {
  76. if (e.currentTarget.dataset.object == "SchoolType") {
  77. var selectIndex = e.currentTarget.dataset.index;
  78. var selectText = that.data.SchoolType[selectIndex].Name;
  79. var isSelect = that.data.SchoolType[selectIndex].CSS == "Selected";
  80. that.setData({
  81. SchoolType: that.data.SchoolType,
  82. latitude: mapSetting.latitude,
  83. longitude: mapSetting.longitude,
  84. scale: mapSetting.scale,
  85. });
  86. that.setMap();
  87. }
  88. });
  89. },
  90. setMap: function () {
  91. var that = this;
  92. var markers = [];
  93. that.getCurrentPosition(that, markers, function (markers) {
  94. that.getCurrentSchoolPosition(that, markers, function (markers) {
  95. that.getSchoolPosition(that, markers, function (markers) {
  96. that.setData({
  97. markers: markers,
  98. });
  99. });
  100. });
  101. });
  102. },
  103. getCurrentPosition: function (obj, markers, callback) {
  104. var that = this;
  105. wx.getLocation({
  106. type: 'wgs84',
  107. success(res) {
  108. var mypos = {
  109. id: 9999,
  110. latitude: res.latitude,
  111. longitude: res.longitude,
  112. zIndex: 10,
  113. callout: {
  114. content: "我的位置",
  115. color: '#EB5C28',
  116. fontSize: 14,
  117. borderWidth: 2,
  118. borderRadius: 10,
  119. borderColor: '#EB5C28',
  120. padding: 5,
  121. display: 'ALWAYS',
  122. textAlign: 'center'
  123. },
  124. }
  125. markers.push(mypos);
  126. callback(markers);
  127. },
  128. fail(res) {
  129. callback(markers);
  130. }
  131. });
  132. },
  133. getCurrentSchoolPosition: function (obj, markers, callback) {
  134. if (obj.data.Options.ID) {
  135. var arr1 = obj.data.Options.Coordinates.split(",");
  136. obj.setData({
  137. Address: obj.data.Options.Address,
  138. });
  139. var pos = {
  140. id: Number(obj.data.Options.ID),
  141. latitude: arr1[0],
  142. longitude: arr1[1],
  143. zIndex: 9,
  144. callout: {
  145. content: obj.data.Options.Name,
  146. color: '#EB5C28',
  147. fontSize: 14,
  148. borderWidth: 2,
  149. borderRadius: 10,
  150. borderColor: '#EB5C28',
  151. padding: 5,
  152. display: 'ALWAYS',
  153. textAlign: 'center'
  154. },
  155. }
  156. markers.push(pos);
  157. }
  158. callback(markers);
  159. },
  160. getDistrict: function (obj, markers, callback) {
  161. var that=this;
  162. if (obj.data.School.length > 0)
  163. callback(markers);
  164. else {
  165. var arr=[];
  166. if (that.data.PageType == 3) {
  167. main.getCollect(function(data){
  168. arr=getSchool(data["收藏的高中"]);
  169. obj.setData({
  170. School: arr,
  171. });
  172. callback(markers);
  173. });
  174. } else {
  175. wx.showLoading();
  176. main.getData("GetMPSSchool?SchoolType1=高中&DistrictID=" + Number(obj.data.Options.DistrictID), function (data) {
  177. arr=getSchool(data);
  178. obj.setData({
  179. School: arr,
  180. });
  181. callback(markers);
  182. setTimeout(function () {
  183. wx.hideLoading();
  184. }, 800);
  185. });
  186. }
  187. }
  188. function getSchool(data){
  189. var arr = [];
  190. for (var i = 0; i < data.length; i++) {
  191. var item = data[i];
  192. var school = {};
  193. school.ID = item.ID;
  194. school.Name = item.SchoolShortName;
  195. school.IsSchoolType2Short = false;
  196. if (item.SchoolType2Short == "市重点")
  197. school.IsSchoolType2Short = true;
  198. if (item.Coordinates) {
  199. var arr1 = item.Coordinates.split(",");
  200. school.Latitude = arr1[0];
  201. school.Longitude = arr1[1];
  202. if (item.SchoolType2Short == "区重点(特色)")
  203. item.SchoolType2Short = item.SchoolType2Short.substr(0, 3);
  204. school.SchoolType2Short = item.SchoolType2Short;
  205. arr.push(school);
  206. }
  207. }
  208. return arr;
  209. }
  210. },
  211. getSchoolPosition: function (obj, markers, callback) {
  212. var that = this;
  213. obj.getDistrict(obj, markers, function (markers) {
  214. for (var i = 0; i < obj.data.School.length; i++) {
  215. var item = obj.data.School[i];
  216. item.IsSchoolType2Short = false;
  217. for (var j = 0; j < that.data.SchoolType.length; j++) {
  218. if (that.data.SchoolType[j].CSS == "Selected" &&
  219. item.SchoolType2Short == that.data.SchoolType[j].Name)
  220. item.IsSchoolType2Short = true;
  221. }
  222. if (item.IsSchoolType2Short &&
  223. (!obj.data.Options.ID || (obj.data.Options.ID && obj.data.Options.ID != item.ID))) {
  224. var pos = {
  225. id: item.ID,
  226. latitude: item.Latitude,
  227. longitude: item.Longitude,
  228. alpha: 0.8,
  229. callout: {
  230. content: item.Name,
  231. color: '#4D4D4D',
  232. fontSize: 14,
  233. borderWidth: 2,
  234. borderRadius: 10,
  235. borderColor: '#4D4D4D',
  236. bgColor: '#fff',
  237. padding: 5,
  238. display: 'ALWAYS',
  239. textAlign: 'center'
  240. },
  241. }
  242. markers.push(pos);
  243. }
  244. }
  245. callback(markers);
  246. });
  247. },
  248. goto: function (e) {
  249. main.goto(e);
  250. },
  251. copyUrl:function(e){
  252. var url=e.currentTarget.dataset.url;
  253. wx.setClipboardData({
  254. data: url,
  255. success (res) {
  256. wx.showToast({
  257. title: '已复制',
  258. });
  259. }
  260. });
  261. },
  262. onMarkerTap(e) {
  263. //console.log('@@ markertap', e)
  264. this.gotoSchool(e);
  265. },
  266. onCalloutTap(e) {
  267. //console.log('@@ onCalloutTap', e)
  268. this.gotoSchool(e);
  269. },
  270. onLabelTap(e) {
  271. //console.log('@@ labletap', e)
  272. this.gotoSchool(e);
  273. },
  274. gotoSchool(e){
  275. var id=e.detail.markerId;
  276. if (id<9999)
  277. main.goto({currentTarget:{dataset:{url:"school?ID="+id}}});
  278. },
  279. onShareTimeline: function () {
  280. return this.onShareAppMessage();
  281. },
  282. onShareAppMessage: function () {
  283. var that=this;
  284. var title="上海中招";
  285. var path=app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID;
  286. if (that.data.PageType == 1) {
  287. title+="校址·"+that.data.SchoolName;
  288. path+='&type=school&ID='+that.data.Options.ID;
  289. }
  290. else if (that.data.PageType == 2) {
  291. title+="学校地图·"+that.data.DistrictName;
  292. path+='&type=district&ID='+that.data.DistriceID+'&Name='+that.data.DistrictName;
  293. }
  294. else{
  295. title=app.globalData.ShareTitle;
  296. }
  297. debugger;
  298. return {
  299. title: title,
  300. path: path
  301. }
  302. },
  303. })