map.js 8.3 KB

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