1.User Location能做什么?
获取用户的位置;3颗卫星确定位置
追踪用户的移动;
2.User Location的关键API
Location Manager:用于管理Android的用户定位服务;
Location Providers:提供多种定位方式供开发者选择;
GPS卫星进行定位:
需要在AndroidManifest.xml中声明权限:android.permission.ACCESS_FINE_LOCATION
NETWORD定位:
声明权限:android.permission.ACCESS_FINE_LOCATION
或
android.permission.ACCESS_COARSE_LOCATION(较粗糙)
3.获取用户的当前的位置步骤
在androidmanifest.xml文件中声明权限;
获取LocationManager对象;
选择LocationProvider对象;
绑定LocationListtener,当用户位置改变则触发函数;
e.g.
在AndroidManifest.xml中声明权限:android.permission.ACCESS_FINE_LOCATION
</application> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> </manifest>
main.xml中是一个按钮:
<Button android:id="@+id/locationButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="绑定监听器"/>
mainActivity.java其中步骤
获取LocationManager对象;
选择LocationProvider对象;
绑定LocationListtener;
package com.example.locate; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private Button button=null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button=(Button)findViewById(R.id.locationButton); button.setOnClickListener(new ButtonListener());//给button设置监听器 } //实现ButtonListener private class ButtonListener implements OnClickListener { @Override public void onClick(View arg0) { // 得到LocationManager对象 LocationManager locationmanager= (LocationManager)MainActivity.this.getSystemService(Context.LOCATION_SERVICE); //getSystemService用户得到一些系统服务,其返回值是oboject,所以要对其进行转型(LocationManager) //为locationmanager绑定了监听器,参数:使用的定位方式,间隔最小时间,最小距离,监听器 locationmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new TextLocationListener()); } } //locationlistener共包含了四个方法, private class TextLocationListener implements LocationListener{ @Override //location对象代表当前位置,可以从中获取经度和纬度 public void onLocationChanged(Location location) { // TODO Auto-generated method stub System.out.println(location.getLongitude()); System.out.println(location.getLatitude()); } @Override /*Called when the provider is disabled by the user. If requestLocationUpdates is called on an already disabled provider, this method is called immediately.*/ public void onProviderDisabled(String provider) { } @Override //Called when the provider is enabled by the user. public void onProviderEnabled(String provider) { } @Override /*Called when the provider status changes. This method is called when a provider is unable to fetch a location or if the provider has recently become available after a period of unavailability.*/ public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } } }
4.使用用DDMS模拟定位
打开DDMS中的:
点击send则会打印出
改变其longitude和latitude:重新打印信息改变