自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android中GPS定位(獲取經(jīng)緯度)

移動開發(fā) Android
本文通過作者自己的理解與實踐為大家呈現(xiàn)了AndroidGPS定位獲取經(jīng)緯度的實現(xiàn)方法與代碼,把作者自己對這一知識點的領(lǐng)悟和實行分享給大家。

AndroidGPS定位問題,眾所周知是一個蠻麻煩的問題.當(dāng)初我是新手,現(xiàn)在我也是新手,也搞了我頭大,網(wǎng)上搜索了很多的例子,一直處于僵持階段,而現(xiàn)在終于搞定了,因為我現(xiàn)在只需要獲取到經(jīng)緯度就可以了,反正獲取經(jīng)緯度可以從我這篇文章中看看;上代碼。

在AndroidManifest.xml中加入權(quán)限:
<uses-permission android:name="android.permission.ACCESSFINELOCATION"/>
<uses-permission android:name="android.permission.ACCESSCOARSELOCATION"/>

  1. package com.example.tt; 
  2.  
  3. import android.location.Location; 
  4. import android.location.LocationListener; 
  5. import android.location.LocationManager; 
  6. import android.os.Bundle; 
  7. import android.app.Activity; 
  8. import android.content.Context; 
  9. import android.view.View; 
  10. import android.view.View.OnClickListener; 
  11. import android.widget.Button; 
  12. import android.widget.TextView; 
  13. import android.widget.Toast; 
  14.  
  15. public class MainActivity extends Activity { 
  16.      
  17.     @Override 
  18.     protected void onCreate(Bundle savedInstanceState) { 
  19.         super.onCreate(savedInstanceState); 
  20.         setContentView(R.layout.activity_main); 
  21.                  
  22.         Button button=(Button)findViewById(R.id.button1); 
  23.         button.setOnClickListener(new OnClickListener() { 
  24.              
  25.             @Override 
  26.             public void onClick(View arg0) { 
  27.                 // TODO Auto-generated method stub 
  28.                 String serviceString=Context.LOCATION_SERVICE; 
  29.                 LocationManager locationManager=(LocationManager)getSystemService(serviceString); 
  30.                 String provider=LocationManager.GPS_PROVIDER; 
  31.                 Location location=locationManager.getLastKnownLocation(provider); 
  32.                 getLocationInfo(location); 
  33.                                 locationManager.requestLocationUpdates(provider, 20000, locationListener); 
  34.             } 
  35.         });          
  36.     } 
  37.     private void getLocationInfo(Location location) { 
  38.         String latLongInfo; 
  39.         TextView lo=(TextView)findViewById(R.id.textView1); 
  40.         if(location!=null){ 
  41.             double lat=location.getLatitude(); 
  42.             double lng=location.getLongitude(); 
  43.             latLongInfo="Lat:"+lat+"\nLong:"+lng; 
  44.             lo.setText(latLongInfo); 
  45.         }else { 
  46.             latLongInfo="No location found"
  47.             lo.setText(latLongInfo); 
  48.         }        
  49.     } 
  50.     private final LocationListener locationListener =new LocationListener() {        
  51.         @Override 
  52.         public void onStatusChanged(String provider, int status, Bundle extras) { 
  53.             // TODO Auto-generated method stub 
  54.              
  55.         }        
  56.         @Override 
  57.         public void onProviderEnabled(String provider) { 
  58.             getLocationInfo(null); 
  59.              
  60.         }        
  61.         @Override 
  62.         public void onProviderDisabled(String provider) { 
  63.             getLocationInfo(null);           
  64.         }        
  65.         @Override 
  66.         public void onLocationChanged(Location location) { 
  67.             getLocationInfo(location); 
  68.             Toast.makeText(MainActivity.this"位置改變了::::::::::::"3000).show(); 
  69.         } 
  70.     }; 

當(dāng)需要使用基站定位時,可以將String provider=LocationManager.GPS_PROVIDER;改為**String provider=LocationManager.NETWORK_PROVIDER;
**
具體如果還要判斷GPS搜索不到時切換基站定位,那樣的功能就不要我寫了,新手都應(yīng)該會.
還有就是如果用到Google定位到哪個城市地點什么的,也easy了。

以上就是我個人對AndroidGPS定位的理解和實現(xiàn)方法。

責(zé)任編輯:閆佳明 來源: my.eoe.cn
相關(guān)推薦

2013-05-23 14:43:15

Android開發(fā)IP地址經(jīng)緯度坐標

2023-03-13 22:01:15

ChatGPTPython

2012-06-14 09:37:45

Google地圖

2023-12-11 07:37:08

mongodb經(jīng)緯度性能

2011-05-27 15:56:30

Android

2021-04-18 16:34:13

PythonAPI接口

2014-07-14 13:03:26

2011-10-21 09:28:25

百度地圖API

2014-11-13 14:07:04

2011-05-24 09:23:00

中電信定位云

2017-11-13 15:46:07

2013-04-01 13:19:43

iOS定位與坐標算法

2011-05-23 10:06:15

2021-01-01 19:02:08

GPS定位欺騙網(wǎng)絡(luò)安全

2011-07-18 13:37:53

2023-10-20 16:25:30

Python

2017-09-01 13:41:20

Android定位服務(wù)

2012-08-13 14:03:25

Google Map

2012-02-01 09:33:36

百度地圖API
點贊
收藏

51CTO技術(shù)棧公眾號