Android LocationManager 使用
2012-10-14 23:24
441 查看
private LocationManager locationManager;
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
要实时的得到位置信息,得注册一个location的listener。
private LocationListener locationListener;
locationManager.requestLocationUpdates(“gps”, 1000, 0, locationListener); 每秒更新位置信息,不考虑距离变化。
locationManager.removeUpdates(locationListener); 移除listener
在使用这个locationListener之前,还得先new一下,在位置信息更新时要做的操作都可以在这里实现
locationListener = new LocationListener()
{
// implement necessary methods
public void onLocationChanged(Location location)
{
// TODO Auto-generated method stub
位置信息更新
}
public void onProviderDisabled(String provider)
{
// called when the provider be disabled by user
}
public void onProviderEnabled(String provider)
{
// called when the provider be enabled
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
// TODO Auto-generated method stub
provider状态改变
}
};
要得到位置信息,也可以单独调用getLastKnownLocation
Location m_location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
位置信息中包含着 经纬度,高度,速度,UTC时间,定位精度等有用的信息。
得到了位置信息,下面来看卫星信息。Android下提供了GpsStatus这个类,通过调用此类的一个method getSatellites() ,可以得到接收到的卫星的信息列表Iterable<GpsSatellite> 。当然这些操作也是在一个listener当中来做的:GpsStatus.Listener。GpsStatus的listener也是注册于locationManager:
private GpsStatus.Listener statusListener;
locationManager.addGpsStatusListener(statusListener);
locationManager.removeGpsStatusListener(statusListener);
初始化并实现更新时相应的操作:
private GpsStatus gpsStatus;
statusListener = new GpsStatus.Listener()
{
public void onGpsStatusChanged(int event)
{
// TODO Auto-generated method stub
gpsStatus= locationManager.getGpsStatus(null);
switch(event)
{
case GPS_EVENT_FIRST_FIX:
//第一次定位时间UTC
gpsStatus.getTimeToFirstFix();
break;
case GPS_EVENT_SATELLITE_STATUS:
//得到所有收到的卫星的信息,包括 卫星的高度角、方位角、信噪比、和伪随机号(及卫星编号)
Iterable<GpsSatellite> allSatellites;
allSatellites = gpsStatus.getSatellites();
break;
case GPS_EVENT_STARTED:
//Event sent when the GPS system has started.
break;
case GPS_EVENT_STOPPED:
//Event sent when the GPS system has stopped.
break;
default :
break;
}
}
};
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
要实时的得到位置信息,得注册一个location的listener。
private LocationListener locationListener;
locationManager.requestLocationUpdates(“gps”, 1000, 0, locationListener); 每秒更新位置信息,不考虑距离变化。
locationManager.removeUpdates(locationListener); 移除listener
在使用这个locationListener之前,还得先new一下,在位置信息更新时要做的操作都可以在这里实现
locationListener = new LocationListener()
{
// implement necessary methods
public void onLocationChanged(Location location)
{
// TODO Auto-generated method stub
位置信息更新
}
public void onProviderDisabled(String provider)
{
// called when the provider be disabled by user
}
public void onProviderEnabled(String provider)
{
// called when the provider be enabled
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
// TODO Auto-generated method stub
provider状态改变
}
};
要得到位置信息,也可以单独调用getLastKnownLocation
Location m_location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
位置信息中包含着 经纬度,高度,速度,UTC时间,定位精度等有用的信息。
得到了位置信息,下面来看卫星信息。Android下提供了GpsStatus这个类,通过调用此类的一个method getSatellites() ,可以得到接收到的卫星的信息列表Iterable<GpsSatellite> 。当然这些操作也是在一个listener当中来做的:GpsStatus.Listener。GpsStatus的listener也是注册于locationManager:
private GpsStatus.Listener statusListener;
locationManager.addGpsStatusListener(statusListener);
locationManager.removeGpsStatusListener(statusListener);
初始化并实现更新时相应的操作:
private GpsStatus gpsStatus;
statusListener = new GpsStatus.Listener()
{
public void onGpsStatusChanged(int event)
{
// TODO Auto-generated method stub
gpsStatus= locationManager.getGpsStatus(null);
switch(event)
{
case GPS_EVENT_FIRST_FIX:
//第一次定位时间UTC
gpsStatus.getTimeToFirstFix();
break;
case GPS_EVENT_SATELLITE_STATUS:
//得到所有收到的卫星的信息,包括 卫星的高度角、方位角、信噪比、和伪随机号(及卫星编号)
Iterable<GpsSatellite> allSatellites;
allSatellites = gpsStatus.getSatellites();
break;
case GPS_EVENT_STARTED:
//Event sent when the GPS system has started.
break;
case GPS_EVENT_STOPPED:
//Event sent when the GPS system has stopped.
break;
default :
break;
}
}
};
相关文章推荐
- Android中LocationManager的简单使用,获取当前位置
- [原创]Android中LocationManager的简单使用,获取当前位置
- [原创]Android中LocationManager的简单使用,获取当前位置
- Android中LocationManager的简单使用03
- Android中LocationManager的简单使用02
- Android中LocationManager的简单使用01
- Android中LocationManager的简单使用,获取当前位置[转
- android LocationManager的使用
- Android LocationManager 使用
- Android中LocationManager的简单使用01
- Android中LocationManager的简单使用,获取当前位置(转http://www.javaeye.com/topic/363950)
- android LocationManager的简单使用(获取经纬度信息到textview显示)
- Android中LocationManager的简单使用,获取当前位置
- LocationManager Android自动定位使用以及注意事项(个人)
- Android 百度地图开发(二)--- 定位功能之MyLocationOverlay,PopupOverlay的使用
- [009] 百度地图API之MyLocationOverlay的使用(Android) .
- Android加载器LoaderManager.LoaderCallbacks的使用
- Android 之使用LocalBroadcastManager,源码解析
- android 基于GPS或网络定位学习(LocationManager)
- Android DownloadManager的使用