I can use GPS to get my current location, I'd like to retrieve all the POIs which within my circle region.
The center point of circle is my current location, radius of circle region is x.
POI item is stored in database with its location and other information.
Which solution is the best way to retrieve all the POIs which within my circle.
Any suggestion?
Thanks,
Considering that you've retrieved all of your location points, retrieved your location using LocationManager and wish to check which of those lie inside of your circle area, this may be one way to achieve your goal:
int RADIUS;
ArrayList<Point> points;
Location myLocation;
for(Point point : points){
Location pointLocation = Location("");
pointLocation.setLatitude(point.x); // assume x values are latitudes
pointLocation.setLongitude(point.y); // assume y values are longitudes
if(myLocation.distanceTo(Location pointLocation) < RADIUS){
// This point lies inside
}
}
Another, less Android-ish way would be to check if your data points satisfy the equation:
(myLocation.getLatitude() - pointLocation.getLatitude()) ^ 2 + (myLocation.getLongitude()) - pointLocation.getLongitude()) ^ 2 <= RADIUS ^ 2
Since this equation presupposes that the Earth surface is flat, the result would contain error, magnitude of which would grow with the RADIUS value.
Related
Suppose we have two beacons placed in both sides of the road. We know their latitude and longitude where they are positioned (we treat them as a location). We also know the distance in meters between these two beacons (measered using Haversine Formula). Our device is moving between(inside range of these beacons) these two points.Is there out any function that will help us calculate our current position based on the distance between two beacons or based on the distance from device to a single beacon?
How can i find the location of the device based on these data i have, or is there any thing that will be useful to achieve what i want?
SHORTLY: I want to know where the user is located between two BEACONS without using GPS System but the data i have from the beacon (in this case: Exact Beacons locations, exact distance from user to the beacon and the exact distance between two beacons)
As an illustration:
(Filled Black dots are BEACONS with an imaginary Range, Red dots are some user unknown positions and Green Lines are the Known Distances ; we also know the latitude and longitude of Black Dots) Based on these data i want to find the position of user (Single Red Dot)
note: I checked out this question however i didnt understand why the location is returned as an int and why time t is included there.
Here the Methods to set the locations and get the distance from where you are standing between these two point.
private Location BeaconLocation1() {
Location location = new Location("POINT_LOCATION1");
location.setLatitude(45.0);
location.setLongitude(45.0);
return location;
}
private Location BeaconLocation2() {
Location location = new Location("POINT_LOCATION2");
location.setLatitude(45.5);
location.setLongitude(45.5);
return location;
}
public class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
Location pointLocation1 = BeaconLocation1();
Location pointLocation2 = BeaconLocation2();
float distance1 = location.distanceTo(pointLocation1);
float distance2 = location.distanceTo(pointLocation2);
Toast.makeText(MapsActivity.this,
"Distance from Point1: "+distance1+" Meters", Toast.LENGTH_LONG).show();
Toast.makeText(MapsActivity.this,
"Distance from Point2: "+distance2+" Meters", Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String s, int i, Bundle b) {
}
public void onProviderDisabled(String s) {
}
public void onProviderEnabled(String s) {
}
}
EDIT :
see this post
convert meters to latitude longitude from any point
To focus on your question, no there is no one-shot formula to do everything but there are some to make the process easier. You will have to look around in a whole bunch of math libraries to find them though.
This is the theory behind it.
Okay so we require the latitude and longitude of point 3. I'm going to explain the theory behind it as the code is just too much for me to do right now.
For this we will make use of the bearing from one point to another. You can use this link to get that formula: Bearing formula
I am assuming we know the distance between the device and each of the separate beacons otherwise this is impossible unless you want to use a sort of radar approach to identify this. I can't really help with that. If we do know this we can construct imaginary circles around the two beacons using the distance from them to the device as their radii.
E.g. From device to beacon one is 500 meters. From device to beacon 2 is 200M. Draw an imaginary circle around beacon one with a raduis of 500M and a imaginary circle around beacon two with a radius of 200M.
obviously we can't construct these circles programatically it would be too tedious. So we will use the equation of a circle: (x -h)^2 + (y - k)^2 =r^2.
Quick high school revision reveals that
h and k are the x and y coordinates of the center of the circle, an offset from the Cartesian plain. we will center our imaginary Cartesian plain at point one. Now we will workout the bearing to point two an use the distance from point to to construct a line from 0,0 on our Cartesian plane(Point 1) to point 2. Using tan of our bearing we get the gradient of the line. Now we will use cos and sine of our bearing and using the distance of our hypotenuse to obtain the y and x position of beacon 2 relative to beacon 1 on our Cartesian plane. Now we will sub these values back into our circle equations:
your first equation will always be:
(x -0)^2 + (y - 0)^2 =r^2. Where r is the radius in this example 500M.
second equation is
(x -h)^2 + (y - k)^2 =r^2.Where r is the radius in this example 200M. Except now h will be your calculated x value above and y will be the calculated y value above.
Now for the tricky party. We need to find where these circle intersect. From our buddies at math stack exchange i got the formula.Points of intersection
Now your circles might have zero 1 or two point of intersection based on the devices position. If you get two results you will have to run the entire thing again while the device is moving to see whether we are moving closer or further away from the centers of the circles. From that we can conclude which side is the correct point and the we can dram a straight line to beacon 1 on our Cartesian plain, obtain the gradient of the line, convert it into a bearing from beacon 1 and then reverse engineer the haversine and bearing formula to get the co-ordinates.
It's not pretty or easy but you'll get there eventually.
This is most likely not the only solution so feel free to go searching for other ones.
Best of luck.
I am developing an application that uses google map to navigate the user to a given location. I want to orient the map so the destination point always be at the top of the map and the current location marker to be at the bottom and both markers to be vertically aligned and centered in the map (e.g the vertical line between them to be perpendicular to the screen)
My approach is to find the mid point of the markers and than to calculate the bearing to rotate the map so both markers end up vertically aligned and centered. Centering the mid point will center the markers but I can't calculate the correct value for the bearing.
Any help will be appreciated.
EDIT:
I've tried Location.bearingTo and Location.distanceBetween. For the same input they return different values and the one returned from Location.distanceBetween is what i am looking for.
EDIT2 (Code example):
public static void positionMap(GoogleMap map, LatLng start, LatLng end) {
// zoom the map so both locations are visible
LatLngBounds bounds = LatLngBounds.builder().include(start).include(end).build();
map.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 200));
// find the bearing
float[] results = new float[3];
Location.distanceBetween(
start.latitude,
start.longitude,
end.latitude,
end.longitude,
results);
float bearing = results[2];
// position the map so the two markers are vertically aligned
CameraPosition position = map.getCameraPosition();
CameraPosition cameraPosition = new CameraPosition.Builder(position)
.target(Utils.median(start, end))
.bearing(bearing)
.build();
map.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
Take a look the the distanceBetween method of a location object.
Here is the doc. The initial bearing is the bearing you need to use from the starting point, the final bearing is the bearing you will be on when you reach the destination. I think you would be interested in the initial bearing.
public static void distanceBetween (double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results)
Added in API level 1
Computes the approximate distance in meters between two locations, and optionally the initial and final bearings of the shortest path between them. Distance and bearing are defined using the WGS84 ellipsoid.
The computed distance is stored in results[0]. If results has length 2 or greater, the initial bearing is stored in results[1]. If results has length 3 or greater, the final bearing is stored in results[2].
Parameters
startLatitude the starting latitude
startLongitude the starting longitude
endLatitude the ending latitude
endLongitude the ending longitude
results an array of floats to hold the results
Throws
IllegalArgumentException if results is null or has length < 1
You could try using Location.bearingTo().
Unfortunately this requires converting from LatLng to Location, but that should be simple enough.
first what I want to achive, than I think I can do it
I want to find streets on the edge of area where center point is point where I am standing.
Now for example I'm selecting area with 2km radius. I want to find 360 points (1 point for each degree) and check if point with this coordinates is street or not. Now 3 questions
How to get each point in distance from me
How to get latitude and longitude of this point
How to check if this point is street or not
How to get each point in distance from me
Having the Lat and Lng of source and destination points, you can use Location.distanceBetween method.
float[] results = new float[3];
Location.distanceBetween(srcLat, srcLng, destLat, destLng, results);
The distance between the two points is in index 0 : results[0]
How to get latitude and longitude of this point
Here is a link
How to check if this point is street or not
Here is another link.
Hope this help.
i have punch of GPS coordinates (latitude longitude) i want to create groups each one will contain coordinates that in the same radius , iam using this code to get distance between 2 points:-
float radius = (float) 1000.0;
float distance = loc.distanceTo(loc2);
if (distance < radius)
Toast.makeText(getApplicationContext(), "inside", Toast.LENGTH_LONG).show();
}
but with this code i need to compare each coordinate with the rest to check which one is closest ,which seem insufficient , is there any other way?
thanks in advance
The other way is to use a geo spatial index, like quad tree.
In that case you would first calculate which quad cells are within the radius, and then you only consider the points inside that cells.
But this is some programming, search and understanding effort, so use such an index when a brute force search over all points is to slow.
I am very new to Android and learning through it. So i want to create a arrow like pointer to show where is the fixed location is fixed, as the users move their device, the arrow should automatically updated(like compass).
I have googled for resources but i couldn't manage to find it. Anyone here knows how to do it? any examples or tutorials would be much appreciated.
Example:
fixed location: 20 latitude, 50 longitude
my location: 5 latitude 25, longitude
So how do i find the arrow pointed to fixed location?
If curLat and curLon are the current location's latitude and longitude and if refLat and refLon are the reference location latitude and longitude you may get the bearing from the current location to the reference location like this:
final float[] results= new float[3];
// The computed distance in meters is stored in results[0].
// If results has length 2 or greater, the initial bearing is stored in results[1].
// If results has length 3 or greater, the final bearing is stored in results[2].
Location.distanceBetween(curLat, curLon, refLat, refLong, results);
final float bearing = results[1];
If the distance grows result[2] differ more and more from results[1] unless the the course from the current location to the reference location follows a rhumb line (or loxodrome, see here: http://en.wikipedia.org/wiki/Rhumb_line).