【2013-11-05】android应用开发笔记:GPS相关设置和距离计算
2026/7/10 23:15:31 网站建设 项目流程

[历史归档]本文原发布于 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){Intentintent=newIntent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);context.startActivity(intent);}/** 使用系统 API 计算两点距离,返回米 */publicstaticfloatcalcDistance(doublestartLatitude,doublestartLongitude,doubleendLatitude,doubleendLongitude){float[]result=newfloat[]{-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 也提供了计算距离的方法,但未经实际测试,仅作备忘。

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询