Android编程获取地理位置的经度和纬度实例
本文实例讲述了android编程获取地理位置的经度和纬度。分享给大家供大家参考,具体如下:
在android应用程序中,可以使用locationmanager来获取移动设备所在的地理位置信息。看如下实例:新建android应用程序testlocation。
1、activity_main.xml布局文件
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <textview android:id="@+id/positionview" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </linearlayout>
用于显示获取到的位置信息。
2、mainactivity.java
package com.example.testlocation; import java.util.list; import android.app.activity; import android.content.context; import android.location.location; import android.location.locationlistener; import android.location.locationmanager; import android.os.bundle; import android.view.menu; import android.widget.textview; import android.widget.toast; public class mainactivity extends activity { private textview postionview; private locationmanager locationmanager; private string locationprovider; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //获取显示地理位置信息的textview postionview = (textview) findviewbyid(r.id.positionview); //获取地理位置管理器 locationmanager = (locationmanager) getsystemservice(context.location_service); //获取所有可用的位置提供器 list<string> providers = locationmanager.getproviders(true); if(providers.contains(locationmanager.gps_provider)){ //如果是gps locationprovider = locationmanager.gps_provider; }else if(providers.contains(locationmanager.network_provider)){ //如果是network locationprovider = locationmanager.network_provider; }else{ toast.maketext(this, "没有可用的位置提供器", toast.length_short).show(); return ; } //获取location location location = locationmanager.getlastknownlocation(locationprovider); if(location!=null){ //不为空,显示地理位置经纬度 showlocation(location); } //监视地理位置变化 locationmanager.requestlocationupdates(locationprovider, 3000, 1, locationlistener); } /** * 显示地理位置经度和纬度信息 * @param location */ private void showlocation(location location){ string locationstr = "维度:" + location.getlatitude() +"\n" + "经度:" + location.getlongitude(); postionview.settext(locationstr); } /** * locationlistern监听器 * 参数:地理位置提供器、监听位置变化的时间间隔、位置变化的距离间隔、locationlistener监听器 */ locationlistener locationlistener = new locationlistener() { @override public void onstatuschanged(string provider, int status, bundle arg2) { } @override public void onproviderenabled(string provider) { } @override public void onproviderdisabled(string provider) { } @override public void onlocationchanged(location location) { //如果位置发生变化,重新显示 showlocation(location); } }; @override protected void ondestroy() { super.ondestroy(); if(locationmanager!=null){ //移除监听器 locationmanager.removeupdates(locationlistener); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate the menu; this adds items to the action bar if it is present. getmenuinflater().inflate(r.menu.main, menu); return true; } }
从上面可以看出,获取地理位置信息主要分如下步骤:
(1)获取locationmanager实例,通过getsystemservice方法,传入context.location_service参数。
(2)获取可用的位置提供器,有gps_provider、network_provider、passive_provider三种,前两种比较常用。
(3)将(2)获取到的位置提供器传入locationmanager的方法getlastknownlocation,即可获取location信息。
如果移动设备地理位置不断发生变化,则实时更新需要进行如下步骤:
(4)调用locationmanager的requestlocationupdates方法,第一个参数是位置提供器,第二个参数是监听位置变化的时间间隔(毫秒),第三个参数是监听位置变化的距离间隔(米),第四个参数是locationlistener监听器
(5)当位置发生变化后,就会调用监听器的onlocationchanged方法。
(6)为了省电,节约资源,当程序关闭后,调用locationmanager的removeupdates方法移除监听器。
3、获取权限
修改androidmanifest.xml,添加如下代码:
<uses-permission android:name="android.permission.access_fine_location"/> <uses-permission android:name="android.permission.access_coarse_location" /> <uses-permission android:name="android.permission.access_coarse_location"/>
4、效果
使用模拟器进行测试:点击send
可以使用geocoding api查找具体对应的位置。如下:
(1)修改mainactivity.java
package com.example.testlocation; import java.util.list; import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.client.httpclient; import org.apache.http.client.methods.httpget; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.util.entityutils; import org.json.jsonarray; import org.json.jsonobject; import android.app.activity; import android.content.context; import android.location.location; import android.location.locationlistener; import android.location.locationmanager; import android.os.bundle; import android.os.handler; import android.os.message; import android.view.menu; import android.widget.textview; import android.widget.toast; public class mainactivity extends activity { private textview postionview; private locationmanager locationmanager; private string locationprovider; public static final int show_location = 0; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //获取显示地理位置信息的textview postionview = (textview) findviewbyid(r.id.positionview); //获取地理位置管理器 locationmanager = (locationmanager) getsystemservice(context.location_service); //获取所有可用的位置提供器 list<string> providers = locationmanager.getproviders(true); if(providers.contains(locationmanager.gps_provider)){ //如果是gps locationprovider = locationmanager.gps_provider; }else if(providers.contains(locationmanager.network_provider)){ //如果是network locationprovider = locationmanager.network_provider; }else{ toast.maketext(this, "没有可用的位置提供器", toast.length_short).show(); return ; } //获取location location location = locationmanager.getlastknownlocation(locationprovider); if(location!=null){ //不为空,显示地理位置经纬度 showlocation(location); }else{ toast.maketext(this, "location为空", toast.length_short).show(); } //监视地理位置变化 locationmanager.requestlocationupdates(locationprovider, 3000, 1, locationlistener); } private handler handler = new handler(){ public void handlemessage(message msg){ switch(msg.what){ case show_location: string position = (string) msg.obj; postionview.settext(position); break; default: break; } } }; /** * 显示地理位置经度和纬度信息 * @param location */ private void showlocation(final location location){ /*string locationstr = "维度:" + location.getlatitude() +"\n" + "经度:" + location.getlongitude(); postionview.settext(locationstr);*/ new thread(new runnable() { @override public void run() { try{ //组装反向地理编码的接口位置 stringbuilder url = new stringbuilder(); url.append("http://maps.googleapis.com/maps/api/geocode/json?latlng="); url.append(location.getlatitude()).append(","); url.append(location.getlongitude()); url.append("&sensor=false"); httpclient client = new defaulthttpclient(); httpget httpget = new httpget(url.tostring()); httpget.addheader("accept-language","zh-cn"); httpresponse response = client.execute(httpget); if(response.getstatusline().getstatuscode() == 200){ httpentity entity = response.getentity(); string res = entityutils.tostring(entity); //解析 jsonobject jsonobject = new jsonobject(res); //获取results节点下的位置信息 jsonarray resultarray = jsonobject.getjsonarray("results"); if(resultarray.length() > 0){ jsonobject obj = resultarray.getjsonobject(0); //取出格式化后的位置数据 string address = obj.getstring("formatted_address"); message msg = new message(); msg.what = show_location; msg.obj = address; handler.sendmessage(msg); } } }catch(exception e){ e.printstacktrace(); } } }).start(); } /** * locationlistern监听器 * 参数:地理位置提供器、监听位置变化的时间间隔、位置变化的距离间隔、locationlistener监听器 */ locationlistener locationlistener = new locationlistener() { @override public void onstatuschanged(string provider, int status, bundle arg2) { } @override public void onproviderenabled(string provider) { } @override public void onproviderdisabled(string provider) { } @override public void onlocationchanged(location location) { //如果位置发生变化,重新显示 showlocation(location); } }; @override protected void ondestroy() { super.ondestroy(); if(locationmanager!=null){ //移除监听器 locationmanager.removeupdates(locationlistener); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate the menu; this adds items to the action bar if it is present. getmenuinflater().inflate(r.menu.main, menu); return true; } }
(2)修改androidmanifest.xml
<uses-permission android:name="android.permission.access_fine_location"/> <uses-permission android:name="android.permission.access_coarse_location" /> <uses-permission android:name="android.permission.access_coarse_location"/> <uses-permission android:name="android.permission.internet"/>
希望本文所述对大家android程序设计有所帮助。
推荐阅读