📜 ⬆️ ⬇️

Curves on Google Maps

It seems to me that soon Google will give us the opportunity to draw not only a broken line on their maps, but also a curve. And with the need to draw a curved line, I encountered now, in the process of working with one project. And I had to spin.

I usually like very much to read posts like “See what I have done” (especially when it is not a startup) in Habré. So - look what I did - I implemented a dirty hack that will help draw the curves.

First - the picture (not clickable):
')
Sample graphics on the map



Before reading a lot of letters, I want to try!

I would also want to. Go here at this address . There will be a map. Click it once, then click elsewhere, some distance away from the first click. Look at the result. You can click on.

What for?

I usually want to draw a curved line when I draw a pedestrian path on the map. After all, people do not walk through the houses, do not levitate over the rivers and generally love the pavement. Therefore, I always wanted to portray their path curve.

How to set curves?

It seems to force people to learn the theory of Bezier curves, it is not very cool. Therefore, I decided that it is enough to set curves with functions of one variable. I personally love sine and cosine most of all. They rather colorfully depict the path of a drunken pedestrian.

How will they be displayed?

Graphs of functions will be approximated by graphs of polynomials. In Russian: several points on the graph will be selected and connected by segments. The quality of the resulting graphics and the speed of work depend on the number of points.

What does this look like in code?


my_poly = ApproxPolyline(gmap, lls, f, range, points);
map_instance.addOverlay(my_poly.get_polyline());


gmap is an object of class GMap2 (it is needed because it has a method for converting geographic coordinates to Cartesian),
lls - an array of two points that need to connect the graph (objects class GLatLng ),
f - function of one variable that you want to portray,
range - an array of two numbers (display interval),
points - the number of points that will be connected by segments.

Where can I download?

Links
I posted this on GitHub: the repository page and links to archives with the latest revision script: zip and tar .
The size
The source code of the script at the time of publication weighs 3400 bytes, after compression of YUICompressor - 2026 bytes.
License
The code is distributed under the MIT license.

How to help?

There are two ways:
1) Tell me what's wrong. In the comments, in the mail or in any other convenient way.
2) Clone a branch on github, make changes and let me know if you want them to join my branch.

Does he know how to “rob a cow” ?

No, but he also doesn’t know how to replace gnuplot and other programs that draw graphs. Therefore, anyone who wants to draw, for example, the tangent on the interval [-10; 10], just want to say - it will turn out bad, because the reference points are distributed evenly. Because I’m not going to know why drawing graphs of unlimited functions on maps :-)

Although the tangent on the segment [-1; 1] looks like nothing at all - because it is limited and continuous.

[: ||||:]

Oh yeah, maybe someone did it already, but I don't know. I wanted to shake the old days and remember a little bit of mathematics. If this is really not relevant, let me know and I will hide the topic (just teach me how to do it first).

Source: https://habr.com/ru/post/40503/


All Articles