android取得所在位置的经纬度
android提供了LocationManager来取得位置,用LocationListener来监听位置的变化
先做一些初始化工作:
/** latitude and longitude of current location*/
public static String mLat = "";
public static String mLon = ""; /** time out for GPS location update */
private Timer mGpsTimer = new Timer();
/** TimerTask for time out of GPS location update */
private GpsTimeOutTask mGpsTimeOutTask = new GpsTimeOutTask();
/** GPS location update time out in milliseconds*/
private long mGpsTimeOut = 180000;//3 minutes
<span style="white-space:pre"> </span>public void initiLocationUtil (Context context, LocationObsever locationobsever){
mLocationObsever = locationobsever;
mContext = context;
mLocationManager = (LocationManager)mContext.getSystemService(Context.LOCATION_SERVICE);
mLocationListener = new MyLocationListener();
}
<span style="white-space:pre"> </span>public void RefreshGPS(boolean calledByCreate){
mLocationManager.removeUpdates(mLocationListener);
boolean providerEnable = true;
boolean showLocationServiceDisableNotice = true;
//看是否有GPS权限
if(mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
//開始进行定位 mLocationListener为位置监听器
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
0,
0,
mLocationListener);
showLocationServiceDisableNotice = false;
//start time out timer
mGpsTimer = new Timer();
mGpsTimeOutTask = new GpsTimeOutTask();
mGpsTimer.schedule(mGpsTimeOutTask, mGpsTimeOut);
}
if(mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
0,
0,
mLocationListener);
showLocationServiceDisableNotice = false;
providerEnable = true;
}
if(providerEnable){
if(mLocationObsever != null){
mLocationObsever.notifyChange(REFRESHGPS_COMPLETED, null);
}
}else{
if(mLocationObsever != null){
mLocationObsever.notifyChange(REFRESHGPS_NOPROVIDER, null);
}
}
if(showLocationServiceDisableNotice){
showLocationServiceDisabledDialog();
}
}
监听器:
private class MyLocationListener implements LocationListener{
private boolean mLocationReceived = false;
@Override
public void onLocationChanged(Location location) {
if(location != null && !mLocationReceived){
mLocationReceived = true;
String lon = String.valueOf(location.getLongitude());
String lat = String.valueOf(location.getLatitude());
if(mLocationObsever != null){
mLocationObsever.notifyChange(DEFAULT_LOCATION_COMPLETED, lat+","+lon);
}
}else if(location == null){
if(mLocationObsever != null){
mLocationObsever.notifyChange(GETLOCATION_FAILED, null);
}
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
//if GPS provider is not accessible, try network provider
if(provider.equals(LocationManager.GPS_PROVIDER) && status != LocationProvider.AVAILABLE){
if(mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
0,
0,
mLocationListener);
}else{
mLocationManager.removeUpdates(mLocationListener);
if(mLocationObsever != null){
mLocationObsever.notifyChange(STATUS_CHANGED, null);
}
}
}
}
}
这里用了一个Timer,3分钟后又一次去取一次位置:
private Handler mGpsTimerHandler = new Handler() {
public void handleMessage(Message msg) {
if (mLocationManager == null) {
return;
}
if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
System.out.println("=====use network to get location");
mLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0,
mLocationListener);
} else {
mLocationManager.removeUpdates(mLocationListener);
// mLocationObsever.notifyChange(SETADDLOCATIONBUTTONSTATE_1_SETLOCATIONDES_1,null);
if (mLocationObsever != null) {
mLocationObsever.notifyChange(GPSTIMEOUT, null);
}
}
}
};
界面退出的时候要关掉GPS
/**
* cancel operations of refreshing GPS
*/
public void cancelRefreshGPS(){
if(mLocationManager != null){
mLocationManager.removeUpdates(mLocationListener);
} if(mLocationObsever != null){
mLocationObsever.notifyChange(CANCELGPS_COMPLETED, null);
}
} public void destroy (){
if(mLocationManager != null){
mLocationManager.removeUpdates(mLocationListener);
} if(mGpsTimer != null){
mGpsTimer.cancel();
} cancelRefreshGPS(); mContext = null;
mLocationObsever = null; mLocationBuildingList = null; System.gc();
}
截图是
点击MAP的时候,假设採用google map,必须使用sdk带有google api,然后在application中增加<uses-library android:name="com.google.android.maps" />
然后xml是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:gravity="center_horizontal"
android:orientation="vertical" > <include layout="@layout/title_list" /> <View
android:id="@+id/line"
android:layout_width="fill_parent"
android:layout_height="2dip"
android:layout_below="@id/title"
android:background="@drawable/rc_list_divider" /> <com.google.android.maps.MapView
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="06nx-Rzpy8WU16_gjO8ZbtRYYY-junnxNArrxFg" /> </LinearLayout>
代码是:
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.around_map);
mTextView = (TextView)findViewById(R.id.title_text);
mTextView.setText("地图"); Intent intent = getIntent();
mLatitude = intent.getDoubleExtra("lat", 0.0);
mLongitude = intent.getDoubleExtra("lon", 0.0); mMapView = (MapView) findViewById(R.id.mapview);
mMapView.setClickable(true);
mMapView.setBuiltInZoomControls(true);
mMapView.setSatellite(true);
mapController = mMapView.getController();
// geoPoint = new GeoPoint((int)(mLatitude * 1E6), (int)(mLongitude * 1E6));
geoPoint=new GeoPoint((int)(30.659259*1000000),(int)(104.065762*1000000));
mMapView.displayZoomControls(true); // // 设置地图的初始大小。范围在1和21之间。1:最小尺寸,21:最大尺寸
mapController.setZoom(16);
//
// // 创建MyOverlay对象,用于在地图上绘制图形
MyOverlay myOverlay = new MyOverlay();
mMapView.getOverlays().add(myOverlay);
mapController.animateTo(geoPoint);
} @Override
protected boolean isRouteDisplayed() {
return false;
} class MyOverlay extends Overlay
{
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)
{
Paint paint = new Paint(); //屏幕上文字字体颜色
paint.setColor(Color.RED);
Point screenPoint = new Point(); mapView.getProjection().toPixels(geoPoint, screenPoint);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.location);
// 在地图上绘制图像
canvas.drawBitmap(bmp, screenPoint.x, screenPoint.y, paint);
// 在地图上绘制文字
// canvas.drawText("移动巴士", 10, 100, paint);
return super.draw(canvas, mapView, shadow, when);
}
}
android取得所在位置的经纬度的更多相关文章
- 百度地图中使用mouseover事件获取经纬度时无法拿到鼠标所在位置的经纬度。
用百度2.0的话使用mousemove 鼠标在地图区域移动过程中触发此事件.mouseover参数e中没有point参数
- android百度地图开发之自动定位所在位置与固定位置进行驾车,步行,公交路线搜索
最近跟着百度地图API学地图开发,先是学了路径搜索,对于已知坐标的两点进行驾车.公交.步行三种路径的搜索(公交路径运行没效果,待学习中),后来又 学了定位功能,能够获取到自己所在位置的经纬度,但当将两 ...
- Android中通过GPS或NetWork获取当前位置的经纬度
今天在Android项目中要实现一个通过GPS或NetWork来获取当前移动终端设备的经纬度功能.要实现该功能要用到Android Framework 中的 LocationManager 类.下面我 ...
- android EditText插入字符串到光标所在位置
EditText mTextInput=(EditText)findViewById(R.id.input);//EditText对象 int index = mTextInput.getSelect ...
- GPS获取Location 获取所在地点的经纬度
利用手机获取所在地点的经纬度: Location 在Android 开发中还是经常用到的,比如 通过经纬度获取天气,根据Location 获取所在地区详细Address (比如Google Map 开 ...
- Android开发之位置定位详解与实例解析(GPS定位、Google网络定位,BaiduLBS(SDK)定位)
在android开发中地图和定位是很多软件不可或缺的内容,这些特色功能也给人们带来了很多方便.定位一般分为三种发方案:即GPS定位.Google网络定位以及基站定位 最简单的手机定位方式当然是通过GP ...
- [置顶]
xamarin android使用gps定位获取经纬度
看了文章你会得出以下几个结论 1.android定位主要有四种方式GPS,Network(wifi定位.基站定位),AGPS定位 2.绝大部分android国产手机使用network进行定位是没有作用 ...
- ionic 获取手机所在位置
之前项目中需要使用到定位功能,前边的文章提到的坐标位置是有问题的,是国际坐标,国内的环境使用google地图会出现问题,所以需要使用国内的地图进行坐标解析,因为国内和国外的坐标体系不一致,需要通过转换 ...
- [译]:Xamarin.Android平台功能——位置服务
返回索引目录 原文链接:Location Services. 译文链接:Xamarin.Android平台功能--位置服务 本部分介绍位置服务以及与如何使用位置提供商服务 Location Servi ...
随机推荐
- LINQ实现行列转换
用SQL语句实现行列转换很容易,但也有时候需要在程序中实现,找了好久,发现一篇文章写的挺不错的 http://blog.csdn.net/smartsmile2012/article/details/ ...
- Linux内核学习笔记-1.简介和入门
原创文章,转载请注明:Linux内核学习笔记-1.简介和入门 By Lucio.Yang 部分内容来自:Linux Kernel Development(Third Edition),Robert L ...
- Database SQL script automation management tools investigation
Recently researched about database SQL scripts auto management tools, recorded the results here. Res ...
- jQuery selector 选择器
基本选择器 1. id选择器(指定id元素)将id="one"的元素背景色设置为黑色.(id选择器返单个元素) $(document).ready(function () { $( ...
- springmvc附件上传核心代码
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.Comm ...
- Linux系统之UpStart
子贡问为仁.子曰:“工欲善其事,必先利其器.居是邦也,事其大夫之贤者,友其士之仁者.”——孔子(春秋)<论语·卫灵公> [工欲善其事,必先利其器] 掌握一门技术,知道其发展历程是非常重要的 ...
- 磁盘IO子系统学习资料
1.http://www.ibm.com/developerworks/cn/linux/l-cn-read/ (IBM read系统调用剖析) 2.http://lenky.info/arch ...
- MVC-04 视图(3)
五.Url辅助方法 Url辅助方法与HTML辅助方法很类似,HTML辅助方法用来产生HTML标签,而Url辅助方法则负责用来产生Url网址. @Url.Action("About" ...
- 应用 Valgrind 发现 Linux 程序的内存问题
如何定位应用程序开发中的内存问题,一直是 inux 应用程序开发中的瓶颈所在.有一款非常优秀的 linux 下开源的内存问题检测工具:valgrind,能够极大的帮助你解决上述问题.掌握 valgri ...
- python+opencv
$cd numpy $ sudo python setup.py build $ sudo python setup.py installRunning from numpy source direc ...