国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android LocationManager獲取經(jīng)度與緯度等地理信息

2019-10-22 18:28:32
字體:
供稿:網(wǎng)友

Android LocationManager獲取經(jīng)度與緯度等地理信息

利用LocationManager實現(xiàn)定位功能  

1 實時更新經(jīng)度,緯度  

2 根據(jù)經(jīng)度和緯度獲取地理信息(比如:國家,街道等)(略過)

MainActivity如下:

package cc.bb;  import java.util.Iterator; import java.util.List; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.widget.TextView; import android.app.Activity; import android.content.Context; /**  * Demo描述:  * 利用LocationManager實現(xiàn)定位功能  * 1 實時更新經(jīng)度,緯度  * 2 根據(jù)經(jīng)度和緯度獲取地理信息(比如:國家,街道等)(略過)  *  *  * 注意事項:  * 0 在測試GPS定位時最好在較為寬廣的空間,否則影響定位  * 1 利用mLocationManager.getLastKnownLocation(GPSProvider)獲取Location時常為null.  *  因為設(shè)備定位是需要一定時間的,所以把定位邏輯放在LocationManager的requestLocationUpdates()方法  *   * 2 LocationManager.requestLocationUpdates  *  (String provider, long minTime, float minDistance, LocationListener listener)  *  第一個參數(shù):位置信息的provider,比如GPS  *  第二個參數(shù):更新位置信息的時間間隔,單位毫秒  *  第三個參數(shù):更新位置信息的距離間隔,單位米  *  第四個參數(shù):位置信息變化時的回調(diào)  *   * 3 LocationListener中最重要的回調(diào)方法onLocationChanged()  *  當(dāng)minTime和minDistance同時滿足時會調(diào)用該方法.文檔說明:  *  The minDistance parameter can also be used to control the  *  frequency of location updates. If it is greater than 0 then the  *  location provider will only send your application an update when  *  the location has changed by at least minDistance meters, AND  *  at least minTime milliseconds have passed.  *  比如間隔時間(minTime)到了3秒并且移動的距離(minDistance)大于了5米  *  那么就會調(diào)用該方法.  *   * 4 在Activity的onDestroy()時取消地理位置的更新.  *  *  * 權(quán)限配置:  * <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>  * <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>  * <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>  * <uses-permission android:name="android.permission.INTERNET"/>  */ public class MainActivity extends Activity {   private Context mContext;   private TextView mTextView;   private LocationManager mLocationManager;   private LocationListenerImpl mLocationListenerImpl;    @Override   protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.main);     init();     initLocationManager(mContext);   }      private void init(){     mContext=this;     mTextView=(TextView) findViewById(R.id.textView);   }    private void initLocationManager(Context context){          mLocationManager=(LocationManager)context.getSystemService(Context.LOCATION_SERVICE);          //獲取可用的位置信息Provider.即passive,network,gps中的一個或幾個     List<String> providerList=mLocationManager.getProviders(true);     for (Iterator<String> iterator = providerList.iterator(); iterator.hasNext();) {       String provider = (String) iterator.next();       System.out.println("provider="+provider);     }               //在此采用GPS的方式獲取位置信息     String GPSProvider=LocationManager.GPS_PROVIDER;     Location location=mLocationManager.getLastKnownLocation(GPSProvider);     if (location!=null) {       double longitude=location.getLongitude();       double altitude=location.getAltitude();       System.out.println("longitude="+longitude+",altitude="+altitude);     } else {       System.out.println("location==null");     }          //注冊位置監(jiān)聽     mLocationListenerImpl=new LocationListenerImpl();     mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 5, mLocationListenerImpl);   }         private class LocationListenerImpl implements LocationListener{     //當(dāng)設(shè)備位置發(fā)生變化時調(diào)用該方法     @Override     public void onLocationChanged(Location location) {       if (location!=null) {         showLocation(location);       }     }      //當(dāng)provider的狀態(tài)發(fā)生變化時調(diào)用該方法.比如GPS從可用變?yōu)椴豢捎?     @Override     public void onStatusChanged(String provider, int status, Bundle extras) {            }      //當(dāng)provider被打開的瞬間調(diào)用該方法.比如用戶打開GPS     @Override     public void onProviderEnabled(String provider) {            }      //當(dāng)provider被關(guān)閉的瞬間調(diào)用該方法.比如關(guān)閉打開GPS     @Override     public void onProviderDisabled(String provider) {            }        }         private void showLocation(Location location) {     // 獲取經(jīng)度     double longitude = location.getLongitude();     // 獲取緯度     double altitude = location.getAltitude();     String message="經(jīng)度為:"+longitude+"/n"+"緯度為:"+altitude;     mTextView.setText(message);        }      @Override   protected void onDestroy() {     super.onDestroy();     if (mLocationManager!=null) {       mLocationManager.removeUpdates(mLocationListenerImpl);     }   }       } 

main.xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"   xmlns:tools="http://schemas.android.com/tools"   android:layout_width="match_parent"   android:layout_height="match_parent"   android:paddingBottom="@dimen/activity_vertical_margin"   android:paddingLeft="@dimen/activity_horizontal_margin"   android:paddingRight="@dimen/activity_horizontal_margin"   android:paddingTop="@dimen/activity_vertical_margin"   tools:context=".MainActivity" >    <TextView     android:id="@+id/textView"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:layout_centerInParent="true"     android:gravity="center" />  </RelativeLayout> 

如有疑問請留言或者到本站社區(qū)交流討論,本站關(guān)于Android開發(fā)的文章還有很多,希望大家多多搜索查閱,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!


注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 崇左市| 汝南县| 龙井市| 卓资县| 绥芬河市| 含山县| 游戏| 灵川县| 龙江县| 莱阳市| 齐齐哈尔市| 勃利县| 昔阳县| 开鲁县| 东莞市| 哈密市| 平山县| 辽源市| 鞍山市| 敦煌市| 洪雅县| 巴彦县| 报价| 阳西县| 张北县| 旬邑县| 莱西市| 兴义市| 桃园县| 辉南县| 高阳县| 盈江县| 白玉县| 通州市| 铅山县| 九江县| 寻甸| 普格县| 徐闻县| 库车县| 阳城县|