返回顶部
首页 > 资讯 > 移动开发 >基于Android实现百度地图定位过程详解
  • 540
分享到

基于Android实现百度地图定位过程详解

百度地图地图定位Android 2022-06-06 09:06:50 540人浏览 独家记忆
摘要

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

一、问题描述

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

二、配置环境

1、注册密钥:地址Http://developer.baidu.com/map/

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

 

三、编写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);
    
    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());
    }
  }
  
  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配置信息


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.jereh.baidulocation"
  android:versionCode="1"
  android:versionName="1.0" >
  <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />
  <application
    android:name="com.jereh.baidulocation.MyApplication"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
      android:name="com.jereh.baidulocation.MainActivity"
      android:label="@string/app_name" >
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <cateGory android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    <service
      android:name="com.baidu.location.f"
      android:enabled="true"
      android:process=":remote" >
      <intent-filter>
        <action android:name="com.baidu.location.service_v2.2" >
        </action>
      </intent-filter>
    </service>
  </application>
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >
  </uses-permission>
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >
  </uses-permission>
  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >
  </uses-permission>
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
  </uses-permission>
  <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" >
  </uses-permission>
  <uses-permission android:name="android.permission.READ_PHONE_STATE" >
  </uses-permission>
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
  </uses-permission>
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" >
  </uses-permission>
  <uses-permission android:name="android.permission.READ_LOGS" >
  </uses-permission>
  <uses-permission android:name="android.permission.VIBRATE" />
  <uses-permission android:name="android.permission.WAKE_LOCK" />
  <uses-permission android:name="android.permission.WRITE_SETTINGS" />
</manifest>
您可能感兴趣的文章:android实现百度地图自定义弹出窗口功能Android百度地图定位后获取周边位置的实现代码Android下如何使用百度地图sdkAndroid百度地图实现搜索和定位及自定义图标绘制并点击时弹出泡泡Android 百度地图POI搜索功能实例代码Android百度定位导航之基于百度地图移动获取位置和自动定位Android 调用百度地图api示例Android百度地图自定义公交路线导航Android SDK 百度地图通过poi城市内检索简介接口的使用Android百度地图之方向感应和模式更改


--结束END--

本文标题: 基于Android实现百度地图定位过程详解

本文链接: https://lsjlt.com/news/26374.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作