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

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

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

 

首先第一步:

 

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

 

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

  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,代码如下:

  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行为所增行):

  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工程,所得效果如下(真机深圳测试):

 


转自:http://blog.csdn.net/android_tutor/article/details/5672911


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值