Reputation: 71
I have shoreline coordinates (More than a hundred coordinates) and i want to put the coordinates in to array. After that calculate distance between my current location to all of the coordinates (using for loop) and put the result into an array too. So i have two array
I want to find the minimum value of the Distance array but the program always force close. Here is my code just show ten Coordinates :
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;
}
}
Is Somebody know what is wrong whit my code ? And how to know and show what Goepoint is close with my location ? Here is my LogCat
10-08 13:20:18.784: I/Process(632): Sending signal. PID: 632 SIG: 9 10-08 13:20:31.743: D/dalvikvm(675): GC_FOR_ALLOC freed 254K, 10% free 5364K/5959K, paused 95ms, total 99ms 10-08 13:20:31.993: I/Choreographer(675): Skipped 59 frames! The application may be doing too much work on its main thread. 10-08 13:20:32.024: D/gralloc_goldfish(675): Emulator without GPU emulation detected. 10-08 13:20:33.944: I/Choreographer(675): Skipped 159 frames! The application may be doing too much work on its main thread. 10-08 13:20:34.084: D/AndroidRuntime(675): Shutting down VM 10-08 13:20:34.084: W/dalvikvm(675): threadid=1: thread exiting with uncaught exception (group=0x40a13300) 10-08 13:20:34.174: E/AndroidRuntime(675): FATAL EXCEPTION: main 10-08 13:20:34.174: E/AndroidRuntime(675): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.marker/com.marker.Nav}: java.lang.RuntimeException: stub 10-08 13:20:34.174: E/AndroidRuntime(675): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 10-08 13:20:34.174: E/AndroidRuntime(675): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 10-08 13:20:34.174: E/AndroidRuntime(675): at android.app.ActivityThread.access$600(ActivityThread.java:130) 10-08 13:20:34.174: E/AndroidRuntime(675): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 10-08 13:20:34.174: E/AndroidRuntime(675): at android.os.Handler.dispatchMessage(Handler.java:99) 10-08 13:20:34.174: E/AndroidRuntime(675): at android.os.Looper.loop(Looper.java:137) 10-08 13:20:34.174: E/AndroidRuntime(675): at android.app.ActivityThread.main(ActivityThread.java:4745) 10-08 13:20:34.174: E/AndroidRuntime(675): at java.lang.reflect.Method.invokeNative(Native Method) 10-08 13:20:34.174: E/AndroidRuntime(675): at java.lang.reflect.Method.invoke(Method.java:511) 10-08 13:20:34.174: E/AndroidRuntime(675): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 10-08 13:20:34.174: E/AndroidRuntime(675): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 10-08 13:20:34.174: E/AndroidRuntime(675): at dalvik.system.NativeStart.main(Native Method) 10-08 13:20:34.174: E/AndroidRuntime(675): Caused by: java.lang.RuntimeException: stub 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): at com.marker.Nav.onCreate(Nav.java:32) 10-08 13:20:34.174: E/AndroidRuntime(675): at android.app.Activity.performCreate(Activity.java:5008) 10-08 13:20:34.174: E/AndroidRuntime(675): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 10-08 13:20:34.174: E/AndroidRuntime(675): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 10-08 13:20:34.174: E/AndroidRuntime(675): ... 11 more
Upvotes: 1
Views: 271
Reputation: 2270
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Use getApplicationContext().getSystemService(LOCATION_SERVICE);
or Context.getSystemService(LOCATION_SERVICE);
Upvotes: 2
Reputation: 30300
Without seeing your error message, I can definitely spot a problem. You are iterating past the end of the list. Use <
instead of <=
in your loop condition.
Also, and this may not be an error, but why are you calling Collections.min()
within the loop? Wouldn't you want to do that after all the distances are populated? Am I misunderstanding what you're going for?
Upvotes: 2
Reputation: 14199
It Sounds like you are getting ArrayIndexOutOfBoundsException
Docs
for(int i=0; i<=points.size(); i++){ // Check i<= here it should be < only
So try by changing it to
for(int i=0; i<points.size(); i++){
Upvotes: 1