How to have two worlds in leaflet? - javascript

I have a problem with leaflet when it come to have point near W180. The problem is the same as this one : https://github.com/Leaflet/Leaflet/issues/82
But on marine traffic we can see exactly what I try to achieve.
A continuous world where -180 W will cross this section so I can have a polyline which is not cut and rendered on the other side.
How is it possible since leaflet is able to have greater longitude than 180 ? I've been in the dark on this issue for some time.
I first tried to remove the worldcopyjump and the maxbounds but I don't know how to have two maps at the same time and how to avoid the polyline being cut.
EDIT
I've been looking through the mtMap object on https://www.marinetraffic.com with one boat and there are using this settings :
center: [16.46769474828897, 216.73828125],
maxBounds: [[150,540],[-150,-540]],
My guess is that here are not using negative longitude, so maybe the solution is to have only positive longitude (greater than 180) ?

Related

In Leaflet how can I calculate the distance to the furthest marker from a clicked marker?

My map allows a user to draw circles around a clicked marker. To keep it in bounds of the furthest marker I need to know where that furthest marker is. How do I find the furthest marker from a clicked marker? But to complicate things I need to read the furthest marker title information to make sure it's the correct type of marker. It would have a special 3 letter code in the title to test if it's the correct marker to use. I can only check if it's there not what it says.
The simplest answer was to calculate the furthest distance in all directions at the get go. I did this by first finding a bounding box by calculating the:
$minLat = $rowCorners[minLat]-0.25;
$maxLat = $rowCorners[maxLat]+0.25;
$minLng = $rowCorners[minLng]+0.25;
$maxLng = $rowCorners[maxLng]-0.25;
In this case I gave myself a little room to work in, exact distance were not that critical. By using that information it was easy to draw the circles needed by the users.

leaflet fixed size circle on map

I am using leaflet.js to create few markers and circles. I am using the below given code to draw circles : -
L.circle([ lat, lng ], 1000, {
color : colorCode,
stroke : false,
fillColor : colorCode,
fillOpacity : 0.7
});
Now if I edit this circle on UI and drag this circle vertically downwards, the circle size increases and vice a versa. Similar issue is with calling the above given method with different lat lngs. The same radius (1000) sized circle get plotted with different sizes on map.
My requirement is to place marker with same radius with same size on map everywhere.
I checked L.circleMarker but it takes radius in pixels and also circleMarkers does not scale in zoomin zoomout events. That is why I can't use circleMarkers.
I changed the crs option to 4326 but no success. I am using imageOverlay not tileset. I have created a fiddle.
http://jsfiddle.net/newBee_/88bdrzkr/12/
Try creating a circle on top area then edit and move it downwards. It's size increases. This is what I want to stop. This will resolve the problem of generating circle of same radius via code in different area of map with same size. Please help.
Please suggest.
Edit:
It looks like this is a bug deep into Leaflet 0.x: L.Circle radius computation uses hard-coded Earth projection rather than the specified CRS. Leaflet 1.0 seems to correctly check for the CRS before using the Earth-related computation.
For your case, simply overriding the faulty method seems to fix it, at least visually.
Demo: http://jsfiddle.net/88bdrzkr/13/
The "corrected" method to include in your script:
L.Circle.include({
_getLngRadius: function () {
return this._getLatRadius();
}
});
Regarding iH8's answer, the trick to override L.CRS.Simple.scale is similar to highly zooming (the 256 factor expands the latLng to much further pixels - any high number will do). At high zoom, you are moving your circle along a very short distance, for which the latitude does not change much. So you do not see any visible difference in radius, even though the bug is still there.
Demo of using just higher zoom, no method override at all: http://jsfiddle.net/kau6g8fk/1/
For your need where the circle looks to be more like a visual aid, any of these 3 solutions is enough.
Edit: the CRS is not the issue at all.
Previous message:
If you use Leaflet for indoor mapping, as your jsFiddle suggests (or any flat type map, as opposed to the projection of a sphere like Earth on to a plane), you could simply use L.CRS.Simple
Striked out this faulty solution as pointed out by Ghybs in his answer
Very weird issue, turns out that overloading L.CRS.Simple's scale method to return 256 * Math.pow(2, zoom) fixes this. Here's a fork of your JSFiddle: http://jsfiddle.net/kau6g8fk/ I'm unsure as to the cause of this issue, it would require more research. Will do if i find the time. Found the solution here: http://codepen.io/mike_beweb/pen/BymKGe
The answer below was given before the poster edited his/her question and showed that the used CRS was L.CRS.Simple while i presumed the default CRS. I'll leave it in tact because it might come in handy for some users:
The size change on drag of your L.Circle's is because of your map's default spherical mercator projection (EPSG:3857). Best explained with an image, here's a map with a graticule overlay on every 10 degrees:
Demo on Plunker: Leaflet 0.7.5 EPSG:3857 Spherical
As you move further from the equator every plane becomes higher. Thus your circle automaticly becomes higher the further north/south you drag it. You could use a equirectangular projection (EPSG:4326), in which every plane has the same size regardless of the distance from the equator:
Demo on Plunker: Leaflet 0.7.5 EPSG:4326 Equirectangular
With equirectangle projection you won't have the problem you're having now but you'll have to change your tileset to one with EPSG:4326 projection and those are hard to come by compared to EPSG:3857 tilesets.
If you're not willing or unable to change projection another solution could be to hack around L.CircleMarker and change the radius of your markers depended on current zoomlevel. But that's rather ugly in my opinion.

How to detect or check whether a Poly Line was Inside Another Poly Line or Overlap with Other Poly Line Using Google Maps

I was using Google Maps Javascript API (GWT Maps V3 API) to some poly line in Google Maps. There are some poly line that overlap each other like in this picture:
As we can see from the picture above, there are two poly line blue and purple that overlap each other (they have the same path). Then add a red poly line among those two poly line like this:
Now I need to get all poly line that overlap / intersect with the red poly line. Is there any way I could do this in Google Maps Javascript API? Any comment and answer will be appreciated. Thanks and regards.
You could use geometry library
isLocationOnEdge(point:LatLng, poly:Polygon|Polyline, tolerance?:number)
To determine whether a point falls on or near a polyline, or on or
near the edge of a polygon, pass the point, the polyline/polygon, and
optionally a tolerance value in degrees to
google.maps.geometry.poly.isLocationOnEdge(). The function returns
true if the distance between the point and the closest point on the
line or edge falls within the specified tolerance. The default
tolerance value is 10-9 degrees.

Geolocation api with direction arrow

I am building a simple PHP based off-road navigation webpage for use on a smartphone that will display two icons on a Google map, one being my my current location and one my destination. As I move, the position of the icon for me will automatically update.
Sample code to do the basic stuff includes:This Stackoverflow example and This tutorial.
I would like my icon to be an arrow that points in the direction I am currently walking. The direction would be based on my previous position and my current position. Does anyone know of any method to achieve that please?
One crude method would be to have 8 (or 16) icons, representing N, S, E, W, NE... and pick the icon that approximately matches my direction, but I was hoping for something more dynamic.
The other option is to simply draw a path on the map of where I have been. I am thinking of doing that anyway, but would also like the arrow.
To clarify exactly what I want, This Stackoverflow example contains this code to display a marker of my current position on a map:
map = new google.maps.Map(document.getElementById("map_canvas"), {
zoom: 18,
center: new google.maps.LatLng(startPos.coords.latitude, startPos.coords.longitude),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var marker = new google.maps.Marker({
position: new google.maps.LatLng(startPos.coords.latitude, startPos.coords.longitude),
map: map
});
Instead of the teardrop type marker I want an arrow that is pointing in the direction I am walking. I would calculate the direction for it to point based on the lat/long of my previous location and my current location, but I need the code to insert in the above code where it says position:...
This is basic navigation stuff so I am sure it has been done before. I just haven't been able to find any examples. (I do not want to use Google directions API. It has a usage limit and is not really suited to off-road.)
I haven't tried this myself, but it seems the Google Maps Javascript API provides functionality for this. You can use the google.Maps.Symbol object.
With the rotation property you can set the rotation of the symbol in degrees. You need to get the angle between the current location and destination from the API first then.
With the path property you can set a the symbol. Google already implemented symbols for arrows, they use the constants BACKWARD_CLOSED_ARROW, BACKWARD_OPEN_ARROW, FORWARD_CLOSED_ARROW and FORWARD_OPEN_ARROW.
It also seems to support coloring, scaling, etc. for the symbol, so you can do quite some things with it. I hope this information helped.

google map direction points

I have a gmap with direction line displayed on it (2 or more points). Is there any way to get all points from map depending viewport (zoom level), only those that user can see ?
I won't give you the tea spoon version, but here is how you do it. First of all, make sure you have all your markers available in an array.
Get the bounds from your map: map.getBounds(). This will return a LatLngBounds object, which contains the coordinates of the bottom left point of your viewport as well as the top right part.
Iterate through all your markers. Get their positions: marker.getPosition() and check if they are above and to the right of the map bounds south west point, as well as below and to the left of the map bounds north east point.
If they are, add these points to an array.
That array now contains all the points in your viewport. Repeat as necessary.
More information can be found in the Google Maps API v3 reference

Categories

Resources