amapHelper.js
1.71 KB
define(["mUtil","amap"],function(c){return{convertPoint:function(e,n,t){var o="";switch(n=Number(n)){case 2:o="baidu";break;case 3:o="gps"}""!=o?AMap.convertFrom(e,o,function(e,n){if("ok"===n.info){var o=n.locations[0];t&&t([o.lng,o.lat])}else c.console.error("convert to GD coordinate failed.")}):t&&t(e)},getAddress:function(r,a,c){AMap.plugin("AMap.Geocoder",function(){(new AMap.Geocoder).getAddress([r,a],function(e,n){var o,t;n=n.regeocode||{},o=n.formattedAddress,t=n.addressComponent||{},c&&c({status:"1",errMsg:"",lng:r,lat:a,addr:o,province:t.province,city:t.city,district:t.district,street:t.street,streetNumber:t.streetNumber})})})},placeSearch:function(t,r){AMap.plugin("AMap.PlaceSearch",function(){(new AMap.PlaceSearch).search(t,function(e,n){if("OK"===n.info){var o=n.poiList.pois[0].location;r&&r([o.lng,o.lat])}else c.getLabel(5297,"地址:#ADDR# 没有解析到结果!",function(e){c.console.error(e.replace("#ADDR#",t))})})})},searchNearBy:function(e,n,o,t){AMap.plugin("AMap.PlaceSearch",function(){new AMap.PlaceSearch({pageSize:Number(o)}).searchNearBy("",e,Number(n)||100,function(e,n){var o="OK"===n.info?n.poiList.pois:[];t&&t(o.map(function(e){return{point:{lng:e.location.lng,lat:e.location.lat},title:e.name,address:e.address}}))})})},getPosition:function(r,a){AMap.plugin("AMap.Geocoder",function(){(new AMap.Geocoder).getLocation(r,function(e,n){if("complete"===e&&n.geocodes.length){var o=n.geocodes[0].formattedAddress;if(!function(e,n){for(var o=0,t=0;t<e.length;t++)if(~n.indexOf(e.charAt(t))&&++o>=n.length/2)return!0;return!1}(r,o))return;var t=n.geocodes[0].location;a&&a([t.lng,t.lat])}else c.getLabel(5297,"地址:#ADDR# 没有解析到结果!",function(e){c.console.error(e.replace("#ADDR#",r))})})})}}});