server-side-google-maps 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
data/README.md
CHANGED
@@ -20,12 +20,17 @@ Then, to use within Ruby:
|
|
20
20
|
directions.origin_point # [ 45.5086700, -73.5536800 ]
|
21
21
|
directions.destination_address # 'Ottawa, ON, Canada'
|
22
22
|
directions.destination_point # [ 45.4119000, -75.6984600 ]
|
23
|
-
directions.points #
|
23
|
+
directions.points # Array of [lat,lon] coordinates of route
|
24
24
|
directions.distance # 199901 (metres)
|
25
25
|
|
26
26
|
route = Route.new(['Montreal, QC', 'Ottawa, ON', 'Toronto, ON'], :mode => :bicycling)
|
27
27
|
# All the same methods apply to route as to directions
|
28
28
|
|
29
|
+
# We can also find elevations along a path
|
30
|
+
path = Path.new([ [ 45.5086700, -73.5536800 ], [ 45.4119000, -75.6984600 ] ]) # a straight line
|
31
|
+
path.elevations(20) # Array of equidistant altitudes, in metres, along the path
|
32
|
+
# Paths may only be up to around 230 points long, to comply with Google's URL length limit.
|
33
|
+
|
29
34
|
One final `:mode` is `:direct`, which calculates `points` and estimates
|
30
35
|
`distance` without querying Google. To ensure Google isn't queried, input
|
31
36
|
the origin and destination as latitude/longitude coordinates.
|
@@ -4,6 +4,7 @@ Bundler.require
|
|
4
4
|
require 'server-side-google-maps/version'
|
5
5
|
require 'server-side-google-maps/directions'
|
6
6
|
require 'server-side-google-maps/geo_math'
|
7
|
+
require 'server-side-google-maps/path'
|
7
8
|
require 'server-side-google-maps/route'
|
8
9
|
require 'server-side-google-maps/server'
|
9
10
|
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module ServerSideGoogleMaps
|
2
|
+
class Path
|
3
|
+
def self.get_elevations(params)
|
4
|
+
server = Server.new
|
5
|
+
server.get('/maps/api/elevation', {:sensor => false}.merge(params))
|
6
|
+
end
|
7
|
+
|
8
|
+
def initialize(points)
|
9
|
+
raise ArgumentError.new('path must be an Enumerable') unless Enumerable === points
|
10
|
+
raise ArgumentError.new('path must have one or more points') unless points.length > 0
|
11
|
+
i = 0
|
12
|
+
@points = points.collect do |pt|
|
13
|
+
raise ArgumentError.new("path element #{i} must be a [latitude, longitude] Array") unless pt.length == 2
|
14
|
+
pt.dup
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# Returns an Array of n equidistant altitudes (in metres) along this path
|
19
|
+
def elevations(n)
|
20
|
+
results = self.class.get_elevations(:path => "enc:#{encoded_path}", :samples => n)
|
21
|
+
results['results'].collect { |r| r['elevation'].to_f }
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def encoded_path
|
27
|
+
@encoded_path ||= ::GoogleMapsPolyline::Encoder.new(StringIO.new).encode_points(points_1e5).string
|
28
|
+
end
|
29
|
+
|
30
|
+
def points_1e5
|
31
|
+
@points.map { |latitude, longitude| [ (latitude * 1e5).to_i, (longitude * 1e5).to_i ] }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,124 @@
|
|
1
|
+
{
|
2
|
+
"status": "OK",
|
3
|
+
"results": [ {
|
4
|
+
"location": {
|
5
|
+
"lat": 45.5086700,
|
6
|
+
"lng": -73.5536800
|
7
|
+
},
|
8
|
+
"elevation": 15.3455887
|
9
|
+
}, {
|
10
|
+
"location": {
|
11
|
+
"lat": 45.4185028,
|
12
|
+
"lng": -73.8696901
|
13
|
+
},
|
14
|
+
"elevation": 20.8894730
|
15
|
+
}, {
|
16
|
+
"location": {
|
17
|
+
"lat": 45.3274670,
|
18
|
+
"lng": -74.1846876
|
19
|
+
},
|
20
|
+
"elevation": 44.8820000
|
21
|
+
}, {
|
22
|
+
"location": {
|
23
|
+
"lat": 45.2355682,
|
24
|
+
"lng": -74.4986695
|
25
|
+
},
|
26
|
+
"elevation": 59.5912666
|
27
|
+
}, {
|
28
|
+
"location": {
|
29
|
+
"lat": 45.1428119,
|
30
|
+
"lng": -74.8116328
|
31
|
+
},
|
32
|
+
"elevation": 85.9591980
|
33
|
+
}, {
|
34
|
+
"location": {
|
35
|
+
"lat": 45.0492036,
|
36
|
+
"lng": -75.1235748
|
37
|
+
},
|
38
|
+
"elevation": 90.0000000
|
39
|
+
}, {
|
40
|
+
"location": {
|
41
|
+
"lat": 44.9547490,
|
42
|
+
"lng": -75.4344927
|
43
|
+
},
|
44
|
+
"elevation": 75.8659973
|
45
|
+
}, {
|
46
|
+
"location": {
|
47
|
+
"lat": 44.8594535,
|
48
|
+
"lng": -75.7443843
|
49
|
+
},
|
50
|
+
"elevation": 112.5547333
|
51
|
+
}, {
|
52
|
+
"location": {
|
53
|
+
"lat": 44.7633228,
|
54
|
+
"lng": -76.0532474
|
55
|
+
},
|
56
|
+
"elevation": 138.2833099
|
57
|
+
}, {
|
58
|
+
"location": {
|
59
|
+
"lat": 44.6663624,
|
60
|
+
"lng": -76.3610798
|
61
|
+
},
|
62
|
+
"elevation": 128.3775635
|
63
|
+
}, {
|
64
|
+
"location": {
|
65
|
+
"lat": 44.5685778,
|
66
|
+
"lng": -76.6678798
|
67
|
+
},
|
68
|
+
"elevation": 182.8619995
|
69
|
+
}, {
|
70
|
+
"location": {
|
71
|
+
"lat": 44.4699747,
|
72
|
+
"lng": -76.9736456
|
73
|
+
},
|
74
|
+
"elevation": 155.4394684
|
75
|
+
}, {
|
76
|
+
"location": {
|
77
|
+
"lat": 44.3705587,
|
78
|
+
"lng": -77.2783759
|
79
|
+
},
|
80
|
+
"elevation": 158.1198730
|
81
|
+
}, {
|
82
|
+
"location": {
|
83
|
+
"lat": 44.2703352,
|
84
|
+
"lng": -77.5820692
|
85
|
+
},
|
86
|
+
"elevation": 122.1667023
|
87
|
+
}, {
|
88
|
+
"location": {
|
89
|
+
"lat": 44.1693099,
|
90
|
+
"lng": -77.8847244
|
91
|
+
},
|
92
|
+
"elevation": 154.1021423
|
93
|
+
}, {
|
94
|
+
"location": {
|
95
|
+
"lat": 44.0674883,
|
96
|
+
"lng": -78.1863405
|
97
|
+
},
|
98
|
+
"elevation": 284.4921570
|
99
|
+
}, {
|
100
|
+
"location": {
|
101
|
+
"lat": 43.9648759,
|
102
|
+
"lng": -78.4869167
|
103
|
+
},
|
104
|
+
"elevation": 151.9664764
|
105
|
+
}, {
|
106
|
+
"location": {
|
107
|
+
"lat": 43.8614784,
|
108
|
+
"lng": -78.7864523
|
109
|
+
},
|
110
|
+
"elevation": 70.7919540
|
111
|
+
}, {
|
112
|
+
"location": {
|
113
|
+
"lat": 43.7573013,
|
114
|
+
"lng": -79.0849468
|
115
|
+
},
|
116
|
+
"elevation": 38.9590378
|
117
|
+
}, {
|
118
|
+
"location": {
|
119
|
+
"lat": 43.6523500,
|
120
|
+
"lng": -79.3824000
|
121
|
+
},
|
122
|
+
"elevation": 89.6621323
|
123
|
+
} ]
|
124
|
+
}
|
data/spec/path_spec.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'spec'
|
2
|
+
|
3
|
+
module ServerSideGoogleMaps
|
4
|
+
describe(Path) do
|
5
|
+
describe('#get_elevations') do
|
6
|
+
it('should use the proper URL') do
|
7
|
+
Server.should_receive(:get).with('/maps/api/elevation/json', :query => { :sensor => false, :path => 'abcd', :samples => 20})
|
8
|
+
Path.get_elevations(:path => 'abcd', :samples => 20)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe('#initialize') do
|
13
|
+
it('should not allow a 0-point path') do
|
14
|
+
expect { Path.new([]) }.to(raise_exception(ArgumentError))
|
15
|
+
end
|
16
|
+
|
17
|
+
it('should allow a 1-point path') do
|
18
|
+
pt = [ 45.5086700, -73.5536800 ]
|
19
|
+
Path.new([pt])
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context('with a mocked #get_elevations') do
|
24
|
+
class Parser < HTTParty::Parser
|
25
|
+
public_class_method(:new)
|
26
|
+
end
|
27
|
+
|
28
|
+
before(:each) do
|
29
|
+
Path.stub(:get_elevations) do |params|
|
30
|
+
basename = 'path1-get_elevations-result.txt'
|
31
|
+
data = File.read("#{File.dirname(__FILE__)}/files/#{basename}")
|
32
|
+
parser = Parser.new(data, :json)
|
33
|
+
parser.parse
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe('#elevations') do
|
38
|
+
it('should pass an encoded :path and :samples to #get_elevations') do
|
39
|
+
Path.should_receive(:get_elevations).with(:path => 'enc:elwtGn}|_M~piJnlqb@', :samples => 20)
|
40
|
+
pt1 = [ 45.50867, -73.55368 ]
|
41
|
+
pt2 = [ 43.65235, -79.38240 ]
|
42
|
+
path = Path.new([pt1, pt2])
|
43
|
+
path.elevations(20)
|
44
|
+
end
|
45
|
+
|
46
|
+
it('should return the proper elevations') do
|
47
|
+
pt1 = [ 45.50867, -73.55368 ]
|
48
|
+
pt2 = [ 43.65235, -79.38240 ]
|
49
|
+
path = Path.new([pt1, pt2])
|
50
|
+
elevations = path.elevations(20)
|
51
|
+
elevations.length.should == 20 # the 20 is from the stub file, not the above argument
|
52
|
+
elevations[0].should == 15.3455887
|
53
|
+
elevations[19].should == 89.6621323
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: server-side-google-maps
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 4
|
10
|
+
version: 0.0.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Adam Hooper
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-05-01 00:00:00 -06:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -76,6 +76,7 @@ files:
|
|
76
76
|
- lib/server-side-google-maps.rb
|
77
77
|
- lib/server-side-google-maps/directions.rb
|
78
78
|
- lib/server-side-google-maps/geo_math.rb
|
79
|
+
- lib/server-side-google-maps/path.rb
|
79
80
|
- lib/server-side-google-maps/route.rb
|
80
81
|
- lib/server-side-google-maps/server.rb
|
81
82
|
- lib/server-side-google-maps/version.rb
|
@@ -86,6 +87,8 @@ files:
|
|
86
87
|
- spec/files/directions-Montreal,QC-to-Ottawa,ON-without-overview-polyline.txt
|
87
88
|
- spec/files/directions-Montreal,QC-to-Ottawa,ON.txt
|
88
89
|
- spec/files/directions-Ottawa,ON-to-Toronto,ON.txt
|
90
|
+
- spec/files/path1-get_elevations-result.txt
|
91
|
+
- spec/path_spec.rb
|
89
92
|
- spec/route_spec.rb
|
90
93
|
- spec/spec.rb
|
91
94
|
has_rdoc: true
|