1

我有海岸线坐标(超过一百个坐标),我想将坐标放入数组中。之后计算我当前位置与所有坐标之间的距离(使用 for 循环)并将结果也放入数组中。所以我有两个数组

  1. 地理点数组
  2. 距离阵列

我想找到距离数组的最小值,但程序总是强制关闭。这是我的代码,只显示十个坐标:

public class Nav extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nav);

        List<GeoPoint> points = new ArrayList<GeoPoint>();

        Double latitude1 = Double.parseDouble("-0.696928");
        Double longitude1 = Double.parseDouble("119.843473");       
        GeoPoint latLong1 = new GeoPoint((int)(latitude1*1e6),(int)(longitude1*1e6));

        Double latitude2 = Double.parseDouble("-0.699331");
        Double longitude2 = Double.parseDouble("119.84416");       
        GeoPoint latLong2 = new GeoPoint((int)(latitude2*1e6),(int)(longitude2*1e6));

        Double latitude3 = Double.parseDouble("-0.701048");
        Double longitude3 = Double.parseDouble("119.843817");       
        GeoPoint latLong3 = new GeoPoint((int)(latitude3*1e6),(int)(longitude3*1e6));

        Double latitude4 = Double.parseDouble("-0.702592");
        Double longitude4 = Double.parseDouble("119.843817");       
        GeoPoint latLong4 = new GeoPoint((int)(latitude4*1e6),(int)(longitude4*1e6));

        Double latitude5 = Double.parseDouble("-0.701563");
        Double longitude5 = Double.parseDouble("119.84622");       
        GeoPoint latLong5 = new GeoPoint((int)(latitude5*1e6),(int)(longitude5*1e6));

        Double latitude6 = Double.parseDouble("-0.702249");
        Double longitude6 = Double.parseDouble("119.84725");       
        GeoPoint latLong6 = new GeoPoint((int)(latitude6*1e6),(int)(longitude6*1e6));

        Double latitude7 = Double.parseDouble("-0.703451");
        Double longitude7= Double.parseDouble("119.846907");       
        GeoPoint latLong7 = new GeoPoint((int)(latitude7*1e6),(int)(longitude7*1e6));

        Double latitude8 = Double.parseDouble("-0.705167");
        Double longitude8 = Double.parseDouble("119.847937");       
        GeoPoint latLong8 = new GeoPoint((int)(latitude8*1e6),(int)(longitude8*1e6));

        Double latitude9 = Double.parseDouble("-0.70654");
        Double longitude9 = Double.parseDouble("119.849653");       
        GeoPoint latLong9 = new GeoPoint((int)(latitude9*1e6),(int)(longitude9*1e6));

        Double latitude10 = Double.parseDouble("10. -0.707914");
        Double longitude10 = Double.parseDouble("119.85137");       
        GeoPoint latLong10 = new GeoPoint((int)(latitude10*1e6),(int)(longitude10*1e6));



        points.add(latLong1);
        points.add(latLong2);
        points.add(latLong3);
        points.add(latLong4);
        points.add(latLong5);
        points.add(latLong6);
        points.add(latLong7);
        points.add(latLong8);
        points.add(latLong9);
        points.add(latLong10);


        // Get the location manager
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setAltitudeRequired(false);
        Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, true));
        Double lat,lon;

        lat = location.getLatitude ();
        lon = location.getLongitude ();

        Location locationA = new Location("USER");  
        locationA.setLatitude(lat);  
        locationA.setLongitude(lon);

        ArrayList<Double> dist = new ArrayList<Double>();
        for(int i=0; i<=points.size(); i++){
            double distances = locationA.distanceTo(points(i));

            dist.add(distances);
            double minDist = Collections.min(dist);
            Toast.makeText( getApplicationContext(),
                    "Your Distance to the beach is " + minDist,
                    Toast.LENGTH_LONG).show();

        }       
    }

    private Location points(int i) {
        // TODO Auto-generated method stub
        return null;
    }   
}

有人知道我的代码有什么问题吗?以及如何知道和显示 Goepoint 离我的位置很近?这是我的 LogCat

 
10-08 13:20:18.784: I/Process(632): 发送信号。PID:632 SIG:9
10-08 13:20:31.743: D/dalvikvm(675): GC_FOR_ALLOC 释放 254K,10% 释放 5364K/5959K,暂停 95ms,总共 99ms
10-08 13:20:31.993:I/Choreographer(675):跳过 59 帧!应用程序可能在其主线程上做了太多工作。
10-08 13:20:32.024:D/gralloc_goldfish(675):没有检测到 GPU 仿真的仿真器。
10-08 13:20:33.944:I/Choreographer(675):跳过 159 帧!应用程序可能在其主线程上做了太多工作。
10-08 13:20:34.084: D/AndroidRuntime(675): 关闭虚拟机
10-08 13:20:34.084: W/dalvikvm(675): threadid=1: 线程以未捕获的异常退出 (group=0x40a13300)
10-08 13:20:34.174:E/AndroidRuntime(675):致命异常:主要
10-08 13:20:34.174: E/AndroidRuntime(675): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.marker/com.marker.Nav}: java.lang.RuntimeException: 存根
10-08 13:20:34.174: E/AndroidRuntime(675): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
10-08 13:20:34.174: E/AndroidRuntime(675): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-08 13:20:34.174: E/AndroidRuntime(675): 在 android.app.ActivityThread.access$600(ActivityThread.java:130)
10-08 13:20:34.174: E/AndroidRuntime(675): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-08 13:20:34.174: E/AndroidRuntime(675): 在 android.os.Handler.dispatchMessage(Handler.java:99)
10-08 13:20:34.174: E/AndroidRuntime(675): 在 android.os.Looper.loop(Looper.java:137)
10-08 13:20:34.174: E/AndroidRuntime(675): 在 android.app.ActivityThread.main(ActivityThread.java:4745)
10-08 13:20:34.174: E/AndroidRuntime(675): 在 java.lang.reflect.Method.invokeNative(Native Method)
10-08 13:20:34.174: E/AndroidRuntime(675): 在 java.lang.reflect.Method.invoke(Method.java:511)
10-08 13:20:34.174: E/AndroidRuntime(675): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-08 13:20:34.174: E/AndroidRuntime(675): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-08 13:20:34.174:E/AndroidRuntime(675):在 dalvik.system.NativeStart.main(本机方法)
10-08 13:20:34.174:E/AndroidRuntime(675):原因:java.lang.RuntimeException:存根
10-08 13:20:34.174: E/AndroidRuntime(675): at com.google.android.maps.GeoPoint.(Unknown Source)
10-08 13:20:34.174: E/AndroidRuntime(675): 在 com.marker.Nav.onCreate(Nav.java:32)
10-08 13:20:34.174: E/AndroidRuntime(675): 在 android.app.Activity.performCreate(Activity.java:5008)
10-08 13:20:34.174: E/AndroidRuntime(675): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-08 13:20:34.174: E/AndroidRuntime(675): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
10-08 13:20:34.174: E/AndroidRuntime(675): ... 11 更多
4

3 回答 3

2

没有看到您的错误消息,我绝对可以发现问题。您正在迭代列表的末尾。在循环条件中使用<而不是。<=

此外,这可能不是错误,但你为什么要Collections.min()在循环中调用?在所有距离都被填充之后,您不想这样做吗?我误解你的目的了吗?

于 2013-10-08T05:18:41.760 回答
2

LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

使用 getApplicationContext().getSystemService(LOCATION_SERVICE);

或 Context.getSystemService(LOCATION_SERVICE);

于 2013-10-08T05:21:59.160 回答
1

听起来您正在获取ArrayIndexOutOfBoundsException 文档

for(int i=0; i<=points.size(); i++){ // Check i<= here it should be < only

所以尝试将其更改为

for(int i=0; i<points.size(); i++){
于 2013-10-08T04:57:06.900 回答