码迷,mamicode.com
首页 > 移动开发 > 详细

android-用户定位-<十五>

时间:2015-02-22 18:26:46      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:

1.User Location

  • 获取用户的位置
  • 追踪用户的移动

2.User Location的API

   1.LocationManager  :用于管理android的用户定位服务

   2.LocationProvider :提供多种定位方式给开发者选择

   3.LocationListener : 监听位置是否改变,provider是否可用或改变等信息

3.LocationProvider(定位方式的分类)
   1.GPS定位:

     使用GPS卫星进行定位,需要在清单文件中添加权限:android.permission.ACCESS_FINE_LOCATION

   2.NetWork定位:

     使用信号接收塔和WI-FI介入点进行定位,需要在清单文件中添加权限:上面那个(推荐)或

     android.permission.ACCESS_COARSE_LOCATION

4.代码步骤

     1.在清单文件中添加权限:android.permission.ACCESS_FINE_LOCATION

     2.获取LocationManager对象:

          LocationManager locationManager = (LocationManager)MainActivity.this.getSystemService(Context.LOCATION_SERVICE);

     3.选择LocationProvider:

          locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener());

     4.绑定LocationListener对象:

            LocationListener接口:

                                onLocationChanged(Location location);

                                onProviderDisabled(String provider);

                                onProviderEnabled(String provider);

                                onStatusChanged(String provider, int status, Bundle extras) ;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    private Button button = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button = (Button)findViewById(R.id.locationButtonId);
        button.setOnClickListener(new ButtonListener());
    }
    
    
    private class ButtonListener implements OnClickListener{

        @Override
        public void onClick(View v) {
            //得到LocationManager对象
            LocationManager locationManager = (LocationManager)MainActivity.this.getSystemService(Context.LOCATION_SERVICE);
            //1.定义当前所使用的Location Provider
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new TestLocationListener());
        }
    }
    
    private class TestLocationListener implements LocationListener{

        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            System.out.println(location.getLongitude());
            System.out.println(location.getLatitude());
        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub
            
        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub
            
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub
            
        }
        
    }
}

 


   

android-用户定位-<十五>

标签:

原文地址:http://www.cnblogs.com/beyondbycyx/p/4297611.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!