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. 16进制的颜色怎么转换为我们iOS所用的RGB颜色

    这个我们就是可以直接宏定义到 我们的工程中,这样我们就可以用我们UI小妹,给我们16进制的颜色表示,愉快的编程了... #define UIColorFromRGB(rgbValue) [UIColo ...

  2. python 中文异常问题记录

    头上加入以下内容试试: # -*- coding:utf-8import sysimport osreload(sys)sys.setdefaultencoding( "utf-8" ...

  3. relative、absolute和float

    relative.absolute和float   position:relative和position:absolute都可以改变元素在文档中的位置,都能激活元素的left.top.right.bo ...

  4. Linux中fork()函数详解(转)

    一.fork入门知识 一个进程,包括代码.数据和分配给进程的资源.fork()函数通过系统调用创建一个与原来进程几乎完全相同的进程,也就是两个进程可以做完全相同的事,但如果初始参数或者传入的变量不同, ...

  5. 数据结构之后缀数组suffix array

    在字符串处理当中,后缀树和后缀数组都是非常有力的工具,其中后缀树大家了解得比较多,关于后缀数组则很少见于国内的资料.其实后缀是后缀树的一个非常精巧的替代品,它比后缀树容易编程实现,能够实现后缀树的很多 ...

  6. Microsoft 收购 Apiphany

    StevenMartinMS 2013 年 10 月 23 日上午 10:00 今天,我高兴地宣布我们收购了业界领先的 API 管理交付平台 - Apiphany. 应用程序可扩展性已经不算什么新鲜事 ...

  7. Android学习资料PDF免费大放送,每日更新!有需要的亲看这里

    说明:以前有资料分享的时候,都叫大家留下自己的QQ邮箱,这样被很多的程序猿们认为我是专门来钓你们的QQ邮箱的.为此给大家带来的顾虑,深表抱歉.从现在开始,以后的分享资料我都会放在自己的百度网盘中,提供 ...

  8. android api 中文 (74)—— AdapterView.AdapterContextMenuInfo

    前言 本章内容是android.widget.AdapterView.AdapterContextMenuInfo,版本为Android 2.3 r1,翻译来自"cnmahj",欢 ...

  9. 4种Java引用浅解

    近期研究Java Cache实现,发现使用到了软引用(SoftReference),不太理解,查阅了JDK文档.代码以及几篇文章.做个小结,如有错误,欢迎指正. 之所以想学习一下Java的几种引用类型 ...

  10. python第三方模块

    python相关:1.zeromq网络库:2.twisted框架:twisted:一个基于事件驱动,异步的python高性能网络开发框架:注:什么是基于事件驱动:当(鼠标点击事件)事件注册器注入事件, ...