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

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

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

首先第一步:

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

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

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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. packagecom.android.tutor;
  2. importandroid.app.Activity;
  3. importandroid.content.Context;
  4. importandroid.location.Location;
  5. importandroid.location.LocationManager;
  6. importandroid.os.Bundle;
  7. importandroid.widget.TextView;
  8. publicclassLocationDemoextendsActivity{
  9. privateTextViewlongitude;
  10. privateTextViewlatitude;
  11. @Override
  12. publicvoidonCreate(BundlesavedInstanceState){
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.main);
  15. longitude=(TextView)findViewById(R.id.longitude);
  16. latitude=(TextView)findViewById(R.id.latitude);
  17. LocationmLocation=getLocation(this);
  18. longitude.setText("Longitude:"+mLocation.getLongitude());
  19. latitude.setText("Latitude:"+mLocation.getLatitude());
  20. }
  21. //GettheLocationbyGPSorWIFI
  22. publicLocationgetLocation(Contextcontext){
  23. LocationManagerlocMan=(LocationManager)context
  24. .getSystemService(Context.LOCATION_SERVICE);
  25. Locationlocation=locMan
  26. .getLastKnownLocation(LocationManager.GPS_PROVIDER);
  27. if(location==null){
  28. location=locMan
  29. .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  30. }
  31. returnlocation;
  32. }
  33. }

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

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <manifestxmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.android.tutor"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
  7. <activityandroid:name=".LocationDemo"
  8. android:label="@string/app_name">
  9. <intent-filter>
  10. <actionandroid:name="android.intent.action.MAIN"/>
  11. <categoryandroid:name="android.intent.category.LAUNCHER"/>
  12. </intent-filter>
  13. </activity>
  14. </application>
  15. <uses-sdkandroid:minSdkVersion="7"/>
  16. <uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION"/>
  17. </manifest>

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


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


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

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

首先第一步:

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

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

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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. packagecom.android.tutor;
  2. importandroid.app.Activity;
  3. importandroid.content.Context;
  4. importandroid.location.Location;
  5. importandroid.location.LocationManager;
  6. importandroid.os.Bundle;
  7. importandroid.widget.TextView;
  8. publicclassLocationDemoextendsActivity{
  9. privateTextViewlongitude;
  10. privateTextViewlatitude;
  11. @Override
  12. publicvoidonCreate(BundlesavedInstanceState){
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.main);
  15. longitude=(TextView)findViewById(R.id.longitude);
  16. latitude=(TextView)findViewById(R.id.latitude);
  17. LocationmLocation=getLocation(this);
  18. longitude.setText("Longitude:"+mLocation.getLongitude());
  19. latitude.setText("Latitude:"+mLocation.getLatitude());
  20. }
  21. //GettheLocationbyGPSorWIFI
  22. publicLocationgetLocation(Contextcontext){
  23. LocationManagerlocMan=(LocationManager)context
  24. .getSystemService(Context.LOCATION_SERVICE);
  25. Locationlocation=locMan
  26. .getLastKnownLocation(LocationManager.GPS_PROVIDER);
  27. if(location==null){
  28. location=locMan
  29. .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  30. }
  31. returnlocation;
  32. }
  33. }

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

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <manifestxmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.android.tutor"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
  7. <activityandroid:name=".LocationDemo"
  8. android:label="@string/app_name">
  9. <intent-filter>
  10. <actionandroid:name="android.intent.action.MAIN"/>
  11. <categoryandroid:name="android.intent.category.LAUNCHER"/>
  12. </intent-filter>
  13. </activity>
  14. </application>
  15. <uses-sdkandroid:minSdkVersion="7"/>
  16. <uses-permissionandroid: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、付费专栏及课程。

余额充值