Android实现GPS定位代码实例

2019-12-10 20:01:16王冬梅

易采站长站为您分析Android实现GPS定位实例,对关键操作部份给出代码示例并做了一定的注释,需要的朋友可以参考下

通过GPS取得的是一个Location类型的经纬度, 可以转换为两个Double 纬度和经度.

纬度: 23.223871812820435

纬度: 113.58986039161628

首先创建一个TextView和两个Button

 

 
  1. <TextView  android:id="@+id/text" 
  2. android:layout_width="fill_parent"  android:layout_height="wrap_content" /> 
  3.   <Button 
  4. android:id="@+id/btnStart"  android:layout_width="wrap_content" 
  5. android:layout_height="wrap_content"  android:text="定位" /> 
  6. <Button  android:id="@+id/btnStop" 
  7. android:layout_width="wrap_content"  android:layout_height="wrap_content" 
  8. android:text="停止" /> 

然后添加主Activity的代码

Location 是存放经纬度的一个类型

LocationManager是位置管理服务类型

 

 
  1. private Button btnStart;  private Button btnStop; 
  2. private TextView textView;  private Location mLocation; 
  3. private LocationManager mLocationManager;  /** Called when the activity is first created. */ 
  4. @Override  public void onCreate(Bundle savedInstanceState) 
  5. {   
  6. super.onCreate(savedInstanceState);  setContentView(R.layout.main);