Android 高手进阶教程(十四)之----Android Location的使用!!

来源:互联网 发布:警用强光手电 淘宝 编辑:程序博客网 时间:2024/04/29 09:10

大家好,今天说说Location , Location 在Android 开发中还是经常用到的,比如 通过经纬度获取天气,根据Location 获取所在地区详细Address (比如Google Map 开发).等。而在Android 中通过LocationManager 来获取Location .通常获取Location 有GPS 获取,WIFI 获取。

我今天做一个简单的小Demo ,来教大家如何获取Location ,从而获取经纬度。下一节将教大家通过Location 来获取Address .

 

首先第一步:

 

创建一个Android 工程命名为LocationDemo .

 

第二步:修改main.xml 代码如下:

[java] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView   
  8.     android:id="@+id/longitude"   
  9.     android:layout_width="fill_parent"   
  10.     android:layout_height="wrap_content"   
  11.     android:text="longitude:"  
  12.     />  
  13. <TextView  
  14.     android:id="@+id/latitude"    
  15.     android:layout_width="fill_parent"   
  16.     android:layout_height="wrap_content"   
  17.     android:text="latitude:"  
  18.     />  
  19. </LinearLayout>  

 

第三步:修改LocationDemo.java ,代码如下:

[java] view plaincopy
  1. package com.android.tutor;  
  2. import android.app.Activity;  
  3. import android.content.Context;  
  4. import android.location.Location;  
  5. import android.location.LocationManager;  
  6. import android.os.Bundle;  
  7. import android.widget.TextView;  
  8. public class LocationDemo extends Activity {  
  9.      
  10.     private TextView longitude;  
  11.     private TextView latitude;  
  12.     @Override  
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.           
  17.         longitude = (TextView)findViewById(R.id.longitude);  
  18.         latitude = (TextView)findViewById(R.id.latitude);  
  19.           
  20.         Location mLocation = getLocation(this);  
  21.           
  22.         longitude.setText("Longitude: " + mLocation.getLongitude());  
  23.         latitude.setText("Latitude: " + mLocation.getLatitude());  
  24.     }  
  25.       
  26.     //Get the Location by GPS or WIFI  
  27.     public Location getLocation(Context context) {  
  28.         LocationManager locMan = (LocationManager) context  
  29.                 .getSystemService(Context.LOCATION_SERVICE);  
  30.         Location location = locMan  
  31.                 .getLastKnownLocation(LocationManager.GPS_PROVIDER);  
  32.         if (location == null) {  
  33.             location = locMan  
  34.                     .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);  
  35.         }  
  36.         return location;  
  37.     }  
  38. }  

 

第四步:增加权限,修改AndroidManifest.xml 代码如下(第16行为所增行):

[java] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="com.android.tutor"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  7.         <activity android:name=".LocationDemo"  
  8.                   android:label="@string/app_name">  
  9.             <intent-filter>  
  10.                 <action android:name="android.intent.action.MAIN" />  
  11.                 <category android:name="android.intent.category.LAUNCHER" />  
  12.             </intent-filter>  
  13.         </activity>  
  14.     </application>  
  15.     <uses-sdk android:minSdkVersion="7" />  
  16.     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>  
  17. </manifest>   

 

第五步:运行LocationDemo 工程,所得效果如下(真机深圳测试):

 

0 0
原创粉丝点击