2023年7月7日发(作者:)
AndroidLocationManager获取经度与纬度等地理信息Android LocationManager获取经度与纬度等地理信息利⽤LocationManager实现定位功能
1 实时更新经度,纬度
2 根据经度和纬度获取地理信息(⽐如:国家,街道等)(略过)
MainActivity如下:package ;
import or;
import ;
import on;
import onListener;
import onManager;
import ;
import ew;
import ty;
import t;
/**
* Demo描述:
* 利⽤LocationManager实现定位功能
* 1 实时更新经度,纬度
* 2 根据经度和纬度获取地理信息(⽐如:国家,街道等)(略过)
*
*
* 注意事项:
* 0 在测试GPS定位时最好在较为宽⼴的空间,否则影响定位
* 1 利⽤tKnownLocation(GPSProvider)获取Location时常为null.
* 因为设备定位是需要⼀定时间的,所以把定位逻辑放在LocationManager的requestLocationUpdates()⽅法
*
* 2 tLocationUpdates
* (String provider, long minTime, float minDistance, LocationListener listener)
* 第⼀个参数:位置信息的provider,⽐如GPS
* 第⼆个参数:更新位置信息的时间间隔,单位毫秒
* 第三个参数:更新位置信息的距离间隔,单位⽶
* 第四个参数:位置信息变化时的回调
*
* 3 LocationListener中最重要的回调⽅法onLocationChanged()
* 当minTime和minDistance同时满⾜时会调⽤该⽅法.⽂档说明:
* The minDistance parameter can also be used to control the
* frequency of location updates. If it is greater than 0 then the
* location provider will only send your application an update when
* the location has changed by at least minDistance meters, AND
* at least minTime milliseconds have passed.
* ⽐如间隔时间(minTime)到了3秒并且移动的距离(minDistance)⼤于了5⽶
* 那么就会调⽤该⽅法.
*
* 4 在Activity的onDestroy()时取消地理位置的更新.
*
*
* 权限配置:
*
*
*
*
*/
public class MainActivity extends Activity {
private Context mContext;
private TextView mTextView;
private LocationManager mLocationManager;
private LocationListenerImpl mLocationListenerImpl;
@Override
protected void onCreate(Bundle savedInstanceState) {
te(savedInstanceState);
setContentView();
init();
initLocationManager(mContext);
}
private void init(){
mContext=this;
mTextView=(TextView) findViewById(ew);
}
private void initLocationManager(Context context){
mLocationManager=(LocationManager)temService(ON_SERVICE);
//获取可⽤的位置信息Provider.即passive,network,gps中的⼀个或⼏个
List
for (Iterator
String provider = (String) ();
n("provider="+provider);
}
//在此采⽤GPS的⽅式获取位置信息
String GPSProvider=_PROVIDER;
Location location=tKnownLocation(GPSProvider);
if (location!=null) {
double longitude=gitude();
double altitude=itude();
n("longitude="+longitude+",altitude="+altitude);
} else {
n("location==null");
}
//注册位置监听
mLocationListenerImpl=new LocationListenerImpl();
tLocationUpdates(_PROVIDER, 3000, 5, mLocationListenerImpl);
}
private class LocationListenerImpl implements LocationListener{
//当设备位置发⽣变化时调⽤该⽅法
@Override
public void onLocationChanged(Location location) {
if (location!=null) {
showLocation(location);
}
}
//当provider的状态发⽣变化时调⽤该⽅法.⽐如GPS从可⽤变为不可⽤.
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
//当provider被打开的瞬间调⽤该⽅法.⽐如⽤户打开GPS
@Override
public void onProviderEnabled(String provider) {
}
//当provider被关闭的瞬间调⽤该⽅法.⽐如关闭打开GPS
@Override
public void onProviderDisabled(String provider) {
}
}
private void showLocation(Location location) {
// 获取经度
double longitude = gitude();
// 获取纬度
double altitude = itude();
String message="经度为:"+longitude+"n"+"纬度为:"+altitude;
t(message);
}
@Override protected void onDestroy() {
roy();
if (mLocationManager!=null) {
Updates(mLocationListenerImpl);
}
}
}
如下: xmlns:tools="/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > android:id="@+id/textView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_centerInParent="true" android:gravity="center" />
如有疑问请留⾔或者到本站社区交流讨论,本站关于Android开发的⽂章还有很多,希望⼤家多多搜索查阅,感谢阅读,希望能帮助到⼤家,谢谢⼤家对本站的⽀持!
发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1688678024a161796.html
评论列表(0条)