溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么在Android中使用LocationManager獲取經度與緯度等地理信息

發(fā)布時間:2021-02-19 15:47:57 來源:億速云 閱讀:337 作者:Leah 欄目:移動開發(fā)

怎么在Android中使用LocationManager獲取經度與緯度等地理信息?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

Android LocationManager獲取經度與緯度等地理信息

利用LocationManager實現定位功能  

1 實時更新經度,緯度  

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

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實現定位功能 
 * 1 實時更新經度,緯度 
 * 2 根據經度和緯度獲取地理信息(比如:國家,街道等)(略過) 
 * 
 * 
 * 注意事項: 
 * 0 在測試GPS定位時最好在較為寬廣的空間,否則影響定位 
 * 1 利用mLocationManager.getLastKnownLocation(GPSProvider)獲取Location時常為null. 
 *  因為設備定位是需要一定時間的,所以把定位邏輯放在LocationManager的requestLocationUpdates()方法 
 *  
 * 2 LocationManager.requestLocationUpdates 
 *  (String provider, long minTime, float minDistance, LocationListener listener) 
 *  第一個參數:位置信息的provider,比如GPS 
 *  第二個參數:更新位置信息的時間間隔,單位毫秒 
 *  第三個參數:更新位置信息的距離間隔,單位米 
 *  第四個參數:位置信息變化時的回調 
 *  
 * 3 LocationListener中最重要的回調方法onLocationChanged() 
 *  當minTime和minDistance同時滿足時會調用該方法.文檔說明: 
 *  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米 
 *  那么就會調用該方法. 
 *  
 * 4 在Activity的onDestroy()時取消地理位置的更新. 
 * 
 * 
 * 權限配置: 
 * <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{ 
    //當設備位置發(fā)生變化時調用該方法 
    @Override 
    public void onLocationChanged(Location location) { 
      if (location!=null) { 
        showLocation(location); 
      } 
    } 
 
    //當provider的狀態(tài)發(fā)生變化時調用該方法.比如GPS從可用變?yōu)椴豢捎? 
    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
       
    } 
 
    //當provider被打開的瞬間調用該方法.比如用戶打開GPS 
    @Override 
    public void onProviderEnabled(String provider) { 
       
    } 
 
    //當provider被關閉的瞬間調用該方法.比如關閉打開GPS 
    @Override 
    public void onProviderDisabled(String provider) { 
       
    } 
     
  } 
   
   
  private void showLocation(Location location) { 
    // 獲取經度 
    double longitude = location.getLongitude(); 
    // 獲取緯度 
    double altitude = location.getAltitude(); 
    String message="經度為:"+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>

關于怎么在Android中使用LocationManager獲取經度與緯度等地理信息問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業(yè)資訊頻道了解更多相關知識。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI