1、比較:
GPS准確度高但耗電多,網絡定位耗電少但准確度低
2、代碼
①添加權限:
AndroidManifest.xml:
<!-- 兩種provider的權限 --> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- 僅網絡定位的權限 --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
②Java代碼:
public class MainActivity extends Activity { //定位都要通過LocationManager這個類實現 private LocationManager locationManager; private String provider; @SuppressWarnings("static-access") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //獲取定位服務 locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); //獲取當前可用的位置控制器 List<String> list = locationManager.getProviders(true); if (list.contains(LocationManager.GPS_PROVIDER)) { //是否為GPS位置控制器 provider = LocationManager.GPS_PROVIDER; } else if (list.contains(LocationManager.NETWORK_PROVIDER)) { //是否為網絡位置控制器 provider = LocationManager.NETWORK_PROVIDER; } else { Toast.makeText(this, "請檢查網絡或GPS是否打開", Toast.LENGTH_LONG).show(); return; } Location location = locationManager.getLastKnownLocation(provider); if (location != null) { //獲取當前位置,這里只用到了經緯度 String string = "緯度為:" + location.getLatitude() + ",經度為:" + location.getLongitude(); } //綁定定位事件,監聽位置是否改變 //第一個參數為控制器類型第二個參數為監聽位置變化的時間間隔(單位:毫秒) //第三個參數為位置變化的間隔(單位:米)第四個參數為位置監聽器 locationManager.requestLocationUpdates(provider, 2000, 2, locationListener); } LocationListener locationListener = new LocationListener() { @Override public void onStatusChanged(String arg0, int arg1, Bundle arg2) { // TODO Auto-generated method stub } @Override public void onProviderEnabled(String arg0) { // TODO Auto-generated method stub } @Override public void onProviderDisabled(String arg0) { // TODO Auto-generated method stub } @Override public void onLocationChanged(Location arg0) { // TODO Auto-generated method stub // 更新當前經緯度 } }; //關閉時解除監聽器 @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); if (locationManager != null) { locationManager.removeUpdates(locationListener); } } }
計算兩點地圖坐標的距離 C#
private static double CalcMil(double X1, double Y1, double X2, double Y2) { double PI = 3.1415926535898; double EARTH_RADIUS = 6378137; //地球半徑 double CurRadLong = 0; //兩點經緯度的弧度 double CurRadLat = 0; double PreRadLong = 0; double PreRadLat = 0; double a = 0, b = 0; //經緯度弧度差 double MilValue = 0; //將經緯度換算成弧度 CurRadLong = (double)(X1); CurRadLong = CurRadLong * PI / 180.0; PreRadLong = (double)(X2); PreRadLong = PreRadLong * PI / 180.0; CurRadLat = (double)(Y1); CurRadLat = CurRadLat * PI / 180.0f; PreRadLat = (double)(Y2); PreRadLat = PreRadLat * PI / 180.0f; //計算經緯度差值 if (CurRadLat > PreRadLat) { a = CurRadLat - PreRadLat; } else { a = PreRadLat - CurRadLat; } if (CurRadLong > PreRadLong) { b = CurRadLong - PreRadLong; } else { b = PreRadLong - CurRadLong; } MilValue = 2 * Math.Asin(Math.Sqrt(Math.Sin(a / 2.0) * Math.Sin(a / 2.0) + Math.Cos(CurRadLat) * Math.Cos(PreRadLat) * Math.Sin(b / 2.0) * Math.Sin(b / 2.0))); MilValue = (double)(EARTH_RADIUS * MilValue); return MilValue; }
計算兩點地圖坐標的距離 Java
/** * Created by yuliang on 2015/3/20. */ public class LocationUtils { private static double EARTH_RADIUS = 6378.137; private static double rad(double d) { return d * Math.PI / 180.0; } /** * 通過經緯度獲取距離(單位:米) * @param lat1 * @param lng1 * @param lat2 * @param lng2 * @return */ public static double getDistance(double lat1, double lng1, double lat2, double lng2) { double radLat1 = rad(lat1); double radLat2 = rad(lat2); double a = radLat1 - radLat2; double b = rad(lng1) - rad(lng2); double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2))); s = s * EARTH_RADIUS; s = Math.round(s * 10000d) / 10000d; s = s*1000; return s; } }
