这一篇文章主要解说的是百度地图的定位功能,然后还有MyLocationOverlay和PopupOverlay两个地图覆盖物的使用。Overlay是“图层”或“覆盖物”的意思,MyLocationOverlay从名字上面理解就是我的位置图层,他可以实如今地图上显示当前位置的图标以及指南针,MyLocationOverlay仅仅负责显示我的位置,位置数据请使用百度定位SDK获取,将获取的位置数据放在一个LocationData结构中并用该结构设置MyLcationOverlay的数据源。就可以创建MyLocationOverlay。PopupOverlay就是弹出窗体图层了,跟PopupWindow类似的东西,以下会介绍他们的用法
定位我们使用的是百度 Android 定位SDKv4.0。我们先了解下定位原理和定位精度
定位原理
使用百度Android定位SDK必须注冊GPS和网络使用权限。定位SDK採用GPS、基站、Wi-Fi信号进行定位。当应用程序向定位SDK发起定位请求时。定位SDK会根据应用的定位因素(GPS、基站、Wi-Fi信号)的实际情况(如是否开启GPS、是否连接网络、是否有信号等)来生成对应定位根据进行定位。
用户能够设置满足自身需求的定位根据:
若用户设置GPS优先。则优先使用GPS进行定位,假设GPS定位未打开或者没有可用位置信息,且网络连接正常。定位SDK则会返回网络定位(即Wi-Fi与基站)的最优结果。为了使获得的网络定位结果更加精确,请打开手机的Wi-Fi开关。
定位精度
了解了百度定位的原理和定位精度之后,接下来我们就来使用百度定位SDKv4.0吧
一 . 导入库文件
在使用百度定位SDKv4.0之前,我们要下载最新的库文件。下载地址:点击下载相关库文件。将liblocSDK4.so文件复制到libs/armeabi文件夹下。
将locSDK4.0.jar文件复制到project的libs文件夹下
二 . 布局文件。一个百度地图控件。加一个手动点击实现定位的button,放在一个相对布局里面,非常easy的布局
-
<?
xml version="1.0" encoding="utf-8"?
>
-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent" >
-
-
<com.baidu.mapapi.map.MapView
-
android:id="@+id/bmapView"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
android:clickable="true" />
-
-
-
<Button
-
android:id="@+id/request"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:layout_alignParentRight="true"
-
android:layout_alignParentTop="true"
-
android:layout_marginRight="10dp"
-
android:layout_marginTop="10dip"
-
android:background="@drawable/custom_loc" />
-
-
</RelativeLayout>
三 . 界面MainActivity代码,先贴上,然后适当解说相关代码,我凝视也比較清楚
-
package com.example.baidumapdemo;
-
-
import android.app.Activity;
-
import android.graphics.Bitmap;
-
import android.os.Bundle;
-
import android.util.Log;
-
import android.view.LayoutInflater;
-
import android.view.View;
-
import android.view.View.MeasureSpec;
-
import android.view.View.OnClickListener;
-
import android.widget.Button;
-
import android.widget.TextView;
-
import android.widget.Toast;
-
-
import com.baidu.location.BDLocation;
-
import com.baidu.location.BDLocationListener;
-
import com.baidu.location.LocationClient;
-
import com.baidu.location.LocationClientOption;
-
import com.baidu.mapapi.BMapManager;
-
import com.baidu.mapapi.MKGeneralListener;
-
import com.baidu.mapapi.map.LocationData;
-
import com.baidu.mapapi.map.MKEvent;
-
import com.baidu.mapapi.map.MapController;
-
import com.baidu.mapapi.map.MapView;
-
import com.baidu.mapapi.map.MyLocationOverlay;
-
import com.baidu.mapapi.map.PopupClickListener;
-
import com.baidu.mapapi.map.PopupOverlay;
-
import com.baidu.platform.comapi.basestruct.GeoPoint;
-
-
public class MainActivity extends Activity {
-
private Toast mToast;
-
private BMapManager mBMapManager;
-
private MapView mMapView = null;
-
private MapController mMapController = null;
-
-
-
-
-
private LocationClient mLocClient;
-
-
-
-
private LocationData mLocData;
-
-
-
-
private LocationOverlay myLocationOverlay = null;
-
-
-
-
private PopupOverlay mPopupOverlay = null;
-
-
private boolean isRequest = false;
-
private boolean isFirstLoc = true;
-
-
-
-
-
private View mPopupView;
-
private BDLocation location;
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
-
mBMapManager = new BMapManager(this);
-
-
-
-
mBMapManager.init("7ae13368159d6a513eaa7a17b9413b4b", new MKGeneralListenerImpl());
-
setContentView(R.layout.activity_main);
-
-
-
((Button) findViewById(R.id.request)).setOnClickListener(new OnClickListener() {
-
-
@Override
-
public void onClick(View v) {
-
requestLocation();
-
}
-
});
-
-
mMapView = (MapView) findViewById(R.id.bmapView);
-
mMapController = mMapView.getController();
-
mMapController.enableClick(true);
-
mMapController.setZoom(14);
-
mMapView.setBuiltInZoomControls(true);
-
-
mMapView.setTraffic(true);
-
-
-
-
-
mPopupView = LayoutInflater.from(this).inflate(R.layout.pop_layout, null);
-
-
-
mPopupOverlay = new PopupOverlay(mMapView ,new PopupClickListener() {
-
-
-
-
-
@Override
-
public void onClickedPopup(int arg0) {
-
-
mPopupOverlay.hidePop();
-
}
-
});
-
-
-
-
mLocClient = new LocationClient(getApplicationContext());
-
mLocClient.registerLocationListener(new BDLocationListenerImpl());
-
-
-
-
-
LocationClientOption option = new LocationClientOption();
-
option.setOpenGps(true);
-
option.setAddrType("all");
-
option.setCoorType("bd09ll");
-
option.setPriority(LocationClientOption.GpsFirst);
-
option.setScanSpan(5000);
-
option.disableCache(false);
-
-
-
-
mLocClient.setLocOption(option);
-
-
-
mLocClient.start();
-
-
-
myLocationOverlay = new LocationOverlay(mMapView);
-
-
-
-
mLocData = new LocationData();
-
myLocationOverlay.setData(mLocData);
-
-
-
mMapView.getOverlays().add(myLocationOverlay);
-
-
-
mMapView.refresh();
-
-
-
}
-
-
-
-
-
-
-
-
public class BDLocationListenerImpl implements BDLocationListener {
-
-
-
-
-
@Override
-
public void onReceiveLocation(BDLocation location) {
-
if (location == null) {
-
return;
-
}
-
-
StringBuffer sb = new StringBuffer(256);
-
sb.append("time : ");
-
sb.append(location.getTime());
-
sb.append("\nerror code : ");
-
sb.append(location.getLocType());
-
sb.append("\nlatitude : ");
-
sb.append(location.getLatitude());
-
sb.append("\nlontitude : ");
-
sb.append(location.getLongitude());
-
sb.append("\nradius : ");
-
sb.append(location.getRadius());
-
if (location.getLocType() == BDLocation.TypeGpsLocation){
-
sb.append("\nspeed : ");
-
sb.append(location.getSpeed());
-
sb.append("\nsatellite : ");
-
sb.append(location.getSatelliteNumber());
-
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
-
sb.append("\naddr : ");
-
sb.append(location.getAddrStr());
-
}
-
-
Log.e("log", sb.toString());
-
-
-
MainActivity.this.location = location;
-
-
mLocData.latitude = location.getLatitude();
-
mLocData.longitude = location.getLongitude();
-
-
mLocData.accuracy = location.getRadius();
-
mLocData.direction = location.getDerect();
-
-
-
myLocationOverlay.setData(mLocData);
-
-
mMapView.refresh();
-
-
-
if(isFirstLoc || isRequest){
-
-
mMapController.animateTo(new GeoPoint(
-
(int) (location.getLatitude() * 1e6), (int) (location
-
.getLongitude() * 1e6)));
-
showPopupOverlay(location);
-
isRequest = false;
-
}
-
isFirstLoc = false;
-
-
}
-
-
-
-
-
@Override
-
public void onReceivePoi(BDLocation poiLocation) {
-
-
}
-
-
}
-
-
-
-
-
private class LocationOverlay extends MyLocationOverlay{
-
-
public LocationOverlay(MapView arg0) {
-
super(arg0);
-
}
-
-
-
-
-
-
@Override
-
protected boolean dispatchTap() {
-
-
showPopupOverlay(location);
-
return super.dispatchTap();
-
}
-
-
}
-
-
-
-
-
-
-
private void showPopupOverlay(BDLocation location){
-
TextView popText = ((TextView)mPopupView.findViewById(R.id.location_tips));
-
popText.setText("[我的位置]\n" + location.getAddrStr());
-
mPopupOverlay.showPopup(getBitmapFromView(popText),
-
new GeoPoint((int)(location.getLatitude()*1e6), (int)(location.getLongitude()*1e6)),
-
15);
-
-
}
-
-
-
-
-
-
public void requestLocation() {
-
isRequest = true;
-
-
if(mLocClient != null && mLocClient.isStarted()){
-
showToast("正在定位......");
-
mLocClient.requestLocation();
-
}else{
-
Log.d("log", "locClient is null or not started");
-
}
-
}
-
-
-
-
-
-
-
-
private void showToast(String msg){
-
if(mToast == null){
-
mToast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
-
}else{
-
mToast.setText(msg);
-
mToast.setDuration(Toast.LENGTH_SHORT);
-
}
-
mToast.show();
-
}
-
-
-
-
-
-
-
public static Bitmap getBitmapFromView(View view) {
-
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
-
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
-
view.buildDrawingCache();
-
Bitmap bitmap = view.getDrawingCache();
-
return bitmap;
-
}
-
-
-
-
-
-
-
-
-
-
-
public class MKGeneralListenerImpl implements MKGeneralListener{
-
-
-
-
-
@Override
-
public void onGetNetworkState(int iError) {
-
if (iError == MKEvent.ERROR_NETWORK_CONNECT) {
-
showToast("您的网络出错啦!
");
-
}
-
}
-
-
-
-
-
@Override
-
public void onGetPermissionState(int iError) {
-
if (iError == MKEvent.ERROR_PERMISSION_DENIED) {
-
showToast("API KEY错误, 请检查。");
-
}
-
}
-
-
}
-
-
@Override
-
protected void onResume() {
-
-
mMapView.onResume();
-
super.onResume();
-
}
-
-
-
-
@Override
-
protected void onPause() {
-
-
mMapView.onPause();
-
super.onPause();
-
}
-
-
@Override
-
protected void onDestroy() {
-
-
mMapView.destroy();
-
-
-
if(mBMapManager != null){
-
mBMapManager.destroy();
-
mBMapManager = null;
-
}
-
-
-
if (mLocClient != null){
-
mLocClient.stop();
-
}
-
-
super.onDestroy();
-
}
-
-
-
-
}