12 .Android Location的使用

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

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

第一步:创建一个Android工程命名为LocationDemo

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

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

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

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

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

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

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

LocationDemo


com.miui.frameworks.servicestests (1 Test) [1/1] com.android.server.location.gnss.hal.Gpo4ClientTest#init: FAILED (141ms) STACKTRACE: java.lang.SecurityException: uid 10289 does not have android.permission.ACCESS_COARSE_LOCATION or android.permission.ACCESS_FINE_LOCATION. at android.os.Parcel.createExceptionOrNull(Parcel.java:3011) at android.os.Parcel.createException(Parcel.java:2995) at android.os.Parcel.readException(Parcel.java:2978) at android.os.Parcel.readException(Parcel.java:2920) at android.location.ILocationManager$Stub$Proxy.registerLocationListener(ILocationManager.java:1291) at android.location.LocationManager.requestLocationUpdates(LocationManager.java:1551) at android.location.LocationManager.requestLocationUpdates(LocationManager.java:1234) at android.location.LocationManager.requestLocationUpdates(LocationManager.java:1199) at com.android.server.location.gnss.hal.Gpo4Client.registerPassiveLocationUpdates(Gpo4Client.java:198) at com.android.server.location.gnss.hal.Gpo4Client.init(Gpo4Client.java:78) at com.android.server.location.gnss.hal.Gpo4ClientTest.init(Gpo4ClientTest.java:209) ... 8 trimmed Caused by: android.os.RemoteException: Remote stack trace: at com.android.server.location.LocationPermissions.enforceLocationPermission(LocationPermissions.java:116) at com.android.server.location.LocationManagerService.registerLocationListener(LocationManagerService.java:786) at android.location.ILocationManager$Stub.onTransact(ILocationManager.java:582) at android.os.Binder.execTransactInternal(Binder.java:1285) at android.os.Binder.execTransact(Binder.java:1249)
06-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值