首页 > 代码库 > GPS技术(二)
GPS技术(二)
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
1 //邻近警告 2 public class MainActivity extends ActionBarActivity { 3 4 @Override 5 protected void onCreate(Bundle savedInstanceState) { 6 super.onCreate(savedInstanceState); 7 setContentView(R.layout.fragment_main); 8 LocationManager lManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 9 double longitude = 123.427109; 10 double latitude = 41.764998; 11 float radius = 2000; 12 Intent intent = new Intent(this, PromimityAlterRecivery.class); 13 PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, 14 intent, 0); 15 lManager.addProximityAlert(latitude, longitude, radius, -1, 16 pendingIntent); 17 18 } 19 20 }
1 public class PromimityAlterRecivery extends BroadcastReceiver { 2 3 @Override 4 public void onReceive(Context context, Intent intent) { 5 // 获取是否进入的指定区域 6 boolean isEnter = intent.getBooleanExtra( 7 LocationManager.KEY_PROXIMITY_ENTERING, false); 8 if (isEnter) { 9 Toast.makeText(context, "你已经进入指定区域", 0).show(); 10 } else { 11 Toast.makeText(context, "你已经进入指定区域然后出去了", 0).show(); 12 } 13 } 14 }
//---------------------------根据经纬度测两点之间的距离---------------------------------------------------------
1 //根据经纬度测两点之间的距离 2 public class MainActivity extends ActionBarActivity { 3 EditText mEditText; 4 5 @Override 6 protected void onCreate(Bundle savedInstanceState) { 7 super.onCreate(savedInstanceState); 8 setContentView(R.layout.fragment_main); 9 mEditText = (EditText) findViewById(R.id.editText1); 10 final float[] result = new float[3]; 11 Location.distanceBetween(20.123, 30.05644, 30.124, 40.543, result); 12 mEditText.setText(String.valueOf(result[0] + "米")); 13 14 } 15 16 }
//---------------------------实时获取GPS定位数据----------------------------------------------------
1 //实时获取GPS定位数据 2 public class MainActivity extends ActionBarActivity { 3 LocationManager mlManager; 4 EditText mEditText; 5 6 @Override 7 protected void onCreate(Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.fragment_main); 10 mEditText = (EditText) findViewById(R.id.editText1); 11 mlManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 12 // 从GPS获取最近的定位信息 13 Location location = mlManager 14 .getLastKnownLocation(LocationManager.GPS_PROVIDER); 15 16 undateView(location); 17 mlManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 8, 18 new LocationListener() { 19 20 @Override 21 public void onStatusChanged(String provider, int status, 22 Bundle extras) { 23 // TODO Auto-generated method stub 24 25 } 26 27 @Override 28 public void onProviderEnabled(String provider) { 29 // 当GPSlocationProvider可用时,更新位置 30 31 undateView(mlManager.getLastKnownLocation(provider)); 32 33 } 34 35 @Override 36 public void onProviderDisabled(String provider) { 37 // TODO Auto-generated method stub 38 39 } 40 41 @Override 42 public void onLocationChanged(Location location) { 43 // 当GPS位置信息发生变化,更新位置 44 undateView(location); 45 46 } 47 }); 48 49 } 50 51 // 用来更新EditText中的信息 52 private void undateView(Location newlocation) { 53 // TODO Auto-generated method stub 54 if (newlocation != null) { 55 StringBuilder sBuilder = new StringBuilder(); 56 sBuilder.append("实时的位置信息:\n"); 57 sBuilder.append("\n经度"); 58 sBuilder.append(newlocation.getLongitude()); 59 sBuilder.append("\n纬度"); 60 sBuilder.append(newlocation.getLatitude()); 61 sBuilder.append("\n高度"); 62 sBuilder.append(newlocation.getAltitude()); 63 sBuilder.append("\n速度"); 64 sBuilder.append(newlocation.getSpeed()); 65 sBuilder.append("\n方向"); 66 sBuilder.append(newlocation.getBearing()); 67 mEditText.setText(sBuilder.toString()); 68 } 69 } 70 71 }
GPS技术(二)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。