Android Location Bias in autocomplete Rectangular Bounds - java

I want the autocomplete result to show only 1 km from the user location.
there is a way with RectangularBounds:
setLocationBias(RectangularBounds.newInstance(
new LatLng(-33.880490, 151.184363),
new LatLng(-33.858754, 151.229596)))
There are only 2 points here. How does it make a rectangular?
If the user Latlng is for example 32.0000000, 30.0000000 , How can I do a RectangularBounds of 1 km around him?

For the first question if you look at the docs
RectangularBounds newInstance (LatLng southwest, LatLng northeast)
Is rectangular because it represents two coordinates: southwest and northeast, that's usually refered as a Bounding Box (or shortened to bbox) and represents the area inside that two coordinates.
The second question is less simple and there is more than one way of doing it.
One approach is to find out a method to add a distance to a coordinate, this methods are always approximate. For this LatLng geographic coordinates you could use:
public static LatLng getCoordinate(double lat0, double lng0, long dy, long dx) {
double lat = lat0 + (180 / Math.PI) * (dy / 6378137);
double lng = lng0 + (180 / Math.PI) * (dx / 6378137) / Math.cos(lat0);
return new LatLng(lat, lng);
}
where lat0 and long0 are the original coordinates and dx and dy are a distance in meters (as the Earth radius is set in meters 6378137).
And now you can create an aproximate bounding box with the original point +- 1000 meters. Supposing the point you set as starting point is at latitude -33.869622 and longitude 151.206979 the bbox would be:
RectangularBounds.newInstance(
getCoordinate(-33.869622, 151.206979, -1000, -1000),
getCoordinate(-33.869622, 151.206979, 1000, 1000))
That is the bbox you could use for the api call.
In case you have an instance variable named currentlocation this will be:
RectangularBounds.newInstance(
getCoordinate(this.currentlocation.getLatitude(), this.currentlocation.getLongitude(), -1000, -1000),
getCoordinate(this.currentlocation.getLatitude(), this.currentlocation.getLongitude(), 1000, 1000))
If you still need to be more precise with the 1km distance and discard points that are inside that square (bbox) but out of circle of 1km radius you could filter the results by checking the distance to the original point before returning the result or displaying it.
As a curiosity
Another way I could think of is to change the coordinate's projection to another one to make the calculations easier, if you transform the coordinate to mercator adding 1000 to the x value would add 1 km and adding 1000 to the y would also add 1 km as it uses meters as unit, but then you should transform back to the original projection (working with LatLng) as it's the one that api is using so I don't think it's worth it, but it should work as well.

Related

Convert longitude and latitude coordinates for world map image to cordinates (x , y) in pixel [duplicate]

I have a map of world.
I need to create a function that gets two double parameters (longitude and latitude) and that function should draw a small circle on that area in the map image.
I have the following info about the map:
The width of the map in pixels
The height of the map in pixels
** I get to pixel X, Y based on that image.
I tried longitude = -106.346771 and latitude = 56.130366;
mapWidth = 3840.0 and mapHeight = 2160.0 ;
double x = (longitude + 180) / 360 * mapWidth;
double y = (1 - Math.log(Math.tan(Math.toRadians(latitude)) + 1 /
Math.cos(Math.toRadians(latitude))) / Math.PI) / 2 * mapHeight;
result : [lon: -106.346771 lat: 56.130366]: X: 785.6344426666666 Y: 671.2084211650845
but not selected in right location.
enter image description here
Unfortunately, this problem is way harder than it seems.
That map is a Projection, so it is distorted from the original (roughly) spherical coordinate system your data is in.
(actually, your lat & long values may themselve be in one of several different projections, but that only really matters if you are doing very precise measurements.)
So your first order of business is to find out exactly what projection the map is using, then start looking for GIS libraries you can use in Java (I think there are several) to perform the translation.
Decide on a radius, r, for your globe model. Your map will be 2πr pixels wide.
Decide on a a latitude cutoff. You can't project a pole to a plane, and at extreme latitudes things get so distorted as to be unusable. 85 degrees is a common choice.
Express your latitude in radians: lat_r
You X coordinate is r * (lat_r+ π). So at 180° W your X coordinate is r * (-π + π) = 0, and at 180° E your X coordinate is r * (π + π) = 2πr
Express your longitude in radians: lon_r
Your Y coordinate is r * tan(lon_r) * (1 - cos(lon_r)). So at 85° N your Y coordinate is r * tan(1.4835) * (1 - cos(1.4835)) = 10.4r

Calculate the difference in azimuth angles between 2 Poses

I have 2 Pose representing 2 positions of the camera and I want to get the difference between their azimuth angles.
The older pose is retrieved from an anchor set with the older camera pose, so that I shouldn't get errors from updates of ARCore's world understanding.
The newer pose is retrieved from the current frame.
I tried to use this formula from wikipedia:
psi = atan2(
2*(qw*qz + qx*qy),
1-2*(qy*qy + qz*qz)
)
Then I substract the older angle from the newer, with no success: when I move the phone to modify the pitch angle only, the result I get also varies.
I think it didn't work because it assumes +Z to be the vertical axis, whereas +Y is the vertical axis in ARCore. So I rotated the axes in the formula so that the vertical axis is Y :
psi = atan2(
2*(qw*qy + qz*qx),
1-2*(qx*qx + qy*qy)
)
It still doesn't work, the result still varies when I change the pitch only. Apparently this is not the right transformation to do.
How can I calculate the difference in azimuth angle between the 2 poses of the camera ?
This might actually be a question for Mathematics Stack Exchange, but I'm not sure if I'm misunderstanding ARCore or the maths, so here it is.
Use the following approach to calculate azimuth that always measured in two dimensions:
public float getAzimuth(PointF aim) {
float angle = Math.toDegrees(Math.atan2(aim.x - x, aim.y - y));
// the range of ± 90.0° must be corrected...
if(angle < 0.0) {
angle += 360.0;
}
return angle;
}
...the following approach to calculate a distance:
float distance = Math.sqrt((x2 – x1) / 2.0 +
(y2 – y1) / 2.0 +
(z2 – z1) / 2.0);
...and the following approach to calculate a plunge:
float plunge = Math.asin((z2 – z1) / distance)

Converting longitude and latitude coordinates to map pixels (X and Y) the right way

Please take a look at my other question which this was done incorrectly here.
What I need to do
I have an image of a map of my country, I took the piece of the map from Google Maps, so that means I know all of the corner's coordinates in longitude and latitude. My program needs to show up the map, and paint targets on it where each target has its only longitude and latitude, similar to how radar displays targets.
The problem
The problem with my solution is that it's not really using real mathematical formulas to get the X, Y position. It uses simple division and multiple by ratio with minimum and maximum as you can see in my other question.
this is the method:
protected Location getCoordinatesByGlobe(float latitude, float longitude) {
/**
* Work out minimum and maximums, clamp inside map bounds
*/
latitude = Math.max(mapLatitudeMin, Math.min(mapLatitudeMax, latitude));
longitude = Math.max(mapLongitudeMin, Math.min(mapLongitudeMax, longitude));
/**
* We need the distance from 0 or minimum long/lat
*/
float adjLon = longitude - mapLongitudeMin;
float adjLat = latitude - mapLatitudeMin;
float mapLongWidth = mapLongitudeMax - mapLongitudeMin;
float mapLatHeight = mapLatitudeMax - mapLatitudeMin;
float mapWidth = mapImage.getWidth();
float mapHeight = mapImage.getHeight();
float longPixelRatio = mapWidth / mapLongWidth;
float latPixelRatio = mapHeight / mapLatHeight;
int x = Math.round(adjLon * longPixelRatio) - 3;// these are offsets for the target icon that shows.. eedit laterrr #oz
int y = Math.round(adjLat * latPixelRatio) + 3; //
// turn it up
y = (int) (mapHeight - y);
return new Location(x, y);
}
What I have tried
So I was a bit with myself tried to think of something logical, on how can I do this. And I came up with something that doesn't really work exactly:
If we have the corner top-left for example coordinates (Longitude and latitude), and we have the coordinates of the target that we want to display, that means we can do distanceToPoint to know how many kilometers far from the start it is.
After that, we need to know the heading to that point, so we do calculateHeading which gives us the angle to the target point.
So lets call A the starting point (top-left corner)
float aLat = 33.49f;
float aLong = 33.69f;
And our target point we call it b:
float bLat = 32f;
float bLong = 35f;
And then we can calculate the distance from A to B in kilometers:
double km = distanceTopPoint(aLat, aLong, bLat, bLong);
And then we calculate the angle to the point:
double angle = calculateHeading(aLat, aLong, bLat, bLong);
And if we have the km distance and angle, we can know the distance in km for longitude and latitude:
int latDistance = (int) Math.round(km * Math.cos(angle));
int lonDistance = (int) Math.round(km * Math.sin(angle));
So now I probably have the distance from the longitude to the target's longitude and same for latitude. But what can I do with this information?
I tried thinking again, and I figured out that I can know the distance from the left top corner to the right top corner distance in km and same for top left corner to top left bottom corner. And then I can do width / km to get the km per pixel.
But I am really unsure, im sure that I am doing something wrong.
Any ideas?
The Mercator projection is a cylindrical projection, i.e. the generalized coordinates can be calculated as:
a = longitude
b = tan(latitude)
These are unscaled coordinates, i.e. they do not correspond to pixel positions.
Let's say you have an image of w x h pixels that represents the area between (min_long, min_lat) - (max_long, max_lat). These coordinates can be converted to generalized projected coordinates with the above formulas, which yields (min_a, min_b) - (max_a, max_b).
Now you need a linear mapping of the generalized coordinates to pixel positions. This linear mapping can be expressed with four parameters (two scaling parameters and two offset parameters):
x = s_a * a + o_a
y = s_b * b = o_a
Therefore, you need to find the four parameters. You know that the top left corner has pixel coordinates (0, 0) and generalized coordinates (min_a, max_b). Similarly for the bottom right corner. This gives you four constraints and a linear system of equations:
0 = s_a * min_a + o_a
0 = s_b * max_b + o_b
w = s_a * max_a + o_a
h = s_b * min_b + o_b
The solution of this system is:
s_a = w / (max_a - min_a)
o_a = -w * min_a / (max_a - min_a)
s_b = -h / (max_b - min_b)
o_b = h * max_b / (max_b - min_b)
And this is it. If you want the pixel coordinates for some arbitrary point `(long, lat), then do the following:
Calculate the generalized coordinates a and b (be careful to use radians when calculating the tangens).
Use the linear map to convert a and b to pixel coordinates x and y with the pre-calculated parameters.
Inversion
To get latitude and longitude from pixel coordinates, do the following:
Calculate the generalized coordinates:
a = (x - o_a) / s_a
b = (x - o_b) / s_b
Calculate the geo-coordinates:
longitude = a
latitude = arc tan (b)
Again, be careful about radians/degrees.

Calculating distance in pixels between two locations

I'm trying to create a method which calculates the x and y in a grid and eventually the distance between that point and the middle. The problem is, I only know a few values. To explain the case a bit better an image:
(the values between '(..,..)' are lat/long combinations).
As you can see, I know the following values:
start of canvas: xy(0,0)
middle of canvas: xy(540,800) and lat/long(52.3702160, 4.8951680)
max dimension of canvas: x 1080, y 1600
point: xy(?,?) and lat/long(52.4167267, 4.8052174)
point: xy(?,?) and lat/long(52,2422306, 5.1129068)
First, I need to do something to calculate the missing x and y's from the points.
I already tried doing the following:
double mapWidth = screenWidth;
double mapHeight = screenHeight;
// get x value
x = (location.getLongitude()+180)*(mapWidth/360);
// convert from degrees to radians
double latRad = location.getLatitude()*Math.PI/180;
// get y value
double mercN = Math.log(Math.tan((Math.PI/4)+(latRad/2)));
y = (mapHeight/2)-(mapWidth*mercN/(2*Math.PI));
point = new PointF((float)x,(float)y);
This works but I'm getting the wrong x and y values.
For example if my points lat/long are further away the x and y's are getting bigger (more to the middle). But they need to be more at the side because the lat/long point is further away.
All lat/long points inside 2km diameter need to be in my grid, if the point is for example 0.9km away from the center it needs to be nearly at the side.
After that I need to calculate the distance between the two points. I already got that part using the following:
Math.sqrt((point.x - point2.x) * (point.x - point2.x) + (point.y - point2.y) * (point.y - point2.y));
My main problem is calculating the x and y from my lat/long points.
If anyone wants to help, thanks in advance!
I completely rethought my way of calculating the x and y.
I solved it by doing the following:
double distanceMeters = mCurrentLocation.distanceTo(location);
x = ((1000+distanceMeters)*mMiddleCoords.x)/1000; //1000 = radius.
y = ((1000+distanceMeters)*mMiddleCoords.y)/1000;
You can't directly use the distance formula from latitude and longitude. You'll have to take into account the curvature of the sphere to calculate the distance.
The minimum distance between two points on a sphere (and hence earth, simplifying it to a perfect sphere) is the length of the chord on what is called the Great Circle running through those points. A Great Circle is a circle with its center running through the center of the sphere).
From Wikipedia:
C = SQRT(X^2 + Y^2 + Z^2)
where:
X = cos(lat2) * cos(long2) - cos(lat1) * cos(long1)
Y = cos(lat2) * sin(long2) - cos(lat1) * sin(long1)
Z = sin(lat2) - sin(lat1)
And the distance is (2 * R * arcsin(C/2)) where R is the radius of the earth or 6371 km
The other alternative - if you know you will always have the Android libraries - is the Location.distanceTo() method.

Make polygons perpendicular to bearing in google maps android

I'm creating polygons on a map
getMap().addPolygon(
getPolygonOptions(point1, point2, widthInMeters)
.fillColor(Color.YELLOW));
The position of the corners is calculated by:
public static PolygonOptions getPolygonOptions(LatLng point1, LatLng point2, double widthInMeters) {
double distance = SphericalUtil.computeDistanceBetween(point1, point2) + 6;
float bearing = location.getBearing();
double bears = bearing;
LatLng corner1 = SphericalUtil.computeOffset(point2, widthInMeters / 2, bears + 90);
LatLng corner2 = SphericalUtil.computeOffset(point2, widthInMeters / 2, bears - 90);
LatLng corner3 = SphericalUtil.computeOffset(corner2, distance, bears);
LatLng corner4 = SphericalUtil.computeOffset(corner1, distance, bears);
return new PolygonOptions().add(corner1, corner2, corner3, corner4);
}
The intent was for the polygons to be perpendicular to the bearing. Certain directions on the map the polygons are nearly that way, but as you can see in this picture, in some directions the polygons end up slanted compared to the current bearing. The current bearing is shown by the blue arrow and the magenta polyline. Can anyone identify whats wrong with my corner calculations?
It may be problem of units which are returning from two functions.
Here, SphericalUtil.computeDistanceBetween(point1, point2) returns the values in meters.
and
Google's location documentation suggests,
location.getBearing() returns values in degrees.
Get the bearing, in degrees.
Bearing is the horizontal direction of travel of this device, and is not related to the device orientation. It is guaranteed to be in the range (0.0, 360.0] if the device has a bearing.
If this location does not have a bearing then 0.0 is returned.
Please, check whether the location is heaving bearing pr not before using location.getBearing()
So, you have convert those degrees into meters and then implement it in your logic. It will solve your issue.

Categories