Android LocationManager獲取經(jīng)度與緯度等地理信息
Android LocationManager獲取經(jīng)度與緯度等地理信息
利用LocationManager實(shí)現(xiàn)定位功能
1 實(shí)時(shí)更新經(jīng)度,緯度
2 根據(jù)經(jīng)度和緯度獲取地理信息(比如:國(guó)家,街道等)(略過)
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實(shí)現(xiàn)定位功能 * 1 實(shí)時(shí)更新經(jīng)度,緯度 * 2 根據(jù)經(jīng)度和緯度獲取地理信息(比如:國(guó)家,街道等)(略過) * * * 注意事項(xiàng): * 0 在測(cè)試GPS定位時(shí)最好在較為寬廣的空間,否則影響定位 * 1 利用mLocationManager.getLastKnownLocation(GPSProvider)獲取Location時(shí)常為null. * 因?yàn)樵O(shè)備定位是需要一定時(shí)間的,所以把定位邏輯放在LocationManager的requestLocationUpdates()方法 * * 2 LocationManager.requestLocationUpdates * (String provider, long minTime, float minDistance, LocationListener listener) * 第一個(gè)參數(shù):位置信息的provider,比如GPS * 第二個(gè)參數(shù):更新位置信息的時(shí)間間隔,單位毫秒 * 第三個(gè)參數(shù):更新位置信息的距離間隔,單位米 * 第四個(gè)參數(shù):位置信息變化時(shí)的回調(diào) * * 3 LocationListener中最重要的回調(diào)方法onLocationChanged() * 當(dāng)minTime和minDistance同時(shí)滿足時(shí)會(huì)調(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. * 比如間隔時(shí)間(minTime)到了3秒并且移動(dòng)的距離(minDistance)大于了5米 * 那么就會(huì)調(diào)用該方法. * * 4 在Activity的onDestroy()時(shí)取消地理位置的更新. * * * 權(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中的一個(gè)或幾個(gè) 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"); } //注冊(cè)位置監(jiān)聽 mLocationListenerImpl=new LocationListenerImpl(); mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 5, mLocationListenerImpl); } private class LocationListenerImpl implements LocationListener{ //當(dāng)設(shè)備位置發(fā)生變化時(shí)調(diào)用該方法 @Override public void onLocationChanged(Location location) { if (location!=null) { showLocation(location); } } //當(dāng)provider的狀態(tài)發(fā)生變化時(shí)調(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ǐng)留言或者到本站社區(qū)交流討論,本站關(guān)于Android開發(fā)的文章還有很多,希望大家多多搜索查閱,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
Android四大組件之Service(服務(wù))實(shí)例詳解
這篇文章主要介紹了Android四大組件之Service(服務(wù))的用法,結(jié)合實(shí)例形式詳細(xì)分析了Service的基本概念,類型,用法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-01-01Android ActionBarActivity設(shè)置全屏無標(biāo)題的方法總結(jié)
這篇文章主要介紹了Android ActionBarActivity設(shè)置全屏無標(biāo)題的相關(guān)資料,需要的朋友可以參考下2017-07-07Android實(shí)現(xiàn)懸浮窗全系統(tǒng)版本
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)懸浮窗全系統(tǒng)版本,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11Android實(shí)現(xiàn)Service下載文件,Notification顯示下載進(jìn)度的示例
本篇文章主要介紹了Android實(shí)現(xiàn)Service下載文件,Notification顯示下載進(jìn)度,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01Android自定義View實(shí)現(xiàn)餅狀圖帶動(dòng)畫效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)餅狀圖帶動(dòng)畫效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12RxJava2.x實(shí)現(xiàn)定時(shí)器的實(shí)例代碼
本篇文章主要介紹了RxJava2.x實(shí)現(xiàn)定時(shí)器,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06詳解AndroidStudio JNI +Gradle3.0以上JNI爬坑之旅
這篇文章主要介紹了詳解AndroidStudio JNI +Gradle3.0以上JNI爬坑之旅,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Android中兩個(gè)類讓你再也不用實(shí)現(xiàn)onActivityResult()
這篇文章主要給大家介紹了關(guān)于Android中兩個(gè)類讓你再也不用實(shí)現(xiàn)onActivityResult()的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧2018-08-08