截流自动化的商城平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

likeMap.js 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. layui.define(["jquery", "form"], function (exports) {
  2. var markersArray = [];
  3. var map = new BMap.Map("map");
  4. var geoc = new BMap.Geocoder();
  5. var obj = {
  6. /**
  7. * 初始化
  8. */
  9. init: function (longitude, latitude) {
  10. new BMap.Geolocation();
  11. var local = new BMap.LocalSearch(map, { renderOptions: {map: map} });
  12. var point = new BMap.Point(longitude, latitude)
  13. map.centerAndZoom(point, 16);
  14. map.addEventListener("click", obj.showInfo);
  15. obj.searchMap(local);
  16. },
  17. /**
  18. * 清除标识
  19. */
  20. clearOverlays: function() {
  21. if (markersArray) {
  22. for (i in markersArray) {
  23. map.removeOverlay(markersArray[i])
  24. }
  25. }
  26. },
  27. /**
  28. * 地图上标注
  29. */
  30. addMarker: function (point) {
  31. var marker = new BMap.Marker(point);
  32. markersArray.push(marker);
  33. obj.clearOverlays();
  34. map.addOverlay(marker);
  35. },
  36. /**
  37. * 点击地图事件处理
  38. */
  39. showInfo: function (e) {
  40. $("input[name='longitude']").val(e.point.lng);
  41. $("input[name='latitude']").val(e.point.lat);
  42. geoc.getLocation(e.point, function (rs) {
  43. var addComp = rs.addressComponents;
  44. var info_address = addComp.street + addComp.streetNumber;
  45. if(info_address !== ''){
  46. $("input[name='address']").val(info_address);
  47. }
  48. });
  49. obj.addMarker(e.point);
  50. },
  51. /**
  52. * 搜索地图
  53. * @param local
  54. */
  55. searchMap: function (local) {
  56. $(document).on('click', '#searchMap', function() {
  57. var province_id = $("#province");
  58. var city_id = $("#city");
  59. var district_id = $("#district");
  60. var shop_address = $("input[name='address']").val();
  61. if(province_id.val() === null){
  62. layer.open({icon:2, time:2000, content:"请选择省份"});
  63. return;
  64. }
  65. if(city_id.val() === null){
  66. layer.open({icon:2, time:2000, content:"请选择市"});
  67. return;
  68. }
  69. if(district_id.val() === null){
  70. layer.open({icon:2,time:2000,content:"请选择镇/区"});
  71. return;
  72. }
  73. var address = province_id.find("option:selected").text() + city_id.find("option:selected").text() + district_id.find("option:selected").text() + shop_address;
  74. //找到最符合的标注a点坐标
  75. local.setSearchCompleteCallback(function (searchResult) {
  76. var poi = searchResult.getPoi(0);
  77. $("input[name='longitude']").val(poi.point.lng);
  78. $("input[name='latitude']").val(poi.point.lat); //获取经度和纬度,将结果显示在文本框中
  79. map.centerAndZoom(poi.point, 13);
  80. });
  81. local.search(address);
  82. });
  83. }
  84. };
  85. exports("likeMap", obj);
  86. });