Sorry, a comment I attempted to post on John-Scott's blog (of had a bunch of the code removed from it. Just posting it here so it's not lost.
Hey John,
So I was out of town for the weekend (without internet access even!!).
But by trimming I simply mean that I'm taking a subset of coordinates from the complete trail (say 20 points vs 400+).
What I'm doing is:
Importing a gpx file (from a GPS device)
Parsing the xml and geocoding the coordinate pairs
From there I associate all geocoordinates related to a specific path (i.e. gpx file) through my object: Path.
I then pass the Path into my own method (EncodedPath) that creates a string of all lat/long pairs in the following style: "latitude1, longitude1\nlatitude2, ... longitude_n". Once the string is created, it's passed into your EncodedPolyline class and the results for encoded_points and encoded_levels are stored in the Path object/database.
The gmap overlaying is done by looping through the existing paths and creating overlays for each. This is the point at which I pass in the the encoded polyline data from your lib.
Here's the class I wrote to interact with yours (sorry if i can't get the code to highlight):
Path is an object structured so with a group/grouping of geocodes (as specified by the geocode's path_id):
The GPX file itself is one I'm grabbing from motionbased (for example:
And here's the portion of my controller that outputs the JS for the map:
I'm not sure this info is helpful at all -- anyway, below are some samples of the string Im passing your code (passing a trimmed one so it doesn't take up the whole page) and the encoded data thats returned/stored.
Passing the above into googles polyline encoder utility will map the points (but not as a line). An error also appears on the page.
Passing in encoded values from a full listing of points (i.e. all coordinates from the gpx file) and the website tells you that the number of points and levels are mismatched. Maybe this is a problem with the datatypes I'm using?
Cool. sorry for the long, maybe difficult to read comment. It sounds like you have it working well on your end, however, so maybe I've just gone about using it wrong <crosses fingers> ; )
Thanks again,
m
Hey John,
So I was out of town for the weekend (without internet access even!!).
But by trimming I simply mean that I'm taking a subset of coordinates from the complete trail (say 20 points vs 400+).
What I'm doing is:
Importing a gpx file (from a GPS device)
Parsing the xml and geocoding the coordinate pairs
From there I associate all geocoordinates related to a specific path (i.e. gpx file) through my object: Path.
I then pass the Path into my own method (EncodedPath) that creates a string of all lat/long pairs in the following style: "latitude1, longitude1\nlatitude2, ... longitude_n". Once the string is created, it's passed into your EncodedPolyline class and the results for encoded_points and encoded_levels are stored in the Path object/database.
The gmap overlaying is done by looping through the existing paths and creating overlays for each. This is the point at which I pass in the the encoded polyline data from your lib.
Here's the class I wrote to interact with yours (sorry if i can't get the code to highlight):
Code:
class EncodedPath
attr_reader :string
def initialize(path)
@string = ""
encodePath(path)
end
def encodePath(path)
path.geocodes.each { |g|
@string << "#{g.latitude}, #{g.longitude}\n"
}
@string = @string.chomp
encoded = EncodedPolyline.new(self.string)
path.encoded_points = encoded.encoded_points
path.encoded_levels = encoded.encoded_levels
end
end
Path is an object structured so with a group/grouping of geocodes (as specified by the geocode's path_id):
Code:
g = Geocode.new
=> #<Geocode:0x12c6568 @attributes={"country_code"=>nil, "latitude"=>nil, "postal_code"=>nil, "thoroughfare"=>nil, "administrative_area"=>nil, "time"=>nil, "altitude"=>nil, "path_id"=>nil, "speed"=>nil, "dependent_locality"=>nil, "locality"=>nil, "longitude"=>nil, "course"=>nil, "path_order"=>nil, "sub_administrative_area"=>nil, "address"=>nil}, @new_record=true>
>> p = Path.new
=> #<Path:0x12bd684 @attributes={"encoded_levels"=>nil, "encoded_points"=>nil, "trail_id"=>nil}, @new_record=true>
The GPX file itself is one I'm grabbing from motionbased (for example:
And here's the portion of my controller that outputs the JS for the map:
Code:
@trails.each { |t|
t.paths.each { |path|
i = 0
lastpoint = Geocode.new
size = path.geocodes.size - 1
# error occurs here -- possibly because points arent matching the number of levels?
jsCode = <<-END_OF_STRING
var encodedPolyline = new GPolyline.fromEncoded({
color: "#FF0000",
weight: 10,
points: "#{path.encoded_points}",
levels: "#{path.encoded_levels}",
zoomFactor: 32,
numLevels: 4
});
map.addOverlay(encodedPolyline);
END_OF_STRING
@map.record_init jsCode # record_init passes javascript code through
I'm not sure this info is helpful at all -- anyway, below are some samples of the string Im passing your code (passing a trimmed one so it doesn't take up the whole page) and the encoded data thats returned/stored.
Code:
>> EncodedPath.new(p)
=> #<EncodedPath:0x3468e28 @string="37.5556, -122.071\n37.5556, -122.071\n37.5555, -122.071\n37.5553, -122.072\n37.5552, -122.072\n37.5551, -122.072\n37.555, -122.072\n37.555, -122.073\n37.5548, -122.073\n37.5547, -122.073\n37.5547, -122.073\n37.5544, -122.074\n37.5544, -122.074\n37.5543, -122.074\n37.5542, -122.075">
>> p.encoded_points
=> "oafdFv~`hV??R?f@fER?R?R??fEf@?T???x@fE??R?RfE"
>> p.encoded_levels
=> "P?ADABCEBC?F?AP"
Passing the above into googles polyline encoder utility will map the points (but not as a line). An error also appears on the page.
Passing in encoded values from a full listing of points (i.e. all coordinates from the gpx file) and the website tells you that the number of points and levels are mismatched. Maybe this is a problem with the datatypes I'm using?
Cool. sorry for the long, maybe difficult to read comment. It sounds like you have it working well on your end, however, so maybe I've just gone about using it wrong <crosses fingers> ; )
Thanks again,
m