coordconver 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +138 -2
- data/lib/coordconver.rb +11 -0
- data/lib/coordconver/coordconver.rb +93 -93
- data/lib/coordconver/degree.rb +57 -58
- data/lib/coordconver/geo/baidu.rb +46 -0
- data/lib/coordconver/geo/client.rb +12 -0
- data/lib/coordconver/geo/configuration.rb +22 -0
- data/lib/coordconver/geo/gaode.rb +50 -0
- data/lib/coordconver/utils/http.rb +70 -0
- data/lib/coordconver/utils/request.rb +40 -0
- data/lib/coordconver/version.rb +1 -1
- metadata +10 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 547a5de94434f34a523968d850e7f857135e819b
|
4
|
+
data.tar.gz: c2b74b48ddaa5a2f581fb3791ab28791c4ca3070
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 751b6f920215846deb3aafbc7392755e7dc6f75602c1c7d66fbf6c92123049e69207647d17f6cf62ec0df05f47098271470fb4b5d1550e64a4c1b6fe31d67a98
|
7
|
+
data.tar.gz: 092f9da7b7177e74b7993bd6720739abc727f1d4f32e1cf90bdb107b88da886f29a4d55b6b4e7ad38839d83920ff81ed85ee8e65158a16674fe10258dcc12f5c
|
data/README.md
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
- 2.坐标点到经纬度的相互转换
|
7
7
|
|
8
|
-
- 3.坐标点到地理位置的转换
|
8
|
+
- 3.坐标点到地理位置的转换
|
9
9
|
|
10
10
|
## 安装
|
11
11
|
|
@@ -104,11 +104,147 @@ gem 'coordconver'
|
|
104
104
|
114°65’24.6″=114+65/60+24.6/3600=115.00683333333333
|
105
105
|
|
106
106
|
|
107
|
-
## 坐标点与地址转换
|
107
|
+
## 坐标点与地址转换
|
108
|
+
|
109
|
+
地理编码/逆地理编码API是一类简单的HTTP接口,提供结构化地址与经纬度之间的相互转化服务。
|
110
|
+
|
111
|
+
该套API免费对外开放,使用API您需先申请对应的API Key,
|
112
|
+
|
113
|
+
- 百度地图 默认配额6000次/天。
|
114
|
+
|
115
|
+
- 高德地图API,企业用户单个Key支持200万次/天,5万次/分钟。
|
116
|
+
|
117
|
+
防止同一key 使用超额, 建议申请多个key使用让系统随机调用。
|
118
|
+
|
119
|
+
### 用法
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
Coordconver::Client.new(["百度 ak1","百度 ak2"],["高德 key1"," key2"])
|
123
|
+
|
124
|
+
Coordconver::Baidu.regeo(116.322987, 39.983424) #百度地图api在线坐标转换地址
|
125
|
+
|
126
|
+
Coordconver::Gaode.regeo(116.480881,39.989410) #高德地图api在线坐标转换地址
|
127
|
+
|
128
|
+
Coordconver::Gaode.geo("北京方恒国际中心A座") #高德地图api在线地址转换坐标
|
129
|
+
|
130
|
+
Coordconver::Baidu.geo("百度大厦") #百度地图api在线地址转换坐标
|
131
|
+
```
|
132
|
+
|
133
|
+
示例:
|
134
|
+
|
135
|
+
>Coordconver::Client.new(["xxxxx],["xxxx"])
|
136
|
+
|
108
137
|
1.百度地图转换
|
138
|
+
[Online API](http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding)
|
139
|
+
|
140
|
+
|
141
|
+
- 地理编码:即地址解析,由详细到街道的结构化地址得到百度经纬度信息
|
142
|
+
例如:“北京市海淀区中关村南大街27号”地址解析的结果是“lng:116.31985,lat:39.959836”。
|
143
|
+
|
144
|
+
>Coordconver::Baidu.geo("百度大厦")["result"]["location"].values # => [116.30775539540981, 40.05685561073758]
|
145
|
+
|
146
|
+
```json
|
147
|
+
{
|
148
|
+
"status": 0,
|
149
|
+
"result": {
|
150
|
+
"location": {
|
151
|
+
"lng": 116.30775539540981,
|
152
|
+
"lat": 40.05685561073758
|
153
|
+
},
|
154
|
+
"precise": 1,
|
155
|
+
"confidence": 80,
|
156
|
+
"level": "商务大厦"
|
157
|
+
}
|
158
|
+
}
|
159
|
+
```
|
160
|
+
|
161
|
+
- 逆地理编码:即逆地址解析,由百度经纬度信息得到结构化地址信息,例如:“lat:31.325152,lng:120.558957”逆地址解析的结果是“江苏省苏州市虎丘区塔园路318号”。
|
162
|
+
|
163
|
+
>Coordconver::Baidu.regeo(116.32298699999993,39.98342407140365)["result"]["formatted_address"] # => 北京市海淀区中关村大街27号1101-08室
|
164
|
+
|
165
|
+
```json
|
166
|
+
{
|
167
|
+
"status": 0,
|
168
|
+
"result": {
|
169
|
+
"location": {
|
170
|
+
"lng": 116.32298699999993,
|
171
|
+
"lat": 39.98342407140365
|
172
|
+
},
|
173
|
+
"formatted_address": "北京市海淀区中关村大街27号1101-08室",
|
174
|
+
"business": "中关村,人民大学,苏州街",
|
175
|
+
"addressComponent": {
|
176
|
+
"country": "中国",
|
177
|
+
"country_code": 0,
|
178
|
+
"province": "北京市",
|
179
|
+
"city": "北京市",
|
180
|
+
"district": "海淀区",
|
181
|
+
"adcode": "110108",
|
182
|
+
"street": "中关村大街",
|
183
|
+
"street_number": "27号1101-08室",
|
184
|
+
"direction": "附近",
|
185
|
+
"distance": "7"
|
186
|
+
},
|
187
|
+
"pois": [],
|
188
|
+
"poiRegions": [],
|
189
|
+
"sematic_description": "北京远景国际公寓(中关村店)内0米",
|
190
|
+
"cityCode": 131
|
191
|
+
}
|
192
|
+
}
|
193
|
+
|
194
|
+
```
|
109
195
|
|
110
196
|
2.高德地图转换
|
111
197
|
|
198
|
+
[API](http://lbs.amap.com/api/webservice/guide/api/georegeo/#geo)
|
199
|
+
|
200
|
+
- 地理编码:将详细的结构化地址转换为高德经纬度坐标,且支持名胜景区、标志性建筑物名称解析为高德经纬度坐标。
|
201
|
+
例如:北京市朝阳区阜通东大街6号-->116.480881,39.989410
|
202
|
+
天安门-->116.397499,39.908722
|
203
|
+
|
204
|
+
>Coordconver::Gaode.geo("北京方恒国际中心A座")["geocodes"][0]["location"]
|
205
|
+
|
206
|
+
```json
|
207
|
+
{
|
208
|
+
"status" : "1",
|
209
|
+
"info" : "OK",
|
210
|
+
"infocode" : "10000",
|
211
|
+
"count" : "1",
|
212
|
+
"geocodes" :
|
213
|
+
[
|
214
|
+
"0" :
|
215
|
+
{
|
216
|
+
"formatted_address" : "北京市朝阳区方恒国际中心|A座",
|
217
|
+
"province" : "北京市",
|
218
|
+
"citycode" : "010",
|
219
|
+
"city" : "北京市",
|
220
|
+
"district" : "朝阳区",
|
221
|
+
...
|
222
|
+
"location" : "116.480724,39.989584",
|
223
|
+
"level" : "门牌号"
|
224
|
+
}
|
225
|
+
]
|
226
|
+
}
|
227
|
+
```
|
228
|
+
|
229
|
+
- 逆地理编码:将经纬度转换为详细结构化的地址,且返回附近周边的POI信息,以及该经纬度所在的POI信息。
|
230
|
+
例如:116.480881,39.989410-->北京市朝阳区阜通东大街6号
|
231
|
+
|
232
|
+
>conver::Gaode.regeo(116.480881,39.989410)['regeocode']["formatted_address"]
|
233
|
+
|
234
|
+
|
235
|
+
```json
|
236
|
+
{
|
237
|
+
"status" : "1",
|
238
|
+
"info" : "OK",
|
239
|
+
"infocode" : "10000",
|
240
|
+
"regeocode" :
|
241
|
+
{
|
242
|
+
"formatted_address" : "北京市朝阳区望京街道方恒国际中心B座方恒国际中心",
|
243
|
+
...
|
244
|
+
}
|
245
|
+
}
|
246
|
+
|
247
|
+
```
|
112
248
|
|
113
249
|
|
114
250
|
## Development
|
data/lib/coordconver.rb
CHANGED
@@ -1,8 +1,19 @@
|
|
1
1
|
require "coordconver/version"
|
2
2
|
require "coordconver/coordconver"
|
3
3
|
require "coordconver/degree"
|
4
|
+
require "coordconver/geo/configuration"
|
5
|
+
require "coordconver/geo/client"
|
6
|
+
require "coordconver/geo/baidu"
|
7
|
+
require "coordconver/geo/gaode"
|
8
|
+
require "coordconver/utils/http"
|
9
|
+
require "coordconver/utils/request"
|
4
10
|
|
5
11
|
module Coordconver
|
12
|
+
|
6
13
|
include Coordconver
|
7
14
|
include Degree
|
15
|
+
include Geo::Configuration
|
16
|
+
include Geo
|
17
|
+
include Utils
|
18
|
+
|
8
19
|
end
|
@@ -1,106 +1,106 @@
|
|
1
1
|
module Coordconver
|
2
|
-
|
2
|
+
module Coordconver
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
def self.included(base)
|
5
|
+
base.extend(ClassMethods)
|
6
|
+
end
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
8
|
+
# 定义一些常量
|
9
|
+
X_PI = 3.14159265358979324 * 3000.0 / 180.0
|
10
|
+
PI = 3.1415926535897932384626
|
11
|
+
AA = 6378245.0 #a
|
12
|
+
EE = 0.00669342162296594323 #e
|
13
13
|
|
14
|
-
|
14
|
+
module ClassMethods
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
16
|
+
def transformlat(lng, lat)
|
17
|
+
ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat + 0.2 * Math.sqrt((lng).abs);
|
18
|
+
ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
|
19
|
+
ret += (20.0 * Math.sin(lat * PI) + 40.0 * Math.sin(lat / 3.0 * PI)) * 2.0 / 3.0;
|
20
|
+
ret += (160.0 * Math.sin(lat / 12.0 * PI) + 320 * Math.sin(lat * PI / 30.0)) * 2.0 / 3.0;
|
21
|
+
return ret
|
22
|
+
end
|
23
23
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
24
|
+
def transformlng(lng, lat)
|
25
|
+
ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.sqrt((lng).abs);
|
26
|
+
ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
|
27
|
+
ret += (20.0 * Math.sin(lng * PI) + 40.0 * Math.sin(lng / 3.0 * PI)) * 2.0 / 3.0;
|
28
|
+
ret += (150.0 * Math.sin(lng / 12.0 * PI) + 300.0 * Math.sin(lng / 30.0 * PI)) * 2.0 / 3.0;
|
29
|
+
end
|
30
30
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
31
|
+
# 百度坐标系 (BD-09) -> 火星坐标系 (GCJ-02)
|
32
|
+
# 即 百度 -> 谷歌、高德
|
33
|
+
# Coordconver.bd_gcj(lng, lat)
|
34
|
+
def bd_gcj(lng, lat)
|
35
|
+
x = lng - 0.0065;
|
36
|
+
y = lat - 0.006;
|
37
|
+
z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * X_PI);
|
38
|
+
theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * X_PI);
|
39
|
+
gg_lng = z * Math.cos(theta);
|
40
|
+
gg_lat = z * Math.sin(theta);
|
41
|
+
return [gg_lng, gg_lat]
|
42
|
+
end
|
43
43
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
44
|
+
# 百度坐标系 (BD-09) -> 地球坐标 (WGS-84)
|
45
|
+
# 即 百度 -> 地球坐标
|
46
|
+
# Coordconver.bd_wgs(lng, lat)
|
47
|
+
def bd_wgs(lng, lat)
|
48
|
+
loca = wgs_gcj(lng, lat)
|
49
|
+
gcj_bd(loca[0], loca[1])
|
50
|
+
end
|
51
51
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
52
|
+
# 火星坐标系 (GCJ02) -> 地球坐标 (WGS-84)
|
53
|
+
# 即 谷歌、高德 -> 地球坐标
|
54
|
+
# Coordconver.gcj_wgs(lng, lat)
|
55
|
+
def gcj_wgs(lng, lat)
|
56
|
+
dlat = transformlat(lng - 105.0, lat - 35.0);
|
57
|
+
dlng = transformlng(lng - 105.0, lat - 35.0);
|
58
|
+
radlat = lat / 180.0 * PI;
|
59
|
+
magic = Math.sin(radlat);
|
60
|
+
magic = 1 - EE * magic * magic;
|
61
|
+
sqrtmagic = Math.sqrt(magic);
|
62
|
+
dlat = (dlat * 180.0) / ((AA * (1 - EE)) / (magic * sqrtmagic) * PI);
|
63
|
+
dlng = (dlng * 180.0) / (AA / sqrtmagic * Math.cos(radlat) * PI);
|
64
|
+
mglat = lat + dlat;
|
65
|
+
mglng = lng + dlng;
|
66
|
+
return [lng * 2 - mglng, lat * 2 - mglat]
|
67
|
+
end
|
68
68
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
69
|
+
# 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09)
|
70
|
+
# 即 谷歌、高德 -> 百度
|
71
|
+
# Coordconver.gcj_bd(lng, lat)
|
72
|
+
def gcj_bd(lng, lat)
|
73
|
+
z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * X_PI);
|
74
|
+
theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * X_PI);
|
75
|
+
bd_lng = z * Math.cos(theta) + 0.0065;
|
76
|
+
bd_lat = z * Math.sin(theta) + 0.006;
|
77
|
+
return [bd_lng, bd_lat]
|
78
|
+
end
|
79
79
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
80
|
+
# 地球坐标 ( WGS-84) 与百度坐标系 (BD-09)
|
81
|
+
# 即 地球坐标 -> 百度
|
82
|
+
# Coordconver.wgs_bd(lng, lat)
|
83
|
+
def wgs_bd(lng, lat)
|
84
|
+
loca = wgs_gcj(lng, lat)
|
85
|
+
gcj_bd(loca[0], loca[1])
|
86
|
+
end
|
87
87
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
88
|
+
# 地球坐标 ( WGS-84) 与火星坐标系 (GCJ-02)
|
89
|
+
# 即 地球坐标 -> 谷歌、高德
|
90
|
+
# Coordconver.wgs_gcj(lng, lat)
|
91
|
+
def wgs_gcj(lng, lat)
|
92
|
+
dlat = transformlat(lng - 105.0, lat - 35.0);
|
93
|
+
dlng = transformlng(lng - 105.0, lat - 35.0);
|
94
|
+
radlat = lat / 180.0 * PI;
|
95
|
+
magic = Math.sin(radlat);
|
96
|
+
magic = 1 - EE * magic * magic;
|
97
|
+
sqrtmagic = Math.sqrt(magic);
|
98
|
+
dlat = (dlat * 180.0) / ((AA * (1 - EE)) / (magic * sqrtmagic) * PI);
|
99
|
+
dlng = (dlng * 180.0) / (AA / sqrtmagic * Math.cos(radlat) * PI);
|
100
|
+
mglat = lat + dlat;
|
101
|
+
mglng = lng + dlng;
|
102
|
+
return [mglng, mglat]
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
106
|
end
|
data/lib/coordconver/degree.rb
CHANGED
@@ -1,60 +1,59 @@
|
|
1
1
|
module Coordconver
|
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
|
-
end
|
2
|
+
module Degree
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
|
9
|
+
# 取整数位
|
10
|
+
def num(n)
|
11
|
+
n.to_i
|
12
|
+
end
|
13
|
+
|
14
|
+
# 取小数数位 Decimal digit
|
15
|
+
def dig(n)
|
16
|
+
n.to_f-num(n)
|
17
|
+
end
|
18
|
+
|
19
|
+
# 计算分
|
20
|
+
def min(n)
|
21
|
+
dig(n)*60
|
22
|
+
end
|
23
|
+
|
24
|
+
# 计算秒
|
25
|
+
def sec(n)
|
26
|
+
(min(n)-num(min(n)))*60
|
27
|
+
end
|
28
|
+
|
29
|
+
# 坐标转度数
|
30
|
+
def degree(lng, lat)
|
31
|
+
[cal_degree(lng),cal_degree(lat)]
|
32
|
+
end
|
33
|
+
|
34
|
+
def cal_degree(n)
|
35
|
+
"#{num(n)}°#{num(min(n))}′#{num(sec(n))}″"
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
# 获取分
|
40
|
+
def get_min(n)
|
41
|
+
n.split('°').last.to_f
|
42
|
+
end
|
43
|
+
|
44
|
+
# 获取秒
|
45
|
+
def get_sec(n)
|
46
|
+
n.split('′').last.to_f
|
47
|
+
end
|
48
|
+
|
49
|
+
# 度数转坐标
|
50
|
+
def cal_undegree(n)
|
51
|
+
(num(n) + get_min(n)/60 + get_sec(n)/3600).round(5)
|
52
|
+
end
|
53
|
+
|
54
|
+
def undegree(lng, lat)
|
55
|
+
[cal_undegree(lng),cal_undegree(lat)]
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
60
59
|
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module Coordconver
|
2
|
+
module Geo
|
3
|
+
module Baidu
|
4
|
+
extend self
|
5
|
+
|
6
|
+
# Coordconver::Baidu.regeo(39.983424,116.322987)
|
7
|
+
def regeo(lng, lat)
|
8
|
+
url = regeo_url(lng, lat)
|
9
|
+
Utils::Request.get(url, params: nil)
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
def geo(address)
|
14
|
+
url = base_url
|
15
|
+
|
16
|
+
params = {
|
17
|
+
ak: baidu_map_ak,
|
18
|
+
output: 'json',
|
19
|
+
address: address
|
20
|
+
}
|
21
|
+
|
22
|
+
Utils::Request.get(url, params: params)
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def regeo_url(lng, lat)
|
28
|
+
base_url + "?location=#{lat},#{lng}&output=json&ak=#{baidu_map_ak}"
|
29
|
+
end
|
30
|
+
|
31
|
+
# http://api.map.baidu.com/geocoder/v2/?location=39.983424,116.322987&output=json&ak=uzInpBxCpE0DiPnln3UbDv4OQTdOgmXu
|
32
|
+
|
33
|
+
# http://api.map.baidu.com/geocoder/v2?location=39,116.480881&output=json&ak=uzInpBxCpE0DiPnln3UbDv4OQTdOgmXu
|
34
|
+
|
35
|
+
# http://api.map.baidu.com/geocoder/v2/?output=json&address=百度大厦&city=北京市&ak=您的ak
|
36
|
+
|
37
|
+
def base_url
|
38
|
+
Configuration.settings[:baidu_base_url]
|
39
|
+
end
|
40
|
+
|
41
|
+
def baidu_map_ak
|
42
|
+
Configuration.settings[:baidu_map_aks].sample
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Coordconver
|
2
|
+
module Geo
|
3
|
+
module Configuration
|
4
|
+
extend self
|
5
|
+
|
6
|
+
DEFAULT_MAP_OPTIONS = {
|
7
|
+
gaode_base_url: 'http://restapi.amap.com/v3/geocode',
|
8
|
+
baidu_base_url: 'http://api.map.baidu.com/geocoder/v2/',
|
9
|
+
output: 'json'
|
10
|
+
}
|
11
|
+
|
12
|
+
attr_reader :settings
|
13
|
+
|
14
|
+
def init(baidu_map_aks, gaode_map_keys)
|
15
|
+
@settings = DEFAULT_MAP_OPTIONS.merge!(baidu_map_aks: baidu_map_aks, gaode_map_keys: gaode_map_keys)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module Coordconver
|
2
|
+
module Geo
|
3
|
+
module Gaode
|
4
|
+
extend self
|
5
|
+
|
6
|
+
# Coordconver::Gaode.regeo(116.480881,39.989410)
|
7
|
+
# 116.480881,39.989410-->北京市朝阳区阜通东大街6号
|
8
|
+
def regeo(lng, lat)
|
9
|
+
url = regeo_url(lng, lat)
|
10
|
+
# params = {
|
11
|
+
# key: gaode_map_key,
|
12
|
+
# location: lng + "," + lat
|
13
|
+
# }
|
14
|
+
|
15
|
+
Utils::Request.get(url, params: nil)
|
16
|
+
end
|
17
|
+
|
18
|
+
# Coordconver::Gaode.geo("北京市朝阳区阜通东大街6号")
|
19
|
+
# 116.480881,39.989410-->北京市朝阳区阜通东大街6号
|
20
|
+
def geo(address)
|
21
|
+
url = geo_url
|
22
|
+
|
23
|
+
params = {
|
24
|
+
key: gaode_map_key,
|
25
|
+
address: address
|
26
|
+
}
|
27
|
+
|
28
|
+
Utils::Request.get(url, params: params)
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def regeo_url(lng, lat)
|
34
|
+
base_url + '/regeo' + "?key=#{gaode_map_key}&location=#{lng},#{lat}"
|
35
|
+
end
|
36
|
+
|
37
|
+
def geo_url
|
38
|
+
base_url + '/geo'
|
39
|
+
end
|
40
|
+
|
41
|
+
def base_url
|
42
|
+
Configuration.settings[:gaode_base_url]
|
43
|
+
end
|
44
|
+
|
45
|
+
def gaode_map_key
|
46
|
+
Configuration.settings[:gaode_map_keys].sample
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module Coordconver
|
5
|
+
module Utils
|
6
|
+
class Http
|
7
|
+
|
8
|
+
DEFAULT_USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36"
|
9
|
+
DEFAULT_OPEN_TIMEOUT = 20
|
10
|
+
DEFAULT_READ_TIMEOUT = 120
|
11
|
+
DEFAULT_RETRY_TIMES = 3
|
12
|
+
RETRY_SLEEP_TIME = 3
|
13
|
+
|
14
|
+
HTTP_VERB_MAP = {
|
15
|
+
get: Net::HTTP::Get,
|
16
|
+
post: Net::HTTP::Post,
|
17
|
+
put: Net::HTTP::Put,
|
18
|
+
delete: Net::HTTP::Delete
|
19
|
+
}
|
20
|
+
|
21
|
+
DEFAULT_HEADERS = {
|
22
|
+
'user-agent' => DEFAULT_USER_AGENT,
|
23
|
+
'accept' => 'application/json',
|
24
|
+
'content-type' => 'application/json',
|
25
|
+
'connection' => 'close'
|
26
|
+
}
|
27
|
+
|
28
|
+
def initialize(method, url, params: nil, body: nil, headers: {}, opts: {})
|
29
|
+
method = method.downcase.to_sym
|
30
|
+
err_msg = "http method #{method} is not supported"
|
31
|
+
raise err_msg unless HTTP_VERB_MAP.keys.include?(method)
|
32
|
+
@uri = URI(url)
|
33
|
+
@uri.query = URI.encode_www_form(params) unless params.nil?
|
34
|
+
@request = prepare_request(method, body, headers)
|
35
|
+
@opts = opts
|
36
|
+
end
|
37
|
+
|
38
|
+
def send_request
|
39
|
+
tries ||= DEFAULT_RETRY_TIMES
|
40
|
+
opts ||= default_opts.merge @opts
|
41
|
+
Net::HTTP.start(@uri.host, @uri.port, opts) do |http|
|
42
|
+
http.request(@request)
|
43
|
+
end
|
44
|
+
# if raise Timeout::Error retry it for 3 times
|
45
|
+
rescue Net::OpenTimeout, Net::ReadTimeout
|
46
|
+
(tries -= 1).zero? ? (raise "Time out") : retry
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def prepare_request(method, body, headers)
|
52
|
+
headers = DEFAULT_HEADERS.merge(headers)
|
53
|
+
request = HTTP_VERB_MAP[method].new @uri
|
54
|
+
request.initialize_http_header(headers)
|
55
|
+
request.body = body.to_json unless body.nil?
|
56
|
+
request
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
def default_opts
|
61
|
+
{
|
62
|
+
use_ssl: 'https' == @uri.scheme,
|
63
|
+
open_timeout: DEFAULT_OPEN_TIMEOUT,
|
64
|
+
read_timeout: DEFAULT_READ_TIMEOUT
|
65
|
+
}
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Coordconver
|
2
|
+
module Utils
|
3
|
+
module Request
|
4
|
+
extend self
|
5
|
+
|
6
|
+
def get(url, params: nil, headers: {})
|
7
|
+
send_request(:get, url, params: params, headers: headers)
|
8
|
+
end
|
9
|
+
|
10
|
+
def post(url, body: , headers: {})
|
11
|
+
send_request(:post, url, body: body, headers: headers)
|
12
|
+
end
|
13
|
+
|
14
|
+
def put(url, body: , headers: {})
|
15
|
+
send_request(:put, url, body: body, headers: headers)
|
16
|
+
end
|
17
|
+
|
18
|
+
def delete(url, params: nil, headers: {})
|
19
|
+
send_request(:delete, url, params: params, headers: headers)
|
20
|
+
end
|
21
|
+
|
22
|
+
def send_request(method, url, params: nil, body: nil, headers: {}, opts: {})
|
23
|
+
raw_response = Utils::Http.new(
|
24
|
+
method.to_sym,
|
25
|
+
url,
|
26
|
+
params: params,
|
27
|
+
body: body,
|
28
|
+
headers: headers,
|
29
|
+
opts: opts
|
30
|
+
).send_request
|
31
|
+
|
32
|
+
parse_body(raw_response.body)
|
33
|
+
end
|
34
|
+
|
35
|
+
def parse_body(body)
|
36
|
+
JSON.parse(body)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/lib/coordconver/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: coordconver
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- chenwei
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-12-
|
11
|
+
date: 2016-12-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -73,6 +73,12 @@ files:
|
|
73
73
|
- lib/coordconver.rb
|
74
74
|
- lib/coordconver/coordconver.rb
|
75
75
|
- lib/coordconver/degree.rb
|
76
|
+
- lib/coordconver/geo/baidu.rb
|
77
|
+
- lib/coordconver/geo/client.rb
|
78
|
+
- lib/coordconver/geo/configuration.rb
|
79
|
+
- lib/coordconver/geo/gaode.rb
|
80
|
+
- lib/coordconver/utils/http.rb
|
81
|
+
- lib/coordconver/utils/request.rb
|
76
82
|
- lib/coordconver/version.rb
|
77
83
|
homepage: https://github.com/cheenwe/coordconver
|
78
84
|
licenses:
|
@@ -94,8 +100,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
94
100
|
version: '0'
|
95
101
|
requirements: []
|
96
102
|
rubyforge_project:
|
97
|
-
rubygems_version: 2.
|
103
|
+
rubygems_version: 2.6.1
|
98
104
|
signing_key:
|
99
105
|
specification_version: 4
|
100
106
|
summary: 坐标转换/Coordconver
|
101
107
|
test_files: []
|
108
|
+
has_rdoc:
|