好记性不如铅笔头

android, 编程

android应用开发笔记:GPS相关设置和距离计算

我们在使用android开发GPS应用的时候,往往需要计算两点间的距离,以及提示用户GPS没有打开等等。下面备忘下工具类:

public class GPSUtils
{
	public static boolean isGPSOpen(Context context)
	{
		LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
		return lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
	}
	
	public static boolean isAGPSOpen(Context context)
	{
		LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
		return lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
	}

	public static void openLocatoinSettings(Context context)
	{
		Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
		context.startActivity(intent);
	}
	
	/* 返回米 */
	public static float calcDistance(double startLatitude, double startLongitude, double endLatitude, double endLongitude)
	{
		float [] result = new float[]{-1f,0f,0f};
		Location.distanceBetween(startLatitude, startLongitude, endLatitude, endLongitude, result);
		return result[0];
	}
	
	/* 返回米 */
	public static float calcDistanceByBD(double startLatitude, double startLongitude, double endLatitude, double endLongitude)
	{
		return (float)DistanceUtil.getDistance(new GeoPoint((int)(startLatitude * 1e6), (int)(startLongitude * 1e6)), 
										new GeoPoint((int)(endLatitude * 1e6), (int) (endLongitude * 1e6)));
	}
}

 备注:

1)百度地图API也附带了一个计算距离的方法,但是作者并没有实际测试过,只是先放这备忘。

发表评论

18 − 5 =

此站点使用 Akismet 来减少垃圾评论。了解我们如何处理您的评论数据