map_address.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. //author 周
  2. // var map, marker, infoWindow, geolocation, geocoder;//地图加载默认参数
  3. mapClass = function(id, lnglat, map_success_back){
  4. /*******************************************************地图加载事件start**********************************************************/
  5. //加载地图,调用浏览器定位服务
  6. _this = this;
  7. this.init(id, lnglat, map_success_back);//初始化函数
  8. };
  9. mapClass.prototype = {
  10. init : function(id, lnglat, map_success_back){
  11. _this.setAttr();
  12. _this.map_callback = map_success_back;
  13. var msp_json = {
  14. zoom: _this.zoom
  15. }
  16. if(lnglat["lat"] != ''|| lnglat["lng"] != '' ){
  17. msp_json["center"] = [lnglat["lng"], lnglat["lat"]]//初始化地图中心点}
  18. }
  19. _this.map = new AMap.Map(id, msp_json);
  20. //加载地图插件
  21. _this.map.plugin('AMap.Geolocation', function() {
  22. _this.geolocation = new AMap.Geolocation({
  23. enableHighAccuracy: false,//是否使用高精度定位,默认:true
  24. timeout: 1, //超过10秒后停止定位,默认:无穷大
  25. buttonOffset: new AMap.Pixel(2.5, 2.5),//定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
  26. zoomToAccuracy: false, //定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false
  27. buttonPosition:'RB',
  28. showMarker: false, //定位成功后在定位到的位置显示点标记,默认:true
  29. });
  30. _this.map.plugin(["AMap.ToolBar"], function() {
  31. _this.map.addControl(new AMap.ToolBar());
  32. });
  33. // _this.geolocation.getCurrentPosition();
  34. _this.marker = new AMap.Marker({
  35. map:_this.map,
  36. bubble:true
  37. })
  38. _this.infoWindow = new AMap.InfoWindow({
  39. content: '',
  40. offset: {x: 0, y: -30}
  41. });
  42. _this.map.plugin('AMap.Geocoder',function(){
  43. _this.geocoder = new AMap.Geocoder({
  44. radius: 500 //范围,默认:500
  45. });
  46. });
  47. AMap.event.addListener(_this.geolocation, 'complete', function(data){
  48. _this.map_change = false;
  49. _this.markerMove(data.position, data.formattedAddress, data.addressComponent);
  50. });//返回定位信息
  51. AMap.event.addListener(_this.geolocation, 'error', function(){}); //返回定位出错信息
  52. });
  53. _this.map.on('click',function(e){
  54. if(_this.map_click){
  55. _this.map_change = false;
  56. _this.geocoder.getAddress(e.lnglat,function(status,result){
  57. if(status == 'complete'){
  58. _this.markerMove(e.lnglat, result.regeocode.formattedAddress,result.regeocode.addressComponent);
  59. }else{
  60. _this.markerMove(e.lnglat, "定位失败");
  61. }
  62. })
  63. }else{
  64. return false;
  65. }
  66. })
  67. if(_this.map_callback != undefined){
  68. _this.map_callback(_this);
  69. }
  70. },
  71. getCurrentPosition : function (){
  72. _this.geolocation.getCurrentPosition();
  73. },
  74. onComplete : function(data){
  75. // console.log(data);
  76. // if (_this.location) {
  77. _this.map_change = false;
  78. _this.markerMove(data.position, data.formattedAddress, data.addressComponent);
  79. // }else{
  80. // _this.location = true;
  81. // if(_this.map_callback != undefined){
  82. // _this.map_callback(_this);
  83. // }
  84. //
  85. // }
  86. },
  87. onError : function(){
  88. },
  89. markerMove : function(position, address, data){
  90. var address_detail = address;
  91. address_detail = address_detail.replace(data.province,'');
  92. address_detail = address_detail.replace(data.city,'');
  93. address_detail = address_detail.replace(data.district,'');
  94. address_detail = address_detail.replace(data.township,'');
  95. _this.marker.setPosition(position);
  96. _this.infoWindow.setContent("<p>当前位置:<span class='nc-text-color'>" + address + "</span>");
  97. // _this.infoWindow.setPosition("<p>当前位置:<span style='color:#0689e1;'>" + address + "</span>");
  98. // _this.infoWindow.setPosition();
  99. _this.infoWindow.open(_this.map, position);
  100. _this.map.setZoomAndCenter(20, _this.marker.getPosition());//设置地图放大级别及居中
  101. _this.address.province_name = data.province;
  102. if(data.city == ""){
  103. _this.address.city_name = data.province;
  104. }else{
  105. _this.address.city_name = data.city;
  106. }
  107. _this.address.district_name = data.district;
  108. _this.address.township_name = data.township;
  109. _this.address.area = _this.address.province_name+","+_this.address.city_name+","+_this.address.district_name+","+_this.address.township_name;
  110. _this.address.longitude = position.lng;
  111. _this.address.latitude = position.lat;
  112. _this.address.address = address_detail;
  113. if(!_this.map_change){
  114. console.log( _this.address);
  115. mapChangeCallBack();
  116. }else{
  117. selectCallBack();
  118. }
  119. },
  120. mapChange : function(address){
  121. if (_this.map_change) {
  122. var province_name = _this.address.province > 0 ? _this.address.province_name : '';
  123. var city_name = _this.address.province > 0 && _this.address.city > 0 ? _this.address.city_name : '';
  124. var districts_name = _this.address.province > 0 && _this.address.city > 0 && _this.address.district > 0 ? _this.address.district_name : '';
  125. var township_name = _this.address.province > 0 && _this.address.city > 0 && _this.address.district > 0 && _this.address.township > 0 ? _this.address.township_name : '';
  126. var address_detail = _this.address.province > 0 && _this.address.city > 0 && _this.address.district > 0 && _this.address.township > 0 ? _this.address.address : '';
  127. if(!address){
  128. address = province_name + city_name + districts_name + township_name + address_detail;
  129. }
  130. //console.log(_this.address);
  131. _this.geocoder.getLocation(address, function (status, result) {
  132. if (status === 'complete' && result.info === 'OK') {
  133. _this.markerMove(result.geocodes[0].location, result.geocodes[0].formattedAddress, result.geocodes[0].addressComponent);
  134. }
  135. });
  136. }
  137. },
  138. setAttr : function(){
  139. _this.map = "";
  140. _this.marker = "";
  141. _this.infoWindow = "";
  142. _this.geolocation = "";
  143. _this.geocoder = "";
  144. _this.location = false;
  145. _this.map_change = true;
  146. _this.map_click = true;
  147. _this.zoom = 15;
  148. _this.address = {
  149. province : "",
  150. province_name : "",
  151. city : "",
  152. city_name : "",
  153. district : "",
  154. district_name : "",
  155. township : "",
  156. township_name : "",
  157. address : "",
  158. longitude : "",
  159. latitude : "",
  160. area : ""
  161. };
  162. },
  163. destroy : function(){
  164. _this.map.destroy( );//销毁地图
  165. },
  166. setMapCircle : function(radius_num, position){
  167. if(radius_num == undefined && radius_num <= 0){
  168. return;
  169. }
  170. _this.map.clearMap();
  171. // var circle_arr = map.getAllOverlays('circle');
  172. // if(circle_arr.length > 0){{
  173. // map.remove(circle)
  174. // }
  175. console.log(position);
  176. if(position != undefined){
  177. var center_position = new AMap.LngLat(position['lon'], position['lat'])
  178. }else{
  179. var center_position = _this.marker.getPosition();
  180. }
  181. var circle = new AMap.Circle({
  182. center: center_position,// 圆心位置
  183. radius: radius_num*1000, //半径
  184. strokeColor: "#F33", //线颜色
  185. strokeOpacity: 1, //线透明度
  186. strokeWeight: 3, //线粗细度
  187. fillColor: "#ee2200", //填充颜色
  188. fillOpacity: 0.35//填充透明度
  189. });
  190. _this.map.add(_this.marker);
  191. circle.setMap(_this.map);
  192. }
  193. }