amapHelper.js
5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
define(['mUtil', 'amap'], function (mUtil) {
return {
/**
* 将非高德坐标转换为高德坐标
*
* @param transPoint经纬度数组 如(泛微gps坐标): [121.5222556500,31.0824170200]
* @param type类型可以为 1腾讯/高德 2baidu、3gps等
* */
convertPoint: function(transPoint, type, callback){
var convertType = "", type = Number(type);
switch(type){
case 2:
convertType = "baidu";
break;
case 3:
convertType = "gps";
break;
}
if(convertType != ""){
AMap.convertFrom(transPoint, convertType, function (status, result) {
if (result.info === 'ok') {
var resLnglat = result.locations[0];
callback && callback([resLnglat.lng, resLnglat.lat]);
}else{
mUtil.console.error("convert to GD coordinate failed.");
}
});
}else{
callback && callback(transPoint);
}
},
/**
* 根据经纬度(高德)获取中文地址
* */
getAddress: function(longitude, latitude, callback){
var parseResult = function(addr, addrComp){
callback && callback({
status: "1",
errMsg: "",
lng: longitude,//经度
lat: latitude,//纬度
addr: addr,//中文地址
province: addrComp.province,//省份
city: addrComp.city,//城市
district: addrComp.district,//区
street: addrComp.street,//街道
streetNumber: addrComp.streetNumber//街道号码
});
};
AMap.plugin('AMap.Geocoder', function() {
var geocoder = new AMap.Geocoder();
geocoder.getAddress([longitude, latitude], function(status, rs) {
rs = rs.regeocode || {};
parseResult(rs.formattedAddress, rs.addressComponent || {});
})
});
},
/**
* 根据中文地址查询坐标
* */
placeSearch: function(addr, callback){
AMap.plugin('AMap.PlaceSearch', function () {
var placeSearch = new AMap.PlaceSearch();
placeSearch.search(addr, function(status, result){
if(result.info === "OK"){
var poi = result.poiList.pois[0].location;
callback && callback([poi.lng, poi.lat]);
}else{
mUtil.getLabel(5297, "地址:#ADDR# 没有解析到结果!",function(msg){
mUtil.console.error(msg.replace("#ADDR#",addr));
});
}
});
});
},
/**
* 搜索附近的点
* */
searchNearBy: function(point, radius, num, callback){
AMap.plugin('AMap.PlaceSearch', function () {
var placeSearch = new AMap.PlaceSearch({
pageSize: Number(num)
});
placeSearch.searchNearBy('', point, Number(radius) || 100, function(status, result){
var pois = result.info === "OK" ? result.poiList.pois : [];
callback && callback(pois.map(function(item){
return {
point: {
lng: item.location.lng,
lat: item.location.lat
},
title: item.name,
address: item.address
};
}));
});
});
},
/**
* 将详细地址描述信息转换成地理坐标
*/
getPosition: function(addr, callback){
var isMatchedAddress = function(addr, formmatedAddr){
var j = 0;
for(var i = 0; i < addr.length; i++){
if(~formmatedAddr.indexOf(addr.charAt(i))){
j++;
if(j >= formmatedAddr.length / 2) return true;
}
}
return false;
}
AMap.plugin('AMap.Geocoder', function(){
var geocoder = new AMap.Geocoder();
geocoder.getLocation(addr, function(status, result){
if(status === 'complete' && result.geocodes.length){
var formattedAddress = result.geocodes[0].formattedAddress;
if(!isMatchedAddress(addr, formattedAddress)){
return;
}
var poi = result.geocodes[0].location;
callback && callback([poi.lng, poi.lat]);
}else{
mUtil.getLabel(5297, "地址:#ADDR# 没有解析到结果!",function(msg){
mUtil.console.error(msg.replace("#ADDR#",addr));
});
}
});
});
}
}
});