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. 5.7.1.3 Global 对象的属性

    Global对象还包含了一些属性,例如,特殊的值undefined.NaN以及Infinity都是Global对象的属性.此外,所有原生引用类型的构造函数,像Object和Function,也都是Gl ...

  2. ISO/IEC 14443协议浅谈

    一. 非接触IC卡简介 非接触IC卡又称射频卡,是射频识别技术和IC卡技术有机结合的产物.它解决了无源(卡中无电源)和免接触这一难题,具有更加方便.快捷的特点,广泛用于电子支付.通道控制.公交收费.停 ...

  3. ID卡学习笔记

    前言: 我也来篇关于当时学习ID卡的笔记.前段时间小区装门禁.一个钮扣型的ID卡就要30块.非常黑心.因为其ID卡的成本也就是1块钱以下.因此我也加入到这方面的研究.用来模拟ID卡的T5557卡成本2 ...

  4. HDU 1090 A+B for Input-Output Practice (II)

    #include <cstdio> int main() { int n,a,b; scanf("%d",&n); ; i<=n; i++) { scan ...

  5. HDOJ 1427(dfs) 速算24点

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1427 思路分析: 题目要求判断是否存在一种运算组合使得4个数的计算结果为24,因为搜索的层次为3层,不 ...

  6. SQL server 数据库视频总结

    用了半个多月的时间把,浙江大学耿建玲老师 数据库视频看了一遍.在看视频之前,曾经接收了一个学生信息管理系统,在学习  学生信息管理系统的时候,对于数据库的部分,总是那么一知半解.带着疑惑来看耿建玲老师 ...

  7. UVAlive 2326 Moving Tables(贪心 + 区间问题)

    The famous ACM (Advanced Computer Maker) Company has rented a floor of a building whose shape is in ...

  8. Sublime 操作技巧

    吐槽一下:刚下载的subime不是等宽字体,空格.表达.字母i什么的都很窄,看着不方便: 根据网上说的换成等宽字体,试了好多种字体,字体变了.但宽度没变. 然后有装了soda,和相应的color-th ...

  9. 使用IDENTITY列属性和Sequence对象

    使用IDENTITY列属性 1. 建立表 Sales.MyOrders USE TSQL2012; IF OBJECT_ID(N'Sales.MyOrders', N'U') IS NOT NULL ...

  10. Jenkins持续集成相关文章整理

    构建iOS持续集成平台(一)——自动化构建和依赖管理 构建iOS持续集成平台(二)——测试框架 构建iOS持续集成平台(三)——CI服务器与自动化部署 使用Jenkins搭建iOS开发的CI服务器 一 ...