前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android LocationManager获取经度与纬度等地理信息

Android LocationManager获取经度与纬度等地理信息

作者头像
砸漏
发布2020-10-22 11:27:15
8060
发布2020-10-22 11:27:15
举报
文章被收录于专栏:恩蓝脚本恩蓝脚本

Android LocationManager获取经度与纬度等地理信息

利用LocationManager实现定位功能

1 实时更新经度,纬度

2 根据经度和纬度获取地理信息(比如:国家,街道等)(略过)

MainActivity如下:

代码语言:javascript
复制
package cc.bb; 
import java.util.Iterator; 
import java.util.List; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.widget.TextView; 
import android.app.Activity; 
import android.content.Context; 
/** 
* Demo描述: 
* 利用LocationManager实现定位功能 
* 1 实时更新经度,纬度 
* 2 根据经度和纬度获取地理信息(比如:国家,街道等)(略过) 
* 
* 
* 注意事项: 
* 0 在测试GPS定位时最好在较为宽广的空间,否则影响定位 
* 1 利用mLocationManager.getLastKnownLocation(GPSProvider)获取Location时常为null. 
*  因为设备定位是需要一定时间的,所以把定位逻辑放在LocationManager的requestLocationUpdates()方法 
*  
* 2 LocationManager.requestLocationUpdates 
*  (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()时取消地理位置的更新. 
* 
* 
* 权限配置: 
* <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_MOCK_LOCATION"/  
* <uses-permission android:name="android.permission.INTERNET"/  
*/ 
public class MainActivity extends Activity { 
private Context mContext; 
private TextView mTextView; 
private LocationManager mLocationManager; 
private LocationListenerImpl mLocationListenerImpl; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
init(); 
initLocationManager(mContext); 
} 
private void init(){ 
mContext=this; 
mTextView=(TextView) findViewById(R.id.textView); 
} 
private void initLocationManager(Context context){ 
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();) { 
String provider = (String) iterator.next(); 
System.out.println("provider="+provider); 
} 
//在此采用GPS的方式获取位置信息 
String GPSProvider=LocationManager.GPS_PROVIDER; 
Location location=mLocationManager.getLastKnownLocation(GPSProvider); 
if (location!=null) { 
double longitude=location.getLongitude(); 
double altitude=location.getAltitude(); 
System.out.println("longitude="+longitude+",altitude="+altitude); 
} else { 
System.out.println("location==null"); 
} 
//注册位置监听 
mLocationListenerImpl=new LocationListenerImpl(); 
mLocationManager.requestLocationUpdates(LocationManager.GPS_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 = location.getLongitude(); 
// 获取纬度 
double altitude = location.getAltitude(); 
String message="经度为:"+longitude+"\n"+"纬度为:"+altitude; 
mTextView.setText(message); 
} 
@Override 
protected void onDestroy() { 
super.onDestroy(); 
if (mLocationManager!=null) { 
mLocationManager.removeUpdates(mLocationListenerImpl); 
} 
} 
} 

main.xml如下:

代码语言:javascript
复制
<RelativeLayout xmlns: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"   
<TextView 
android:id="@+id/textView" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:layout_centerInParent="true" 
android:gravity="center" /  
</RelativeLayout  

如有疑问请留言或者到本站社区交流讨论,本站关于Android开发的文章还有很多,希望大家多多搜索查阅,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档