android使用地图编程,基于Android实现百度地图定位过程详解

本文介绍了如何在Android应用中集成百度地图SDK,实现定位功能。首先,需要下载定位SDK并导入项目。接着,在MyApplication类中初始化LocationClient,设置AK并注册监听器。然后,提供获取定位信息、发送广播和停止定位的方法。在MainActivity中注册广播接收器,监听定位结果并显示。最后,确保AndroidManifest.xml配置正确。
摘要由CSDN通过智能技术生成

一、问题描述

LBS位置服务是android应用中重要的功能,应用越来越广泛,下面我们逐步学习和实现lbs相关的应用如定位、地图、导航等,首先我们看如何基于百度地图实现定位功能

二、配置环境

2、下载定位SDK,并导入SDK如图所示:

1d09a6fd5e05a260417e237af96ca6bc.png

三、编写MyApplication类

编写MyApplication类,为了使用方便我们可以将实现定位的方法封装的Application组件中

封装下列方法

1、  获取定位信息——requestLocationInfo()

2、  通过广播发送位置信息——sendBroadCast()

3、  停止定位——stopLocationClient()

public class MyApplication extends Application{

public LocationClient mLocationClient = null;

public GeofenceClient mGeofenceClient;

public MyLocationListenner myListener = new MyLocationListenner();

public static String TAG = "MyApplication";

private static MyApplication mInstance = null;

@Override

public void onCreate(){

mInstance = this;

mLocationClient = new LocationClient(this);

/**

* 项目的key,自己到官网申请 http://lbsyun.baidu.com/apiconsole/key

*/

mLocationClient.setAK("你的应用Key");

mLocationClient.registerLocationListener(myListener);

mGeofenceClient = new GeofenceClient(this);

super.onCreate();

Log.d(TAG, "... Application onCreate... pid=" + Process.myPid());

}

public static MyApplication getInstance(){

return mInstance;

}

/**

* 停止定位

*/

public void stopLocationClient(){

if (mLocationClient != null && mLocationClient.isStarted()){

mLocationClient.stop();

}

}

/**

* 发起定位

*/

public void requestLocationInfo(){

setLocationOption();

if (mLocationClient != null && !mLocationClient.isStarted()){

mLocationClient.start();

}

if (mLocationClient != null && mLocationClient.isStarted()){

mLocationClient.requestLocation();

}

}

/**

* 设置百度地图的相关参数

*/

private void setLocationOption(){

LocationClientOption option = new LocationClientOption();

option.setOpenGps(true); // 打开gps

option.setCoorType("bd09ll"); // 设置坐标类型

option.setServiceName("com.baidu.location.service_v2.9");

option.setPoiExtraInfo(true);

option.setAddrType("all");

option.setPoiNumber(10);

option.disableCache(true);

mLocationClient.setLocOption(option);

}

/**

* 监听函数,有更新位置的时候,格式化成字符串,输出到屏幕中

*/

public class MyLocationListenner implements BDLocationListener

{

@Override

public void onReceiveLocation(BDLocation location){

if (location == null){

sendBroadCast("定位失败!");

return;

}

sendBroadCast(location.getAddrStr());

}

public void onReceivePoi(BDLocation poiLocation){

if (poiLocation == null){

sendBroadCast("定位失败!");

return;

}

sendBroadCast(poiLocation.getAddrStr());

}

}

/**

* 得到发送广播

* @param address

*/

public void sendBroadCast(String address){

stopLocationClient();

Intent intent = new Intent(MainActivity.LOCATION_BCR);

intent.putExtra("address", address);

sendBroadcast(intent);

}

}

三、 主程序MainActivity

public class MainActivity extends Activity{

public static String TAG = "LocTestDemo";

private BroadcastReceiver broadcastReceiver;

public static String LOCATION_BCR = "location_bcr";

private Button locBtn;

private TextView locInfo;

private MyApplication application;

@Override

protected void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

application=(MyApplication)super.getApplication();

initialize();

initializeViews();

initializeListeners();

}

private void initialize(){

registerBroadCastReceiver();//注册广播

}

private void initializeViews()

{

locBtn = (Button) findViewById(R.id.location);

locInfo = (TextView) findViewById(R.id.location_info);

}

private void initializeListeners()

{

locBtn.setOnClickListener(new OnClickListener(){

@Override

public void onClick(View v){

locInfo.setText("定位中...");

//调用请求定位信息

application.requestLocationInfo();

}

});

}

/**

* 注册一个广播,监听定位结果,接受广播获得地址信息

*/

private void registerBroadCastReceiver()

{

broadcastReceiver = new BroadcastReceiver(){

public void onReceive(Context context, Intent intent){

String address = intent.getStringExtra("address");

locInfo.setText(address);

}

};

IntentFilter intentToReceiveFilter = new IntentFilter();

intentToReceiveFilter.addAction(LOCATION_BCR);

registerReceiver(broadcastReceiver, intentToReceiveFilter);

}

@Override

protected void onDestroy(){

super.onDestroy();

unregisterReceiver(broadcastReceiver);

}

}

四、 AndroidManifest.xml配置信息

package="com.jereh.baidulocation"

android:versionCode="1"

android:versionName="1.0" >

android:minSdkVersion="8"

android:targetSdkVersion="17" />

android:name="com.jereh.baidulocation.MyApplication"

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

android:name="com.jereh.baidulocation.MainActivity"

android:label="@string/app_name" >

android:name="com.baidu.location.f"

android:enabled="true"

android:process=":remote" >

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值