How do you determine if LocationX is between LocationA and LocationB? Is there an Android way to determine it?
UPDATE I am asking here if LocationX is on the BuildingA or on the BuildingB. I have the upper left and lower right coordinates of the two buildings.
UPDATE2 Let say LocationX is a person. Main goal is to determine if that person is on the BuildingA or is on the BuildingB.
UPDATE3 Let say that BuildingA and BuildingB are rectangle/square. so we just need upper left and lower right latitude and longitude.
UPDATE4 Let me to add code snippet from @ligi 's response. But the code below is failed.
Location locationA = new Location("A");
locationA.setLatitude(35.70217034224572);
locationA.setLongitude(139.74359443782043);
Location locationB = new Location("B");
locationB.setLatitude(35.69994863045598);
locationB.setLongitude(139.74633029101562);
Location locationX = new Location("X");
// TODO re开发者_如何学编程sult: X:169.661 Y:347.359
locationX.setLatitude(35.70066307001967);
locationX.setLongitude(139.74450102446747);
//distanceAB > (distanceXA + distanceXB - threshold)
float threshold = 0f;
float distanceAB = locationA.distanceTo(locationB);
float distanceXA = locationX.distanceTo(locationA);
float distanceXB = locationX.distanceTo(locationB);
boolean result = distanceAB > (distanceXA + distanceXB - threshold);
assertTrue(result);
calculate 3 distances ( e.g. via Location.distanceTo ) : distanceAB , distanceXA , distanceXB then check the following: (distanceAB == (distanceXA + distanceXB)) and you might introduce a bit of fuzzynes with some threshold bigger than 0 as in the example check - depending on the use case - as in reality things will not be optimal strung
I see that the problem has changed with the updates - the new problem could be addressed e.g. with Rect.contains - just build a Rect for each building and check with Rect.contains if your person is in the building.
A point (a,b) is contained in the rectangle defined by the two points (x0,y0) and (x1,y1) if minimum(x0,x1) < a < maximum(x0,x1) and minimum(y0,y1) < b < maximum(y0,y1).
A point (a,b) is on the line connecting (x0,y0) to (x1,y1) if a - y0 = ((y1-y0)/(x1-x0))*(b - x0), assuming x1 != x0. If x0 does equal x1, (a,b) is on the line if and only if a also equals x0.
The distance between the point (a,b) and the point (x0,y0) is the square root of (a-x0)^2+(b-y0)^2.
精彩评论