Polylines going off the map - google-maps

I'm trying to show a broad view of the globe with several markers scattered with polylines connecting them. My map's view is locked so one can't rotate/zoom/pan/move or anything like that. Look at this screenshot:
http://gyazo.com/b81336ff514d76e95ac721aa5381d6c1
The polylines merely take the shortest route which in this case, involves going off the map. I want the lines to all go across the center of the map. Is there a way to do this within the API?

To detect it you must take a look at the longitudes of the 2 points of the path.
When one longitude is greater than 0 and the other is smaller than 0 and the difference between both longitudes is greater than 180 you must add an additional point in the middle of the path.
Sample-code:
//path is an array of 2 LatLng's
if(
((path[0].lng()>=0)!=(path[1].lng()>=0))
&&
(Math.abs(path[0].lng())+Math.abs(path[1].lng())>=180)){
console.log(123)
path=[ path[0],
new google.maps.LatLng((path[0].lat()+path[1].lat())/2,
(path[0].lng()+path[1].lng())/2),
path[1]]
}

Related

Get total degrees latitude/longitude displayed on a Google map when world wraps- Javascript v3 API

I am writing some code which clusters markers on a Google map. The clustering algorithm relies on knowing how many degrees latitude and longitude are currently visible to the user, as I break the map into a grid of n/map_pixels_width x n/map_pixels_height squares and need to know how many degrees of lat/lon are in each square to know which square each marker point belongs to.
Under normal circumstances where the map does not wrap this is relatively easy to calculate using getBounds() on the Google Map object and doing the calculations to figure out the latitudinal and longitudinal distances between the returned North-East and Sout-West points. Where I'm running into issues is where the map is zoomed out to the extent that it wraps the entire Earth > 1 times. For example, I can zoom out the map so that the entire Earth is "tiled" 5 times over which equates to 360 * 5 = 1800 longitudinal degrees and, but then the call to getBounds() no longer provides useful information:
m.getBounds().getNorthEast().lat()
88.31833020528785
m.getBounds().getNorthEast().lng()
180
m.getBounds().getSouthWest().lat()
-88.5485785544835
m.getBounds().getSouthWest().lng()
-180
Basically, the longitudes getBounds() reports are just the min and max for one whole globe which says nothing about how many times the Earth is repeated. Although Google Maps doesn't tile the map vertically (it just inserts gray filing space if zoomed out far enough), I have conceptually the same problem -- I need to know how many total degrees of space the vertical area would consume.
Is there a way to get the total number of visible longitudinal degrees?
So based on this answer to another question, I found a (hackish) way to solve this. Basically, the Google Maps OverlayView class has a getProjection() method returning a MapCanvasProjection object, which in turn has a getWorldWidth() method which returns the width of the world at the current zoom level in pixels. So the way to solve the problem then is to:
Add a dummy OverlayView to the map that doesn't actually present an overlay.
Get the overlay's projection.
Get the world width from the projection.
Calculate the number of visible longitudinal degrees as pixel_width_of_map_element / world_width_in_pixels * 360
It would be better if there were a way to do this without creating a dummy overlay, but this method seems to work.

algorith or formula to get a set of lat lon values based on viewport and zoomlevel

I need to display around 50,000 markers on the map. But i was able to plot only 10000 points. I thought of implementing this way correct me if i'm wrong...
Instead of fetching whole data at once just fetch points that are in the viewport and depending on the zoomlevel.
ex:google maps: at one zoomlevel only states are shows if we zoom in further cities are shown
I'm stuck with the zoomlevel.. how to relate zoomlevel and viewport.Is there any algorithm or formulae that helps in getting the lat long values or it needs to be hardcoded in the database like for particular lat-lon this is the zoomlevel range so while fetching range is checked.
i'm using openlayers bbox feature to get the bounds
Thanx in advance
google.maps.Map.getBounds() will return the lat/long bounds of the viewport.
50,000 markers is a lot compared to what Google Maps can handle; you would have to be way zoomed in to have few enough markers to be under the limits. You might do better by creating custom tiles with dots instead of markers. You can see an example at
http://maps.webfoot.com/demos/election2008/
Scroll down to the third overlay to see dots; select zip codes to see LOTS of dots.

How to place markers every "x" meters on all street inside a specific area?

I want to create a map that will work on local pc that will create markers on all streets and roads inside an area that I have to draw manually.
The idea is that the markers need to be separated by each others of a fixed value (for ex: 10 meters).
I cannot find anything in the documentation or in this forum.
Thanks.

Around-the-world path breaking in Google Static Maps

I'm creating Google Static Maps with a path between markers and have come across certain coordinate combinations that break the path.
The path breaks in this example (markers # Beijing, SF, NY, Azores, Rome):
http://maps.google.com/maps/api/staticmap?path=color:0xff0000cc|weight:3|39.904214,116.407413|37.77493,-122.419416|40.714353,-74.005973|38.721642,-27.220577|41.89052,12.494249&markers=39.904214,116.407413|37.77493,-122.419416|40.714353,-74.005973|38.721642,-27.220577|41.89052,12.494249&maptype=terrain&sensor=false&size=640x404
And works in this example (marker # Rome removed)
http://maps.google.com/maps/api/staticmap?path=color:0xff0000cc|weight:3|39.904214,116.407413|37.77493,-122.419416|40.714353,-74.005973|38.721642,-27.220577&markers=39.904214,116.407413|37.77493,-122.419416|40.714353,-74.005973|38.721642,-27.220577&maptype=terrain&sensor=false&size=640x404
The cause is not crossing over the Greenwich Meridian as I initially thought. It breaks even if I add a marker before that, seemingly any a point East of the automatically calculated centerpoint of the map...but only when the path starts on the other side of -180 longitude.
To prove that, you can see that the Beijing, SF, NY, Azores, Rome path displays correctly when I set the map centerpoint to -170,35
http://maps.google.com/maps/api/staticmap?path=color:0xff0000cc|weight:3|39.904214,116.407413|37.77493,-122.419416|40.714353,-74.005973|38.721642,-27.220577|41.89052,12.494249&markers=39.904214,116.407413|37.77493,-122.419416|40.714353,-74.005973|38.721642,-27.220577|41.89052,12.494249&maptype=terrain&sensor=false&size=640x404&center=-170,35
Unfortunately, I can't programmatically set center like this because I never know the collection of points I'm going to get, and it would be next to impossible to detect that the passed points would cause a line break.
Any ideas?
First: you can calculate the center even if you don't know the collection of points (locations) at compile time: at runtime you know all the points (otherwise it would be not possible to insert the coordinates into the URL), therefore you can easily iterate over all the points and compute the center. You can computing the average of all coordinates and you get the geometric center (also called centroid): this is the easier way. Another way to do is to check the most distant pair of locations (for both, latitude and longitude) and then set the middle point as center: this requires slightly more coding (for instance to compute the longitude distance you need to take into account the you have to compute the distance in two directions, since it is possible to go 'around the world) and it has an higher complexity.
I don't go deeper in this topic because, even if you compute correctly the center of your points, this does NOT resolve the problem, and moreover the center provided automatically by the Google static map API is always correct: again the center is related to the problem, but it is not the (couse of the) problem.
An aspect that is trivial but important to keep in mind: Google static map draws a path between two locations always by considering the shortest path, i.e. by drawing the shortest straight line.
Therefore if you are in a situation where your path has to go from a location A to a location B, and the shortest path between A and B goes 'around the world' (or better, it goes out from one side of the image), then the path appears 'borken' as in maps that you have shown. In practice A and B are near the left and right margins of the map, and the map can not be centered in some point along the shortest path between A and B because of the others point of the path. And this is what happen when you remove 'Rome': without Rome the map can be centered in a way that the path is not borken.
Formally, I think that the problem appears (i.e. the path is broken), when the projection of the path on the equator is longer than 360 degree of longitudes, and the path always goes in the same direction (i.e. always west to east or always east to west).
Google static map in this situation simply adds another world map next to the first one: if you set the zoom to the minimum, you can see up to three world maps. This is really impractical for several reasons:
you can not zoom out infinitely;
if you want also insert markers into the map they will be drawn only on the 'main' map;
potentially you can have a path that goes around the world (passing in sequence from America, Europe, Asia, and America again and so on...) many times, and it would be really terrible to have so many little maps one after each other;
I googled a lot about this problem, and I didn't find any solution, there is a bug open on the bug tracker, but it is unsolved.
In my opinion the 'right' way to do is simply the following: at most one map and if a path has to goes out from one margin of the map/image, then it should appear on the opposite margin and continues to the destination, drawn on the same map.
So I found a first workaround:
you draw your path, and with the same style (line's color, etc) you also draw the path in a reversed way (Google static map allows to draws multiple paths in the same map), i.e. path=A|B|C&path=C|B|A and this will solve the problem in many situations (i.e. the path exits from one side of the image and enters from the other). Unfortunately this not works always: if you have a path that cross the image margin two consecutive times, then you lose a portion of your path
To solve this problem I found a second workaround:
- not draw simply the path and its reverse, but draw a different path for every pair of locations of the path (and reversed), i.e. for a path A->B->C then: path:A|B,path=B|C,path=C|B,path=B|A and this works always
The drawback is that in this way the URL becomes very long and the limit of 2048 characters for URLs is easily reached.
The best solution would be to compute manually the center, check manually where the path will cross the margin, and only for this portion of the path draw an additional path going between the two locations at the margin (and maybe also the reverse), but I do not think it really worth, although I do not think Google ever will solve this problem.

Atlas style map index for static google map

I'm using a static google map, but really this problem could apply to any maps project. I want to divide a map into multiple quadrants (of say 50x50 pixels) and label the columns as A, B, C.... and the rows as 1, 2, 3...
Next I plan to do something like,
1) Find the markers which are the farthest north, east, south, and west
2) Use that info to to define the bounding boxes of each row and column box
3) Classify each marker by its row and column (Example Marker 1 = [A,2])
A few requirements,
I don't know the zoom level because I let Google set the zoom level appropriately for me and I would rather not use an algorithm that is dependent on a zoom level. I do however know the locations of all of the markers that are shown on the map.
Here is an example of a map that I would like to classify the markers for,
static map example link.
I found these which look like a good start,
Resource 1, Resource 2
But I think I'm still in need of some help getting started. Can anyone help write out some pseudo code or post a few more resources? I'm kind of in a rut at the moment.
Thanks! Much appreciated of any help!
Ok two days into this I finally got it. Thought I would share my thoughts with people who stumble upon this later.
Following the PHP code on this site and for translating lat,lng pairs to pixel coordinates, I was able to classify the individual pixel row by the x value and the column by the pixels y value.
To calculate the zoom level, I determine the maxLat, maxLng, minLat, and minLng values defined by the collection of markers. Then I calculated the bounds of the map at a given zoom level. Finally I used a brute force method of checking if the new bounds of the map determined by the zoom level would include the bounds defined by the max,min lat,lng values of the collection of markers. Starting at zoom level 21 (max zoom on google maps) I decrement the zoom level until I find a zoom level that includes all the markers.
It seems, that the zoom level that is calculated in this method matches Google's preset zoom level selected automatically if you do not provide a zoom level for a static map.
In PHP there is a nice library to do all of this here.