怎么在Android中使用LocationManager獲取經(jīng)度與緯度等地理信息

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

行唐網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)建站,行唐網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為行唐上1000家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的行唐做網(wǎng)站的公司定做!

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

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

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

2 根據(jù)經(jīng)度和緯度獲取地理信息(比如:國(guó)家,街道等)(略過(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ó)家,街道等)(略過(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)用該方法.文檔說(shuō)明: 
 *  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)聽(tīng) 
    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被打開(kāi)的瞬間調(diào)用該方法.比如用戶打開(kāi)GPS 
    @Override 
    public void onProviderEnabled(String provider) { 
       
    } 
 
    //當(dāng)provider被關(guān)閉的瞬間調(diào)用該方法.比如關(guān)閉打開(kāi)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>

關(guān)于怎么在Android中使用LocationManager獲取經(jīng)度與緯度等地理信息問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

網(wǎng)站標(biāo)題:怎么在Android中使用LocationManager獲取經(jīng)度與緯度等地理信息
當(dāng)前地址:http://www.muchs.cn/article6/piehog.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、電子商務(wù)網(wǎng)站改版、網(wǎng)站維護(hù)Google、品牌網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)