Google maps - Near positions - javascript

On my website members are tagging photo position on Google maps API. Longitude and latitude are saved in database (SQL).
Does anyone know how to find tagged photos that are in radius 100km of tagged photo?
Let say that latitude and longitude are 46.03765154061627 | 14.5404052734375. Is there any kind of math formula that would check 100km radius position or any other way?
Thank you!

You could calculate the great-circle distance between two points. Luckily this is relatively easy with the haversine formula, assuming a spherical representation of the earth. You may want to read further and check out the JavaScript implementation at Calculate distance, bearing and more between Latitude/Longitude points by Chris Veness.
If you will only have a handful of photos, you can simply calculate the great-circle distance from the user submitted point to each photo point. Then simply sort the result list by the distance, and filter only the photos with a distance below the 100km threshold.
However, if you will be having many photos, you should probably consider filtering these from the database. You could use a database with geo-spatial indexing capabilities. For example MySQL, PostgreSQL and SQL Server 2008 all have geo-spatial features (either natively or via extensions), which include spatial indexing and implementations of the haversine formula.

Google Maps API includes methods for handling Latitude and Longitude coordinates. If you're dealing with a small number of coordinates it would be easy to use its GLatLng class and call the distanceFrom method.
You can also use the GLatLngBounds, which is designed to see if certain coordinates are within a defined rectangular boundary.

to define a circle of 100km radius may be a bit complex (great circle calculation, distance between two points, etc.) ... it's easier to define a "square shape" of 100km (or 200km) length with your point (M) in the middle:
without being too scientific, asuming
the earth is a sphere (I know it isn't, but ...)
the circumference at aequator is ca. 40.000km - so a 100km portion (of longitude) is aequivalent to 0,9 angle degrees
neglecting the fact that for latitude this is varying the closer you come to the poles
rounding the 0.9 to 1 degree
we can say that you want to search for pictures in an area where its picture coords (P) meet the criteria
lon(M)-1 <= lon(P) <= lon(M)+1
lat(M)-1 <= lat(P) <= lat(M)+1
(all in degrees). I would think that - for a WEB service - this is accurate enough and very easy to implement.

Related

Expanding a radius around a polygon of lat/long for X miles

I'm working on a project which requires me to build a function to expand a polygon of long/lat in a distance of X miles to create a "border".
The technology i am using is JavaScript deskgl / nebula gl layers. And i am unsure whether javascript deskgl/nebula gl has the built in ability to perform these operations. If not, this leads me to question the maths behind this functionality.
As i am unfamiliar with long an lat on a 2d plane, nor whether the stack i am using can deal with this, how do i go about expanding an array of arrays (long, lat) by X number of miles.
You should look into the ScaleMode in Nebula, it allows you to click and then drag a polygon and keeps it's shape. If you need to be able to do that by a specific distance, you could look into their implementation of scaling and you could adapt that.

How can I convert map into 10 mile square tiles and find out which tile lat/long falls in?

This is turning out to be more complicated than I thought... map projections, mercator coordinates, something about EPSG:4326, and so on.
What I want to do.
Given lat, long find which tile they belong to.
Given a tile find its bounds in lat,long.
Tile should be ~ 10 miles long.
I can't simply divide lat/long by some number to get 10 mile tiles as distance between longitude changes from equator to poles.
If I let go notion of 10 mile block and just roll with virtual blocks then it's fine.
Tried Looking at how google maps does it, it didn't make sense.
They basically somehow convert lat/long to 0-256, 0-256. Then take calculate some sort of value called pixel value not sure what is that, coords * 2 ^ zoom level. Then divide it by 256 to get tile number.
if you are working with spheric coordinates you should have a third parameter, the radius.
First you have to do is to mesh your map, plane, square, parametric equation, etc... within the radius you should be able to know the triple integral of your 3D plane.
otherwise take a look at this image:
I will suggest you to use sageMath, it is a python library for mathematical computation very useful to see your mathematical work.
Conclusion: Get the area of the plane, mesh it with a parameter of units ( 1 cm, 1m, 1km, etc...), see if 10 mile tiles fit in your area.

Convert latitude/longitude to pixel if 1 point is known

I have a specific task and after doing some research, I feel a little stuck and confused. Here is my point:
I have a raster image with known dimensions (800 x 800 px)
I know two point inside this image with her pixel AND geographical coordinates eg: (200,200 in pixel represent 20.5,14.57 in geo and 300,300 represent 21.4,16.01) - measured on place with GPS
I need an idea how to calculate X,Y in pixel on other point with geo-coordinates 21.71,15.01
Java Script is my environment.
Before taking care of the programming part, you have to care about transforming the WGS84 coordinate system (used by GPS) into plane grid system coordinates.
That's trigonometry, and depending on the size of the system you will be using, the precision you need, and your location (the earth is not a perfect sphere), you will have to choose a projection, or use an approximation. In France, we generally use the Lambert projection.
Then, using this new local plane grid, you can just scale it to pixels and fill your image.
edit : I found this question on stackoverflow very similar, and the accepted answer is well explained
You need to know the extent of the image. Then, when you know the image is eg. 20 degrees long, you also know that 800 pixels equal 20 degrees.

Distance Between Two GEO Locations [duplicate]

This question already has answers here:
How to find distance from the latitude and longitude of two locations?
(12 answers)
Closed 8 years ago.
I have two GEO locations. How can I calculate the distance between them?
If you use the Google Maps API v3 you can calculate the distance as follows:
Include the Google Maps JavaScript file with the geometry library:
http://maps.google.com/maps/api/js?sensor=true&libraries=geometry
The distance can be measured now by using the computeDistanceBetween() method:
var from = new google.maps.LatLng(49.004, 8.456);
var to = new google.maps.LatLng(49.321, 8.789);
var dist = google.maps.geometry.spherical.computeDistanceBetween(from, to);
This page has JavaScript code for computing the distance of two geographical locations, if that is what you're after.
GPS coordinates are geographical coordinates on the WGS84 spheroid. Under this model, Vincenty's formulae gives results of sub-millimeter accuracy. For most applications, that's either overkill or actually deceptive, as the earth's surface is not modelled by WGS84 to that scale.
You can compare the accurracy of various methods of distance computation on this page (broken link; check the source code instead). As you can see, the spherical and the differential approximations (the latter uses the Pythagorean theorem with the correct local metric) are inaccurate for a lot of cases.
Of the remaining methods, the first one uses the spheroid's mean radius to convert from geographical to geocentrical latitude, whereas the second one uses the cosine rule to get more accurate results in case of 'small' distances (where the definition of 'small' mainly depends on the difference in latitude).
A seperate script containing only these two methods can be found here, which provides a function called distance() and expecting four arguments: the two latitudes, the difference in longitude (all in radians) and a boolean flag indicating whether the distance is 'small'.
It depends on what level of accuracy you want.
You could work it out by basic triangle trigonomoetry - ie work out the difference between their longitude, that's one side; then the diff between their latitude, that's the second. Now you can calculate the third side (ie the actual distance between the two) easily enough with basic junior school maths.
However, that method ignores the curvature of the earth's surface, so if you need to take that into account, you'll need to start getting a bit more clever. But you won't need to worry about that unless the distances are quite large or you need an very high degree of accuracy. For most purposes the basic trig method is fine.
The other point, of course is that these methods give you a straight-line measurement. This may be what you want, but you may also want to know the distance to travel - ie on the road. This is completely different, as you'd need to have an accurate map of all the relevant roads. If this is what you need, it might be easier to delegate to Google's maps service (or one of several other similar alternatives).

map geo coordinates (lat, lng) to map (x, y)

I have the geo-coordinates (latidute & longitude) of some cities and would like to get the x,y coordinates so can plot them into a map.
The map is a standart one, just like http://www.wordtravels.com/images/map/Spain/Fuerteventura_map.jpg for example.
I tried several formular I found, but none seems to really work :(. Simple javascript code or ruby would be best :)
There are many ways to approach this problem with varying degrees of precision. However, they all boil down to performing a projection that corresponds with that of your map.
If you know that your map is of the Mercator projection variety, then the lat/long coordinates can simply be treated as X/Y, scaled and translated appropriately. That is, you would find a simple ax+b and cy+d that do the job.
If your map is not Mercator-projection (as it probably isn't if it tries to get the scale consistent, as this one appears to do) then your best bet is to assume it's an "earth-tangent" projection. (This works out OK for small landmasses.) In that case, you need to first project the Lat/Long into a three-dimensional coordinate system.
z=sin(lat)
x=cos(lat)*sin(long)
y=cos(lat)*cos(long)
Positive z points to the north pole. Positive y points to 0, 0, and positive x points to lat 0 long 90 (east) and positive lat/long are north and east. Of course you must convert to radians first.
All of this assumes a spherical Earth, which isn't exactly true but it's close enough unless you're firing long-range mortar rounds.
Anyway, once you have your XYZ, then you'll need to rotate and scale for the map. To rotate around the Z axis, just subtract the base longitude before you project into three dimensions. Do this to center your map on zero-longitude for easiest math.
Once you've done this, you'll only need to rotate the globe forward until your original map is face-front. Do this with a 2-d rotation in the y-z axis. Use http://en.wikipedia.org/wiki/Coordinate_rotations_and_reflections to figure that part out.
Finally, your x,z coordinates are going to line up pretty well with your map's x,y coordinates, for an appropriate scale/translate as described earlier.
in addition to the above answers, there is the open source proj4js library which performs transforms between various map projections. it is used internally by OpenLayers for this kind of thing, and supports a number of popular projections and coordinate systems.
perhaps this will help, i've done a implementation for the US using just javascript.
demo/code: http://the55.net/_11/sketch/us_map
Use the Google Maps API, you can overlay your jpg on the map and position it correctly.
Here is an example
http://code.google.com/apis/maps/documentation/javascript/examples/overlay-hideshow.html
and here is the api page about overlays
http://code.google.com/apis/maps/documentation/javascript/overlays.html
You won't find it easy unless you're working on a very small scale (and close to the Equator). Wikipedia's Geographic coordinate system is a good start...
The easier path could be to make use of something like web mapping and stick with your latitudes and longitudes.

Categories