希望有人可以解释我的问题。 我有9组地理点数据。有没有办法决定/选择哪个是我当前GPS位置最近的地理位置(p1到p9)?
public class LocationCalc extends Activity {
GeoPoint p1, p2, p3, p4, p5, p6, p7, p8, p9;
float result;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location_calc);
//<---GPS DATA starts here
String coordinates[] = {"2.976720", "101.7340622","2.9739549","101.7302322","2.9708705", "101.7306824","2.9700341", "101.7316818", "2.9681962", "101.7278519", "2.96585", "101.7311020", "2.9625874", "101.7255707", "2.9755125", "101.7287445", "2.9779000", "101.7305298" };
float lat0 = Float.parseFloat(coordinates[0]);
float lng0 = Float.parseFloat(coordinates[1]);
float lat1 = Float.parseFloat(coordinates[2]);
float lng1 = Float.parseFloat(coordinates[3]);
float lat2 = Float.parseFloat(coordinates[4]);
float lng2 = Float.parseFloat(coordinates[5]);
float lat3 = Float.parseFloat(coordinates[6]);
float lng3 = Float.parseFloat(coordinates[7]);
float lat4 = Float.parseFloat(coordinates[8]);
float lng4 = Float.parseFloat(coordinates[9]);
float lat5 = Float.parseFloat(coordinates[10]);
float lng5 = Float.parseFloat(coordinates[11]);
float lat6 = Float.parseFloat(coordinates[12]);
float lng6 = Float.parseFloat(coordinates[13]);
float lat7 = Float.parseFloat(coordinates[14]);
float lng7 = Float.parseFloat(coordinates[15]);
float lat8 = Float.parseFloat(coordinates[16]);
float lng8 = Float.parseFloat(coordinates[17]);
p1 = new GeoPoint(
(int)(lat0*1E6),
(int)(lng0*1E6));
p2 = new GeoPoint(
(int)(lat1*1E6),
(int)(lng1*1E6));
p3 = new GeoPoint(
(int)(lat2*1E6),
(int)(lng2*1E6));
p4 = new GeoPoint(
(int)(lat3*1E6),
(int)(lng3*1E6));
p5 = new GeoPoint(
(int)(lat4*1E6),
(int)(lng4*1E6));
p6 = new GeoPoint(
(int)(lat5*1E6),
(int)(lng5*1E6));
p7 = new GeoPoint(
(int)(lat6*1E6),
(int)(lng6*1E6));
p8 = new GeoPoint(
(int)(lat7*1E6),
(int)(lng7*1E6));
p9 = new GeoPoint(
(int)(lat8*1E6),
(int)(lng8*1E6));
String[] myString = new String[17]; //create array
for (int number = 0; number < myString.length; number++) {
float[] result = new float[17];
Location loc = null;
Location.distanceBetween(loc.getLatitude(), loc.getLongitude(), lat[], lng[], result);
//<---- I'm stucked at this part where I'm not sure how to use For loop and to decide the shortest distance(p1 to p9)
}
}
添加了代码,我一直停留在距离之间,我的道歉仍然处于学习android的阶段。我也发现使用For循环的困难,如下面的Eric所述。请做一些灯。感谢。
答案 0 :(得分:0)
为什么不使用Location.distanceBetween
?
如:
float results = new float[1];
Location.distanceBetween(lat1, long1, lat2, long2, results);
您可以在基本for
循环中使用此功能来检测最小值(lat1
,long1
可以作为起点,您可以更改lat2
和{{ 1}}在循环的每次迭代中),然后返回距离最小的点。