利用LocationManager获取经度,纬度等地理信息完整示例

packagecc.bb;importjava.util.Iterator;importjava.util.List;importandroid.location.Location;importandroid.location.LocationListener;importandroid.location.LocationManager;importandroid.os.Bundle;importandroid.widget.TextView;importandroid.app.Activity;importandroid.content.Context;/***Demo描述:*利用LocationManager实现定位功能*1实时更新经度,纬度*2根据经度和纬度获取地理信息(比如:国家,街道等)(略过)***注意事项:*0在测试GPS定位时最好在较为宽广的空间,否则影响定位*1利用mLocationManager.getLastKnownLocation(GPSProvider)获取Location时常为null.*因为设备定位是需要一定时间的,所以把定位逻辑放在LocationManager的requestLocationUpdates()方法**2LocationManager.requestLocationUpdates*(Stringprovider,longminTime,floatminDistance,LocationListenerlistener)*第一个参数:位置信息的provider,比如GPS*第二个参数:更新位置信息的时间间隔,单位毫秒*第三个参数:更新位置信息的距离间隔,单位米*第四个参数:位置信息变化时的回调**3LocationListener中最重要的回调方法onLocationChanged()*当minTime和minDistance同时满足时会调用该方法.文档说明:*TheminDistanceparametercanalsobeusedtocontrolthe*frequencyoflocationupdates.Ifitisgreaterthan0thenthe*locationproviderwillonlysendyourapplicationanupdatewhen*thelocationhaschangedbyatleastminDistancemeters,AND*atleastminTimemillisecondshavepassed.*比如间隔时间(minTime)到了3秒并且移动的距离(minDistance)大于了5米*那么就会调用该方法.**4在Activity的onDestroy()时取消地理位置的更新.***权限配置:*<uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION"/>*<uses-permissionandroid:name="android.permission.ACCESS_COARSE_LOCATION"/>*<uses-permissionandroid:name="android.permission.ACCESS_MOCK_LOCATION"/>*<uses-permissionandroid:name="android.permission.INTERNET"/>*/publicclassMainActivityextendsActivity{privateContextmContext;privateTextViewmTextView;privateLocationManagermLocationManager;privateLocationListenerImplmLocationListenerImpl;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);init();initLocationManager(mContext);}privatevoidinit(){mContext=this;mTextView=(TextView)findViewById(R.id.textView);}privatevoidinitLocationManager(Contextcontext){mLocationManager=(LocationManager)context.getSystemService(Context.LOCATION_SERVICE);//获取可用的位置信息Provider.即passive,network,gps中的一个或几个List<String>providerList=mLocationManager.getProviders(true);for(Iterator<String>iterator=providerList.iterator();iterator.hasNext();){Stringprovider=(String)iterator.next();System.out.println("provider="+provider);}//在此采用GPS的方式获取位置信息StringGPSProvider=LocationManager.GPS_PROVIDER;Locationlocation=mLocationManager.getLastKnownLocation(GPSProvider);if(location!=null){doublelongitude=location.getLongitude();doublealtitude=location.getAltitude();System.out.println("longitude="+longitude+",altitude="+altitude);}else{System.out.println("location==null");}//注册位置监听mLocationListenerImpl=newLocationListenerImpl();mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,3000,5,mLocationListenerImpl);}privateclassLocationListenerImplimplementsLocationListener{//当设备位置发生变化时调用该方法@OverridepublicvoidonLocationChanged(Locationlocation){if(location!=null){showLocation(location);}}//当provider的状态发生变化时调用该方法.比如GPS从可用变为不可用.@OverridepublicvoidonStatusChanged(Stringprovider,intstatus,Bundleextras){}//当provider被打开的瞬间调用该方法.比如用户打开GPS@OverridepublicvoidonProviderEnabled(Stringprovider){}//当provider被关闭的瞬间调用该方法.比如关闭打开GPS@OverridepublicvoidonProviderDisabled(Stringprovider){}}privatevoidshowLocation(Locationlocation){//获取经度doublelongitude=location.getLongitude();//获取纬度doublealtitude=location.getAltitude();Stringmessage="经度为:"+longitude+"\n"+"纬度为:"+altitude;mTextView.setText(message);}@OverrideprotectedvoidonDestroy(){super.onDestroy();if(mLocationManager!=null){mLocationManager.removeUpdates(mLocationListenerImpl);}}}

main.xml中如下:

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/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"><TextViewandroid:id="@+id/textView"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_centerInParent="true"android:gravity="center"/></RelativeLayout>


其他精彩文章文章

Android KSOAP2调用.net webservicejQuery教程(8)-DOM树操作之使用反向插入方法android学习笔记(34)使用AlertDialog创建简单对话框android学习笔记(33)画廊视图(Gallery)的功能和用法android navidgation drawer 在导航抽屉中如何改变List选中项的...


更多关于android开发文章