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);
}
}

代码能够在http://download.csdn.net/detail/baidu_nod/7622677下载

android取得所在位置的经纬度的更多相关文章

  1. 百度地图中使用mouseover事件获取经纬度时无法拿到鼠标所在位置的经纬度。

    用百度2.0的话使用mousemove 鼠标在地图区域移动过程中触发此事件.mouseover参数e中没有point参数

  2. android百度地图开发之自动定位所在位置与固定位置进行驾车,步行,公交路线搜索

    最近跟着百度地图API学地图开发,先是学了路径搜索,对于已知坐标的两点进行驾车.公交.步行三种路径的搜索(公交路径运行没效果,待学习中),后来又 学了定位功能,能够获取到自己所在位置的经纬度,但当将两 ...

  3. Android中通过GPS或NetWork获取当前位置的经纬度

    今天在Android项目中要实现一个通过GPS或NetWork来获取当前移动终端设备的经纬度功能.要实现该功能要用到Android Framework 中的 LocationManager 类.下面我 ...

  4. android EditText插入字符串到光标所在位置

    EditText mTextInput=(EditText)findViewById(R.id.input);//EditText对象 int index = mTextInput.getSelect ...

  5. GPS获取Location 获取所在地点的经纬度

    利用手机获取所在地点的经纬度: Location 在Android 开发中还是经常用到的,比如 通过经纬度获取天气,根据Location 获取所在地区详细Address (比如Google Map 开 ...

  6. Android开发之位置定位详解与实例解析(GPS定位、Google网络定位,BaiduLBS(SDK)定位)

    在android开发中地图和定位是很多软件不可或缺的内容,这些特色功能也给人们带来了很多方便.定位一般分为三种发方案:即GPS定位.Google网络定位以及基站定位 最简单的手机定位方式当然是通过GP ...

  7. [置顶] xamarin android使用gps定位获取经纬度

    看了文章你会得出以下几个结论 1.android定位主要有四种方式GPS,Network(wifi定位.基站定位),AGPS定位 2.绝大部分android国产手机使用network进行定位是没有作用 ...

  8. ionic 获取手机所在位置

    之前项目中需要使用到定位功能,前边的文章提到的坐标位置是有问题的,是国际坐标,国内的环境使用google地图会出现问题,所以需要使用国内的地图进行坐标解析,因为国内和国外的坐标体系不一致,需要通过转换 ...

  9. [译]:Xamarin.Android平台功能——位置服务

    返回索引目录 原文链接:Location Services. 译文链接:Xamarin.Android平台功能--位置服务 本部分介绍位置服务以及与如何使用位置提供商服务 Location Servi ...

随机推荐

  1. POJ 3630 Phone List(trie树的简单应用)

    题目链接:http://poj.org/problem?id=3630 题意:给你多个字符串,如果其中任意两个字符串满足一个是另一个的前缀,那么输出NO,否则输出YES 思路:简单的trie树应用,插 ...

  2. C++内置类型对象之间的转换

    C++定义了一组内置类型对象之间的标准转换,在必要时它们被编译器隐式地应用到对象上. 隐式类型转换发生在下列这些典型情况下. 1. 在混合类型的算数表达式中 规则:在这种情况下最宽的数据类型成为目标转 ...

  3. Spring学习之注入方式

    我们知道,Spring对象属性的注入方式有两种:设值注入和构造注入. 假设有个类为People,该对象包含三个属性,name和school还有age,这些属性都有各自的setter和getter方法, ...

  4. php 计算多维数组中所有值的总和

    php 内置函数 array_sum() 函数返回数组中所有值的总和,只能返回一维数组的总和: 计算多维数组所有值的和就要自定义函数了: function get_sum($array) { $num ...

  5. Git Command Summary (Updated)

    取得Git仓库 初始化一个版本仓库 git init Clone远程版本库 git clone git@xbc.me:wordpress.git 添加远程版本库origin,语法为 git remot ...

  6. mybatis字段名与类属性名不相同的冲突

    新建一个表 CREATE TABLE orders( order_id INT PRIMARY KEY AUTO_INCREMENT, order_no ), order_price FLOAT ); ...

  7. (iOS)推送常见问题

    1.为什么启动的时候出现 Did Fail To Register For Remote Notifications With Error的错误程序运行的时候出现下面的错误信息: did Fail T ...

  8. DLL与EXE之间的通讯调用 以及 回调函数的线程执行空间

    dll 与 exe 之间的通讯方式有很多种, 本文采用回调函数的方法实现, 本文也将研究多线程,多模块的情况下,回调函数所在的线程, 啥也不说了,先附上代码: 下面的是dll模块的的, dll的工程文 ...

  9. 没有开发者账号,如何解锁wp8设备

    原文 http://www.cnblogs.com/vsdot/p/3263454.html 问题的引入 好了,问题的由来是这样的,我想把我开发的wp8手机部署到手机上,可是竟然出现了下面的问题: [ ...

  10. C#)Windows Shell 编程系列5 - 获取图标

    原文 C#)Windows Shell 编程系列5 - 获取图标 (本系列文章由柠檬的(lc_mtt)原创,转载请注明出处,谢谢-) 接上一节:(C#)Windows Shell 编程系列4 - 上下 ...