android的用户定位(一)

一、用户定位涉及到得关键API:

  location manager :管理用户的定位服务

  location provider : 选择哪种途径获得用户位置

二、获取用户位置的途径:

  GPS,需要在AndroidManifest.xml中声明:android.permission.ACCESS_FINE_LOCATION

  NETWORK:信号塔或wifi,需要在androidMmanifest.xml中申明:android.permission.ACCESS_FINE_LOCATION

                          或 android.permisson.ACCESS_COARSE_LOCATION

三、实现的一般步骤:

  1、在AndroidManifest.xml里定义权限

  2、获取Location manager对象

  3、选择location provider

  4、绑定locationListener (位置移动时会触发某个时间)

四、代码参考:

  AndroidManifest.xml     

View Code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="max.userLocation"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".UserLocation"
            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>

</manifest>

  main.xml

View Code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button 
        android:id="@+id/buttonId"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="绑定监听器" />    
    

</LinearLayout>

userLocation.java

View Code
 1 package max.userLocation;
 2 
 3 import android.app.Activity;
 4 import android.location.Location;
 5 import android.location.LocationListener;
 6 import android.location.LocationManager;
 7 import android.os.Bundle;
 8 import android.view.View;
 9 import android.view.View.OnClickListener;
10 import android.widget.Button;
11 
12 public class UserLocation extends Activity {
13     /** Called when the activity is first created. */
14     Button userLocationBtn = null;
15     
16     @Override
17     public void onCreate(Bundle savedInstanceState) {
18         super.onCreate(savedInstanceState);
19         setContentView(R.layout.main);
20         userLocationBtn = (Button) findViewById(R.id.buttonId);
21         userLocationBtn.setOnClickListener(new userLocationBtnListener());
22     }
23     class userLocationBtnListener implements OnClickListener {
24 
25         @Override
26         public void onClick(View arg0) {
27             // TODO Auto-generated method stub
28             LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
29             locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new userLocationListener());
30         }
31         
32     }
33     class userLocationListener implements LocationListener {
34 
35         @Override
36         public void onLocationChanged(Location arg0) {
37             // TODO Auto-generated method stub
38             System.out.println("经度:" + arg0.getLongitude());
39             System.out.println("纬度:" + arg0.getLatitude());
40             
41         }
42 
43         @Override
44         public void onProviderDisabled(String provider) {
45             // TODO Auto-generated method stub
46             
47         }
48 
49         @Override
50         public void onProviderEnabled(String provider) {
51             // TODO Auto-generated method stub
52             
53         }
54 
55         @Override
56         public void onStatusChanged(String provider, int status, Bundle extras) {
57             // TODO Auto-generated method stub
58             
59         }
60         
61     }
62 }

 

 

 

posted on 2012-06-26 14:04  木子小黑 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lidl/archive/2012/06/26/2563609.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值