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. private TextView longitude;
  10. private TextView latitude;
  11. @Override
  12. public void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.main);
  15. longitude = (TextView)findViewById(R.id.longitude);
  16. latitude = (TextView)findViewById(R.id.latitude);
  17. Location mLocation = getLocation(this);
  18. longitude.setText("Longitude: " + mLocation.getLongitude());
  19. latitude.setText("Latitude: " + mLocation.getLatitude());
  20. }
  21. //Get the Location by GPS or WIFI
  22. public Location getLocation(Context context) {
  23. LocationManager locMan = (LocationManager) context
  24. .getSystemService(Context.LOCATION_SERVICE);
  25. Location location = locMan
  26. .getLastKnownLocation(LocationManager.GPS_PROVIDER);
  27. if (location == null) {
  28. location = locMan
  29. .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  30. }
  31. return location;
  32. }
  33. }

第四步:增加权限,修改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 工程,所得效果如下(真机深圳测试):

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值