Некорректная ближайшая точка
Еще раз помогите, пожалуйста) сейчас ошибок в коде нет, все определяется, проблема только в некорректности...Я сейчас в точке 55.898113,37.718918 , программа должна бы показать 55.894683,37.721857 , но показывает 54.463406,19.942433..в чем причина?
вот код ScreenOne
package com.example.app;
import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import java.util.Arrays;
public class ScreenOne extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen_one);
}
GeoPoint currentLocation = new GeoPoint((formatLocation(null)), (formalLoc(null)), "hhh");
private double formatLocation(Location location) {
if (location == null)
return
location.getLatitude();
return 0;
}
private double formalLoc(Location location) {
if (location == null)
return location.getLongitude();
return 0;
}
GeoPoint locations[] = new GeoPoint[]{
new GeoPoint(55.894683,37.721857, "точка1"),
new GeoPoint(54.463406,19.942433, "точка2"),
};
GeoPoint nearest = GeoPoint.getNearestLocation(currentLocation,
Arrays.asList(locations));
public void onClick(View view) {
TextView helloTextView = (TextView) findViewById(R.id.station_name);
helloTextView.setText(nearest.getName());
}
}
а вот код GeoPoint
package com.example.app;
import android.location.Location;
import java.util.Collection;
public class GeoPoint {
public final double lat;
public final double lon;
public String name;
public GeoPoint(double lat, double lon, String name) {
this.lat = lat;
this.lon = lon;
this.name = name;
}
public String getName(){
return name;
}
public GeoPoint(Location location) {
this.lat = location.getLatitude();
this.lon = location.getLongitude();
}
public static GeoPoint getNearestLocation(GeoPoint current, Collection<GeoPoint> locations) {
GeoPoint res = null;
float lastDisance = Float.MAX_VALUE;
float locDistance[] = new float[1];
for (GeoPoint loc: locations) {
Location.distanceBetween(current.lat, current.lon,
loc.lat, loc.lon, locDistance);
if (res == null || locDistance[0] < lastDisance) {
res = loc;
lastDisance = locDistance[0];
}
}
return res;
}
}
Источник: Stack Overflow на русском