webChat.js 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. (function() {
  2. 'use strict';
  3. angular
  4. .module('app')
  5. .directive('gaodeMap', Directive);
  6. Directive.$inject = [];
  7. function Directive() {
  8. var directive = {
  9. link: link,
  10. restrict: 'E',
  11. template: '<div id="container"></div>',
  12. replace: true,
  13. scope: false
  14. // scope: {
  15. // options: '='
  16. // }
  17. };
  18. return directive;
  19. function link(scope, element, attrs) {
  20. var map, geolocation, marker, geocoder;
  21. //加载地图,调用浏览器定位服务
  22. map = new AMap.Map('container', {
  23. resizeEnable: true,
  24. zoom: 17
  25. });
  26. map.plugin('AMap.Geolocation', function() {
  27. geolocation = new AMap.Geolocation({
  28. enableHighAccuracy: true, //是否使用高精度定位,默认:true
  29. timeout: 10000, //超过10秒后停止定位,默认:无穷大
  30. maximumAge: 0, //定位结果缓存0毫秒,默认:0
  31. convert: true, //自动偏移坐标,偏移后的坐标为高德坐标,默认:true
  32. showButton: true, //显示定位按钮,默认:true
  33. buttonPosition: 'RB', //定位按钮停靠位置,默认:'LB',左下角
  34. buttonOffset: new AMap.Pixel(10, 20), //定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
  35. showMarker: true, //定位成功后在定位到的位置显示点标记,默认:true
  36. showCircle: true, //定位成功后用圆圈表示定位精度范围,默认:true
  37. panToLocation: true, //定位成功后将定位到的位置作为地图中心点,默认:true
  38. zoomToAccuracy: true, //定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false
  39. useNative: true
  40. });
  41. map.addControl(geolocation);
  42. geolocation.getCurrentPosition();
  43. AMap.event.addListener(geolocation, 'complete', onComplete); //返回定位信息
  44. AMap.event.addListener(geolocation, 'error', onError); //返回定位出错信息
  45. function onComplete(data) {
  46. map.clearMap();
  47. scope.position = data;
  48. var geocoder = new AMap.Geocoder({
  49. radius: 1000,
  50. extensions: "all"
  51. });
  52. var marker = new AMap.Marker({
  53. map: map,
  54. bubble: false,
  55. visible: true
  56. });
  57. geocoder.getAddress(data.position, function(status, result) {
  58. if (status == 'complete' && result.info === 'OK') {
  59. //console.log(result);
  60. $("#result")[0][0].textContent = result.regeocode.formattedAddress.substring(6);
  61. $("#result")[0][1].textContent = result.regeocode.pois[0].name;
  62. $("#result")[0][2].textContent = result.regeocode.pois[1].name;
  63. }
  64. });
  65. map.on('click', function(e) {
  66. marker.setPosition(e.lnglat);
  67. geocoder.getAddress(e.lnglat, function(status, result) {
  68. if (status == 'complete' && result.info === 'OK') {
  69. //console.log($("#result"));
  70. //var sub = result.regeocode.formattedAddress.indexOf(result.regeocode.aois[0].name)
  71. $("#result")[0][0].textContent = result.regeocode.formattedAddress.substring(6);
  72. $("#result")[0][1].textContent = result.regeocode.pois[0].name;
  73. $("#result")[0][2].textContent = result.regeocode.pois[1].name;
  74. $("#result")[0].selectedIndex = 0;
  75. }
  76. })
  77. });
  78. }
  79. function onError(data) {}
  80. });
  81. }
  82. }
  83. })();