标签:android应用
这一篇文章主要讲解的是百度地图的定位功能,然后还有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文件拷贝到工程的libs目录下
二 . 布局文件,一个百度地图控件,加一个手动点击实现定位的按钮,放在一个相对布局里面,很简单的布局
-
<?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();
-
}
-
-
-
-
}
Android 百度地图开发(二)
标签:android应用
原文地址:http://blog.csdn.net/littleyearyear/article/details/41307781