How check if marker is within viewport in Leaflet, given center and zoom level? - leaflet

Is it possible to check whether a point is within viewport using Leaflet?
I have center of viewport and zoom level. Is it possible to calculate boundaries having only those values?

I think you can use getBounds function of map and contains to check if marker is within viewport or not.
if(m.getBounds().contains(marker.getLatLng()))
//within viewport

I have center of viewport and zoom level. Is it possible to calculate boundaries having those values?
No.
One needs the size of the map (e.g. in pixels) in order to calculate the visible bounds of the map viewport.
Think about the edge cases: a map 0 pixels high and 0 pixels wide will never contain a marker within its bounds, whereas a map of infinite size on an infinitely large screen will always contain any marker.

Related

What is the expected size of a single Mapbox Vector Tile in Pixels?

I've been looking for a straightforward answer to this question but can't seem to find one.
The Mapbox Vector Tile Specification states implies that vector tile coordinates are unitless -- they're just in "vector tile grid coordinates" broadly defined by the "extent" -- often 4096.
On the other hand, the Mapbox Style Specification says that properties like line-width are in units of pixels. For instance, at zoom level 20 a minor road may be styled to have a line width of 30px.
To ensure consistent rendering between implementations, I would expect this would mean that a vector tile must have a defined conversion between "vector tile coordinates" and screen pixels (assuming it's being rendered at its native zoom level). I've seen some mention of 256px and/or 512px per tile, but I would expect this to be part of either the Vector Tile or Map Style specification. Changing a parameter like that would significantly affect the appearance of a map.
So: if I'm rendering a zoom-level-0 vector tile at exactly zoom level 0, how many pixels should the tile occupy on screen?
My best guess is that the expected render size is 512px based on this blog article from Mapbox. It's not 100% critical what this render size is, but it's important to at least know it so that you can render exactly to the specification if desired.

Leaflet bounds with padding

I'm Trying to create caching mechanism instead of bringing the whole map.
right now i use:
getBounds() and receive all the points in the area the user is currently viewing.
What i would like to achieve is Padding for the Bounds.
for example:
map.getBounds(50)
should bring the bounds the user currently is viewing X2 the area.. or maybe one zoom level lower area.. but without interrupting the user.
currently fitBounds (JSFiddle Demo Here) does more or less that just resulting in changing the view of the user :( .. I Also tried getting the bounds from the function it self (fitBounds) but it outputs center and zoom not bounds (and i didnt succeed in resulting correct bounds from them)
Have you tried the pad method on the L.LatLngBounds you get from map.getBounds()? It is specifically made to increase the size of some bounds.
Returns bigger bounds created by extending the current bounds by a given percentage in each direction.
For example if you want to increase the size of your bounds by 1 view port in each direction, you would do:
var newBounds = map.getBounds().pad(1)
Now if you want to double the area of the bounds (approximately, not taking projection into account), you would have to increase the total length in each axis by sqrt(2), hence increase the bounds in each direction by sqrt(2) / 2:
var doubleAreaBounds = map.getBounds().pad(Math.sqrt(2) / 2)

How to position a sprite relative to a body

How do I position a Sprite and Text entities relative to the position of a Body on the screen. I tried body.getPosition().x and this gives weird results.I think it may be a bounds issue, as I want only the area of the screen that is shown to be used in the calculations and not the entire map space that's available.
You will get the body position in PIXELTOMETER(PTM) ratio w.r.t physics world.So you have to multiply PTM ratio to the body to get the pixel values as follows
body.getPosition().x*PTM

Make a coordinate plane

How can I make a coordinate plane view controller in Xcode? I've tried to use OPENGL but it hasn't been working. Do I have to draw lines individually? Make it recursively called so that it keeps making a certain amount of lines?
You will want to take into account what you will be doing to the coordinate plan. Can you move the plane? Can you zoom in?
You will want to set up centerLocation and scale variables. centerLocation denotes your center and this is where you will draw a vertical and a horizontal bold line for the axes. scale denotes how many pixels per unit you want to have. You can the default to something like 10. You can use this variable to control zooming.
If you want to use the plane for something like graphing functions, the bare minimum you need is the axes. You can use the scale to find out where "points" are in your grid. Making functions for this is useful.
If you want to draw grid lines, what you can do is simply check the leftmost bound of the grid using the centerLocation.x as an offset and then iterate through the pixels by increments of scale and simply draw a vertical line at each point you come across until you get to the end. Same for horizontal lines, except you can start at the top, offset centerLocation.y, and add a horizontal line in increments of scale until your counter is greater than the height of the screen.

iPhone : MapKit and displaying the current Map Scale

I need to display on the map a scale showing how far a inch / cm is for example. This will need to change depending on the zoom level.
My theory is that if I know the length of the map, and the length of the graphic, If I know what the current scale of the map was I could just do some maths to work out the graphic indicator scale.
So is there a way to get the current zoom lvl in meters? Is it linked to the span or something?
The zoom level is linked to the span - you first need to get the span of your map view, and then convert it into meters.