欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2017年09月04日 14:57:29   作者:lfdfhl  
這篇文章主要介紹了Android LocationManager獲取經(jīng)度與緯度等地理信息的相關(guān)資料,希望通過本站大家能掌握這樣的知識,需要的朋友可以參考下

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)文章

最新評論