本文主要介绍了Android项目集成百度地图API,使用AlarmManager定时调用Service,在Service中请求坐标更新,并通过坐标得到省、市和县三级地理位置信息的方法。

程序结构很简单,先做简单介绍,后面详细说明:

MainActivity.java是创建项目自带的,不用管; BMapApiDemoApp.java是大部分是百度自带demo文件没有改动,只是在里面添加了Alarm相关的代码; GetLocationService.java是Alarm启动的Service类,在这个类中请求位置更新; GeoCoderParser.java是用于接收地理位置信息的回调类。 百度地图API

集成百度地图API的方式很简单,可以上官网http://developer.baidu.com/map/sdk-android.htm下载相应的库文件和demo。具体的集成方法这里不再赘述,仅仅讨论一下BMapApiDemoApp这个类。代码如下:

 public class BMapApiDemoApp extends Application {

      private static final String TAG = "BMapApiDemoApp";

      static BMapApiDemoApp mDemoApp;

      BMapManager mBMapMan = null;

      String mStrKey = "Your key here!!";
boolean m_bKeyRight = true; static class MyGeneralListener implements MKGeneralListener {
@Override
public void onGetNetworkState(int iError) {
Log.d("MyGeneralListener", "onGetNetworkState error is " + iError);
Toast.makeText(BMapApiDemoApp.mDemoApp.getApplicationContext(),
"Network error", Toast.LENGTH_LONG).show();
} @Override
public void onGetPermissionState(int iError) {
Log.d("MyGeneralListener", "onGetPermissionState error is "
+ iError);
if (iError == MKEvent.ERROR_PERMISSION_DENIED) {
Toast.makeText(BMapApiDemoApp.mDemoApp.getApplicationContext(),
"Permission deny! Add key in BMapApiDemoApp.java!", Toast.LENGTH_LONG).show();
BMapApiDemoApp.mDemoApp.m_bKeyRight = false;
}
}
} @Override
public void onCreate() {
mDemoApp = this;
mBMapMan = new BMapManager(this);
boolean isSuccess = mBMapMan
.init(this.mStrKey, new MyGeneralListener());
if (isSuccess) {
mBMapMan.getLocationManager().setNotifyInternal(, );
startAlarm();
} else {
}
super.onCreate();
} private void startAlarm() {
Log.d(TAG, "start alarm");
AlarmManager am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent collectIntent = new Intent(this, GetLocationService.class);
PendingIntent collectSender
= PendingIntent.getService(this, , collectIntent, );
am.cancel(collectSender);
am.setRepeating(AlarmManager.ELAPSED_REALTIME
, SystemClock.elapsedRealtime()
, *
, collectSender);
} @Override
public void onTerminate() {
// TODO Auto-generated method stub
if (mBMapMan != null) {
mBMapMan.destroy();
mBMapMan = null;
}
super.onTerminate();
} }

主要关注如下几点:

BMapApiDemoApp覆盖了Application类,并在onCreate()函数中初始化BMapManager对象,这样程序就可以共享这个对象。 BMapManager类主要有4个函数,init(), start(), stop() 和destory()。一般在Application类的onCreate()函数中调用init,在onDestory()函数中调用destory,这样在需要使用其他API的地方只需要取到这个对象并调用start和stop方法就可以了。但是为了保证BMapManager对象存在,在其他调用的类中还是会做相应的检查。 文件中变量mStrKey保存的是需要去官网申请的API的key,申请很容易。 我在onCreate()函数中还调用了startAlarm()函数。这个函数的作用是每隔10s调用GetLocationService类。 GetLocationService

GetLocationService主要覆写了两个函数onStartCommand()和onCreate(),代码如下:

 public class GetLocationService extends Service {

      protected static final String TAG = null;
LocationListener mLocationListener = null;
BMapApiDemoApp app = null;
@Override
public void onCreate() {
super.onCreate();
app = (BMapApiDemoApp) this.getApplication();
if (app.mBMapMan == null) {
app.mBMapMan = new BMapManager(getApplication());
app.mBMapMan.init(app.mStrKey,
new BMapApiDemoApp.MyGeneralListener());
}
app.mBMapMan.start();
final MKSearch mMKSearch = new MKSearch();
mMKSearch.init(app.mBMapMan, new GeoCoderParser()); // 注册定位事件
mLocationListener = new LocationListener() { @Override
public void onLocationChanged(Location location) {
Log.d(TAG, "location change");
if (location != null) {
String strLog = String.format("经度:%f\r\n" + "纬度:%f",
location.getLongitude(),
location.getLatitude());
Log.d(TAG, strLog);
GeoPoint point = new GeoPoint((int)(location.getLatitude() * 1E6), (int)(location.getLongitude() * 1E6));
mMKSearch.reverseGeocode(point); app.mBMapMan.getLocationManager().removeUpdates(mLocationListener);
app.mBMapMan.stop();
}
}
};
} @Override
public int onStartCommand(Intent intent, int flag, int startId) {
app.mBMapMan.getLocationManager().requestLocationUpdates(
mLocationListener);
app.mBMapMan.start();
return Service.START_NOT_STICKY;
} @Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
} }

其中:

在onCreate()中取到在BMapApiDemoApp里定义的BMapManager对象,并检查是否存在,不存在就初始化一个; 在onStartCommand()中注册LocationListener并调用BMapManager的start函数; LocationListener是在onCreate()中定义的,mMKSearch.reverseGeocode()传入GeoPoint对象,搜索之后的返回值通过GeoCoderParser类取到;当得到Location信息之后我调用了removeUpdates和stop函数停止请求更新。

 GeoPoint point = new GeoPoint((int)(location.getLatitude() * 1E6), (int)(location.getLongitude() * 1E6));
mMKSearch.reverseGeocode(point); app.mBMapMan.getLocationManager().removeUpdates(mLocationListener);
app.mBMapMan.stop();

代码最后执行的时候有一个小bug就是removeUpdate()貌似被执行了两次,没有详细追究,不知道是什么原因。不过也无伤大雅。

GeoCoderParser

最后就是用于接收MKSearch.reverseGeocode()结果的类。

这个类的实现了MKSearchListener接口,接口的函数比较多,但是我们这里只关注onGetAddrResult(MKAddrInfo arg0, int arg1)函数。这个函数有两个参数:

arg1是错误码,搜索失败的时候通过错误码得到失败原因; arg0是传入的地址信息,它是一个MKAddrInfo对象,对象有个字段是addressComponents,这个字段保存了从省、市一直到门牌号的信息。

  public class GeoCoderParser implements MKSearchListener {

      private static final String TAG = "GeoCoderParser";

      @Override
public void onGetAddrResult(MKAddrInfo arg0, int arg1) {
if (arg1 != ) {
String str = String.format("%d", arg1);
Log.d(TAG, str);
return;
}
MKGeocoderAddressComponent address = arg0.addressComponents;
Log.d(TAG, "Location info:" + address.province + address.city + address.district);
} //other functions }

总结

程序的流程大致就是这样,我也是初学,如果哪里写错了欢迎批评指正。

最后给出项目代码的链接:http://files.cnblogs.com/wangfenjin/BaiduMapService.zip 。我没找到博客园哪里可以传附件,所以是通过“文件”那个标签上传的,不知道你们可不可以下载。

项目代码没什么特殊的地方,主要注意一下Manifest文件里面请求相应的权限和注册Service就行了。

Android通过百度地图API用Service和Alarm在后台定时获取地理位置信息的更多相关文章

  1. Android端百度地图API使用详解

    百度地图API简介 百度地图移动版API(Android)是一套基于Android设备的应用程序接口,通过该接口,可以轻松的访问百度服务和数据,构建功能丰富.交互性强的地图应用程序. 百度地图移动版A ...

  2. 【转载】Android端百度地图API使用详解

    转载地址:http://www.cnblogs.com/rocomp/p/4994110.html 百度地图API简介 百度地图移动版API(Android)是一套基于Android设备的应用程序接口 ...

  3. android 基于百度地图api开发定位以及获取详细地址

    一:百度地图开发必须要到百度开发平台android开发api下载相应的库,已经申请百度地图开发key. 二:新建项目baidumaplocation.设计main.xml文件这里注意的是MapView ...

  4. Android 调用百度地图API

    一.到 百度地图开发平台下载SDK http://lbsyun.baidu.com/index.php?title=androidsdk/sdkandev-download 1.点击自定义下载 2.下 ...

  5. Android使用百度地图API实现GPS步行轨迹

    百度地图Android SDK下载:http://developer.baidu.com/map/sdkandev-download.htm 下面是效果: 采样点取得太频繁所以看起来像是一个个点... ...

  6. 【Android接百度地图API】百度地图Demo点击按钮闪退

    运行百度地图自带的BaiduMap_AndroidSDK_v4.1.0_Sample里面的BaiduMapsApiASDemo发现点击上面的按钮会闪退,控制台报的是xml的问题 查了一下,官方文档特别 ...

  7. android笔记---百度地图api应用 (二) 获取公交路线的详细信息

    package com.example.bdtest; import com.baidu.mapapi.MKEvent; import com.baidu.mapapi.MKPlanNode; imp ...

  8. android笔记---百度地图api应用 (一)

    package com.example.bdtest; import com.baidu.mapapi.MKEvent; import com.baidu.mapapi.MKPlanNode; imp ...

  9. android调用百度地图API

    http://blog.csdn.net/lyq8479/article/details/6384428

随机推荐

  1. Spring 团队开源 nohttp,尽可能不用 HTTP

    Spring 团队开源 nohttp 项目,用以查找.替换和阻止 http:// 的使用. 项目是为了在可能使用 https:// 的情况下不使用到 http://,确保不会发生中间人攻击.Sprin ...

  2. Java时间日期格式转换Date转String和String转Date

    Java时间格式转换大全 import java.text.*; import java.util.Calendar; public class VeDate { /** * 获取现在时间 * * @ ...

  3. 【转载】IDEA的这八条配置你一定要改!

    引言 坦白说,我很少写这种操作类型的文章.因为这种文章没啥新意,大家操作步骤肯定是一样的.然而,我答应了我的同事小阳,给她出一篇!毕竟人家打算从Eclipse转IDEA了,于是以示鼓励,写一篇给她!那 ...

  4. 10.01QBXT集训

    a[问题描述]你是能看到第一题的 friends呢.—— hja何大爷对字符串十分有研究,于是天出题虐杀 zhx.何大爷今天为 何大爷今天为 字符串定义了新的权值计算方法.一个由小写母组成,被定义为其 ...

  5. [CodeForces]908D New Year and Arbitrary Arrangement

    设状态f[i][j]表示有i个a,j个ab的期望 发现如果i+j>=k的话就再来一个b就行了. #include <iostream> #include <cstdio> ...

  6. 1.2、使用pip安装Python包

    大多数 Python 包都使用 pip 实用工具安装,使用 virtualenv 创建虚拟环境时会自动安装 pip.激活虚拟环境后,pip 所在的路径会被添加进 PATH. 注:如果你在 Python ...

  7. 2019-04-17 PowerShell基本语法

    打印Hello World ,Hello theDataDigger writeLog ' Hello World'$Name = "theDataDigger"writeLog ...

  8. centos 7.2 安装php56-xml

    linux下, 使用thinkphp的模板标签,如 eq, gt, volist defined, present , empty等 标签时, 报错: used undefined function ...

  9. 学习EXTJS6(5)基本功能-进度条组件

    Ext.ProgressBar有二种模式:手动和自动:手动:自己控制显示.进度.更新.清除.自动只需要调用Wait方法即可. 配置项:   配置项 类型 说明 renderTo String 指定页面 ...

  10. 【codeforces 793B】Igor and his way to work

    [题目链接]:http://codeforces.com/contest/793/problem/B [题意] 给一个n*m大小的方格; 有一些方格可以走,一些不能走; 然后问你从起点到终点,能不能在 ...