Android LocationManager獲取經(jī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四大組件之Service(服務(wù))實例詳解
這篇文章主要介紹了Android四大組件之Service(服務(wù))的用法,結(jié)合實例形式詳細分析了Service的基本概念,類型,用法與相關(guān)注意事項,需要的朋友可以參考下2016-01-01Android ActionBarActivity設(shè)置全屏無標(biāo)題的方法總結(jié)
這篇文章主要介紹了Android ActionBarActivity設(shè)置全屏無標(biāo)題的相關(guān)資料,需要的朋友可以參考下2017-07-07Android實現(xiàn)Service下載文件,Notification顯示下載進度的示例
本篇文章主要介紹了Android實現(xiàn)Service下載文件,Notification顯示下載進度,具有一定的參考價值,有興趣的可以了解一下。2017-01-01Android自定義View實現(xiàn)餅狀圖帶動畫效果
這篇文章主要為大家詳細介紹了Android自定義View實現(xiàn)餅狀圖帶動畫效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12詳解AndroidStudio JNI +Gradle3.0以上JNI爬坑之旅
這篇文章主要介紹了詳解AndroidStudio JNI +Gradle3.0以上JNI爬坑之旅,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12Android中兩個類讓你再也不用實現(xiàn)onActivityResult()
這篇文章主要給大家介紹了關(guān)于Android中兩個類讓你再也不用實現(xiàn)onActivityResult()的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧2018-08-08