mapservice.js 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. (function (app) {
  2. app.factory("map", ["$q", "$http", function ($q, $http) {
  3. var key = '9f832e163baac0a04d437a52dd3e5d29';
  4. return {
  5. //获取高德地图图片
  6. getMapPic: function (input) {
  7. var deferred = $q.defer();
  8. var defaultPath = "img/demo/defaultmap.png";
  9. if (!input) return defaultPath;
  10. var arr = input.split(',');
  11. var keywords = arr[arr.length - 2]+arr[arr.length - 1];
  12. var url = 'http://restapi.amap.com/v3/place/text?key=' + key + '&keywords=' + keywords + "&callback=JSON_CALLBACK";
  13. $http.jsonp(url).then(function (result) {
  14. var p = result.data.pois[0];
  15. if (p) {
  16. var l = p.location;
  17. var m = "http://restapi.amap.com/v3/staticmap?location=" + l + "&zoom=10&size=720*280&markers=mid,,A:" + l + "&key=" + key;
  18. deferred.resolve(m);
  19. } else {
  20. deferred.resolve(defaultPath);
  21. }
  22. }, function (erro) {
  23. deferred.reject(erro);
  24. });
  25. return deferred.promise;
  26. },
  27. //通过ip获取地址
  28. getLocation: function () {
  29. //http://restapi.amap.com/v3/ip?key=9f832e163baac0a04d437a52dd3e5d29
  30. var deferred = $q.defer();
  31. var url = "http://restapi.amap.com/v3/ip?key=" + key + "&callback=JSON_CALLBACK";
  32. $http.jsonp(url).then(function (result) {
  33. deferred.resolve(result.data);
  34. }, function (erro) {
  35. deferred.reject(erro);
  36. });
  37. ////http://lbs.amap.com/api/javascript-api/reference/plugin/#m_AMap.Geolocation
  38. //var map, geolocation;
  39. ////加载地图,调用浏览器定位服务
  40. //map = new AMap.Map('container', {
  41. // resizeEnable: true
  42. //});
  43. //map.plugin('AMap.Geolocation', function () {
  44. // geolocation = new AMap.Geolocation({
  45. // enableHighAccuracy: true,//是否使用高精度定位,默认:true
  46. // // timeout: 10000, //超过10秒后停止定位,默认:无穷大
  47. // buttonOffset: new AMap.Pixel(10, 20),//定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
  48. // zoomToAccuracy: true, //定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false
  49. // buttonPosition: 'RB'
  50. // });
  51. // map.addControl(geolocation);
  52. // geolocation.getCurrentPosition();
  53. // AMap.event.addListener(geolocation, 'complete', function (data) {
  54. // debugger;
  55. // //返回定位信息
  56. // var str = ['定位成功'];
  57. // str.push('经度:' + data.position.getLng());
  58. // str.push('纬度:' + data.position.getLat());
  59. // str.push('精度:' + data.accuracy + ' 米');
  60. // str.push('是否经过偏移:' + (data.isConverted ? '是' : '否'));
  61. // deferred.resolve(str);
  62. // });
  63. // AMap.event.addListener(geolocation, 'error', function (erro) { //返回定位出错信息
  64. // deferred.reject(erro);
  65. // });
  66. //});
  67. return deferred.promise;
  68. }
  69. }
  70. }]);
  71. })(angular.module('app.services'));