在我的设备上,一切运行正常,但其他人显示出巨大的距离。就像它应该距离点300米,它显示6000km
这是我如何获得我的位置:
private SharedPreferences sPref;
private LocationManager locationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_layout);
sPref=getSharedPreferences(ShareTag, MODE_PRIVATE);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
}
@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
1000 * 10, 10, locationListener);
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 1000 * 10, 10,
locationListener);
}
private LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Log.d("MyLog", "my lat="+location.getLatitude()+" splash mylong="+location.getLongitude());
saveCoordinates(location);
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
//do nothing
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
if (provider.equals(LocationManager.GPS_PROVIDER)) {
//do nothing
} else if (provider.equals(LocationManager.NETWORK_PROVIDER)) {
//do nothing
}
}
};
private void saveCoordinates(Location location){
Log.d("MyLog","get in the save coord");
Editor ed=sPref.edit();
ed.putString("myLatitude", ""+location.getLatitude());
ed.putString("myLongtitude", ""+location.getLongitude());
ed.commit();
}
}
这就是我如何获得坐标并存储它们
然后,当我需要距离时
sPref=context.getSharedPreferences(ShareTag, context.MODE_PRIVATE);
myLatitude=Double.parseDouble(sPref.getString("myLatitude", "0"));
myLongtitude=Double.parseDouble(sPref.getString("myLongtitude", "0"));
float[] result = new float[1];
latitude=address.getLatitude();
longtitude=address.getLongitude();
Location.distanceBetween(latitude, longtitude, myLatitude, myLongtitude, result);
因此,结果[0]显示不切实际的数字。longtitude=address.getLongitude();
和latitude=address.getLatitude();
是正确的,所以问题是myLatitude和myLongtitude。我无法查看这些设备的日志以查看是否用户的坐标是错误的。我的GPS工作不对(chineese设备)所以我从网络提供商那里得到我的坐标 - 这可能就是他们为什么这样做的原因。
我该怎么办?有什么建议吗?
答案 0 :(得分:0)
for get current location ::-
Criteria criteria = new Criteria();
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
String provider = locationManager.NETWORK_PROVIDER;
Location location = locationManager.getLastKnownLocation(provider);
if(location==null)
{
}else
{
longitude = location.getLongitude();
latitude = location.getLatitude();
Log.i("Latitude : ", String.valueOf(latitude));
Log.i("longitude : ", String.valueOf(longitude));
}
get distance in KM ::--
double distance = distFrom(latitude, longitude, latitude1, longitude1);
double km = distance / 0.62137;
method ::--
public static double distFrom(double lat1, double lng1, double lat2,
double lng2) {
double earthRadius = 3958.75;
double dLat = Math.toRadians(lat2 - lat1);
double dLng = Math.toRadians(lng2 - lng1);
double sindLat = Math.sin(dLat / 2);
double sindLng = Math.sin(dLng / 2);
double a = Math.pow(sindLat, 2) + Math.pow(sindLng, 2)
* Math.cos(Math.toRadians(lat1))
* Math.cos(Math.toRadians(lat2));
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double dist = earthRadius * c;
return dist;
}