【2013-11-05】android应用开发笔记:GPS相关设置和距离计算 [历史归档]本文原发布于 cstriker1407.info 个人博客内容为历史存档仅供参考。发布时间2013-11-05 标题android应用开发笔记GPS相关设置和距离计算分类编程 / android 标签android·gps·百度地图android应用开发笔记GPS相关设置和距离计算备注在 Android GPS 应用开发中常需要检测 GPS 是否开启、跳转到位置设置页面、以及计算两点间距离。以下为工具类备忘publicclassGPSUtils{publicstaticbooleanisGPSOpen(Contextcontext){LocationManagerlm(LocationManager)context.getSystemService(Context.LOCATION_SERVICE);returnlm.isProviderEnabled(LocationManager.GPS_PROVIDER);}publicstaticbooleanisAGPSOpen(Contextcontext){LocationManagerlm(LocationManager)context.getSystemService(Context.LOCATION_SERVICE);returnlm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}publicstaticvoidopenLocationSettings(Contextcontext){IntentintentnewIntent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);context.startActivity(intent);}/** 使用系统 API 计算两点距离返回米 */publicstaticfloatcalcDistance(doublestartLatitude,doublestartLongitude,doubleendLatitude,doubleendLongitude){float[]resultnewfloat[]{-1f,0f,0f};Location.distanceBetween(startLatitude,startLongitude,endLatitude,endLongitude,result);returnresult[0];}/** 使用百度地图 API 计算两点距离返回米 */publicstaticfloatcalcDistanceByBD(doublestartLatitude,doublestartLongitude,doubleendLatitude,doubleendLongitude){return(float)DistanceUtil.getDistance(newGeoPoint((int)(startLatitude*1e6),(int)(startLongitude*1e6)),newGeoPoint((int)(endLatitude*1e6),(int)(endLongitude*1e6)));}}备注百度地图 API 也提供了计算距离的方法但未经实际测试仅作备忘。