亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android實(shí)現(xiàn)GPS定位代碼實(shí)例

 更新時(shí)間:2014年07月09日 11:59:13   投稿:junjie  
這篇文章主要介紹了Android實(shí)現(xiàn)GPS定位實(shí)例,對(duì)關(guān)鍵操作部份給出代碼示例并做了一定的注釋,需要的朋友可以參考下

通過(guò)GPS取得的是一個(gè)Location類型的經(jīng)緯度, 可以轉(zhuǎn)換為兩個(gè)Double 緯度和經(jīng)度.
緯度: 23.223871812820435
緯度: 113.58986039161628
首先創(chuàng)建一個(gè)TextView和兩個(gè)Button

<TextView 
 android:id="@+id/text"
 android:layout_width="fill_parent" 
  android:layout_height="wrap_content"  />
 
 <Button
  android:id="@+id/btnStart"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="定位" />
 <Button
  android:id="@+id/btnStop"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="停止" />


然后添加主Activity的代碼
Location 是存放經(jīng)緯度的一個(gè)類型
LocationManager是位置管理服務(wù)類型

private Button btnStart;
private Button btnStop;
private TextView textView;
private Location mLocation;
private LocationManager mLocationManager;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
 
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 
 btnStart = (Button)findViewById(R.id.btnStart);
 btnStop = (Button)findViewById(R.id.btnStop);
 textView = (TextView)findViewById(R.id.text);
 btnStart.setOnClickListener(btnClickListener); //開始定位
 btnStop.setOnClickListener(btnClickListener); //結(jié)束定位按鈕
}
gpsIsOpen是自己寫的查看當(dāng)前GPS是否開啟
getLocation 是自己寫的一個(gè)獲取定位信息的方法
mLocationManager.removeUpdates()是停止當(dāng)前的GPS位置監(jiān)聽
public Button.OnClickListener btnClickListener = new Button.OnClickListener()
{
 public void onClick(View v)
 {
  Button btn = (Button)v;
  if(btn.getId() == R.id.btnStart)
  {
   if(!gpsIsOpen())
    return;
   
  mLocation = getLocation();
   
   if(mLocation != null)
    textView.setText("維度:" + mLocation.getLatitude() + "\n經(jīng)度:" + mLocation.getLongitude());
   else
    textView.setText("獲取不到數(shù)據(jù)");
  }
  else if(btn.getId() == R.id.btnStop)
  {
   mLocationManager.removeUpdates(locationListener);
  }

 }
};
private boolean gpsIsOpen()
{
 boolean bRet = true;

 LocationManager alm = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
 if(!alm.isProviderEnabled(LocationManager.GPS_PROVIDER))
 {
  Toast.makeText(this, "未開啟GPS", Toast.LENGTH_SHORT).show();
  bRet = false;
 }
 else 
 {
  Toast.makeText(this, "GPS已開啟", Toast.LENGTH_SHORT).show();
 }

 return bRet;
}
判斷當(dāng)前是否開啟GPS
private boolean gpsIsOpen()
{
 boolean bRet = true;
 
 LocationManager alm = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
 if(!alm.isProviderEnabled(LocationManager.GPS_PROVIDER))
 {
  Toast.makeText(this, "未開啟GPS", Toast.LENGTH_SHORT).show();
  bRet = false;
 }
 else 
 {
  Toast.makeText(this, "GPS已開啟", Toast.LENGTH_SHORT).show();
 }

 return bRet;
}
該方法獲取當(dāng)前的經(jīng)緯度, 第一次獲取總是null
后面從LocationListener獲取已改變的位置
mLocationManager.requestLocationUpdates()是開啟一個(gè)LocationListener等待位置變化
private Location getLocation()
{
 //獲取位置管理服務(wù)
 mLocationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);

 //查找服務(wù)信息
 Criteria criteria = new Criteria();
 criteria.setAccuracy(Criteria.ACCURACY_FINE); //定位精度: 最高
 criteria.setAltitudeRequired(false); //海拔信息:不需要
 criteria.setBearingRequired(false); //方位信息: 不需要
 criteria.setCostAllowed(true);  //是否允許付費(fèi)
 criteria.setPowerRequirement(Criteria.POWER_LOW); //耗電量: 低功耗
 
 String provider = mLocationManager.getBestProvider(criteria, true); //獲取GPS信息

 Location location = mLocationManager.getLastKnownLocation(provider);
 
 mLocationManager.requestLocationUpdates(provider, 2000, 5, locationListener);
 
 return location;
}
改方法是等待GPS位置改變后得到新的經(jīng)緯度
private final LocationListener locationListener = new LocationListener()
{
 public void onLocationChanged(Location location)
 {
  // TODO Auto-generated method stub
  if(location != null)
   textView.setText("維度:" + location.getLatitude() + "\n經(jīng)度:" 
      + location.getLongitude());
  else
   textView.setText("獲取不到數(shù)據(jù)" + Integer.toString(nCount));
 }

 public void onProviderDisabled(String provider)
 {
  // TODO Auto-generated method stub
 }

 public void onProviderEnabled(String provider)
 {
  // TODO Auto-generated method stub
 }

 public void onStatusChanged(String provider, int status, Bundle extras)
 {
  // TODO Auto-generated method stub
  
 }
};

相關(guān)文章

最新評(píng)論