转载

百度地图定位

上中心代码

package baidumapsdk.demo; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; import com.baidu.location.BDLocation; import com.baidu.location.BDLocationListener; import com.baidu.location.LocationClient; import com.baidu.location.LocationClientOption; import com.baidu.mapapi.map.BaiduMap; import com.baidu.mapapi.map.BitmapDescriptor; import com.baidu.mapapi.map.BitmapDescriptorFactory; import com.baidu.mapapi.map.MapStatusUpdate; import com.baidu.mapapi.map.MapStatusUpdateFactory; import com.baidu.mapapi.map.MapView; import com.baidu.mapapi.map.MyLocationConfiguration; import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode; import com.baidu.mapapi.map.MyLocationData; import com.baidu.mapapi.model.LatLng; /**  * 此demo用来展示如何结合定位SDK实现定位,并使用MyLocationOverlay绘制定位位置 同时展示如何使用自定义图标绘制并点击时弹出泡泡  *   */ public class LocationDemo extends Activity {  // 定位相关  LocationClient mLocClient;  public MyLocationListenner myListener = new MyLocationListenner();  private LocationMode mCurrentMode;  BitmapDescriptor mCurrentMarker;  MapView mMapView;  BaiduMap mBaiduMap;  // UI相关  OnCheckedChangeListener radioButtonListener;  Button requestLocButton;  boolean isFirstLoc = true;// 是否首次定位   @Override  public void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentView(R.layout.activity_location);   requestLocButton = (Button) findViewById(R.id.button1);   mCurrentMode = LocationMode.NORMAL;   requestLocButton.setText("普通");   OnClickListener btnClickListener = new OnClickListener() {    public void onClick(View v) {     switch (mCurrentMode) {     case NORMAL:      requestLocButton.setText("跟随");      mCurrentMode = LocationMode.FOLLOWING;      mBaiduMap        .setMyLocationConfigeration(new MyLocationConfiguration(          mCurrentMode, true, mCurrentMarker));      break;     case COMPASS:      requestLocButton.setText("普通");      mCurrentMode = LocationMode.NORMAL;      mBaiduMap        .setMyLocationConfigeration(new MyLocationConfiguration(          mCurrentMode, true, mCurrentMarker));      break;     case FOLLOWING:      requestLocButton.setText("罗盘");      mCurrentMode = LocationMode.COMPASS;      mBaiduMap        .setMyLocationConfigeration(new MyLocationConfiguration(          mCurrentMode, true, mCurrentMarker));      break;     }    }   };   requestLocButton.setOnClickListener(btnClickListener);   RadioGroup group = (RadioGroup) this.findViewById(R.id.radioGroup);   radioButtonListener = new OnCheckedChangeListener() {    @Override    public void onCheckedChanged(RadioGroup group, int checkedId) {     if (checkedId == R.id.defaulticon) {      // 传入null则,恢复默认图标      mCurrentMarker = null;      mBaiduMap        .setMyLocationConfigeration(new MyLocationConfiguration(          mCurrentMode, true, null));     }     if (checkedId == R.id.customicon) {      // 修改为自定义marker      mCurrentMarker = BitmapDescriptorFactory        .fromResource(R.drawable.icon_geo);      mBaiduMap        .setMyLocationConfigeration(new MyLocationConfiguration(          mCurrentMode, true, mCurrentMarker));     }    }   };   group.setOnCheckedChangeListener(radioButtonListener);   // 地图初始化   mMapView = (MapView) findViewById(R.id.bmapView);   mBaiduMap = mMapView.getMap();   // 开启定位图层   mBaiduMap.setMyLocationEnabled(true);   // 定位初始化   mLocClient = new LocationClient(this);   mLocClient.registerLocationListener(myListener);   LocationClientOption option = new LocationClientOption();   option.setOpenGps(true);// 打开gps   option.setCoorType("bd09ll"); // 设置坐标类型   option.setScanSpan(1000);   mLocClient.setLocOption(option);   mLocClient.start();  }  /**   * 定位SDK监听函数   */  public class MyLocationListenner implements BDLocationListener {   @Override   public void onReceiveLocation(BDLocation location) {    // map view 销毁后不在处理新接收的位置    if (location == null || mMapView == null)     return;    MyLocationData locData = new MyLocationData.Builder()      .accuracy(location.getRadius())      // 此处设置开发者获取到的方向信息,顺时针0-360      .direction(100).latitude(location.getLatitude())      .longitude(location.getLongitude()).build();    mBaiduMap.setMyLocationData(locData);    if (isFirstLoc) {     isFirstLoc = false;     LatLng ll = new LatLng(location.getLatitude(),       location.getLongitude());     MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);     mBaiduMap.animateMapStatus(u);    }   }   public void onReceivePoi(BDLocation poiLocation) {   }  }  @Override  protected void onPause() {   mMapView.onPause();   super.onPause();  }  @Override  protected void onResume() {   mMapView.onResume();   super.onResume();  }  @Override  protected void onDestroy() {   // 退出时销毁定位   mLocClient.stop();   // 关闭定位图层   mBaiduMap.setMyLocationEnabled(false);   mMapView.onDestroy();   mMapView = null;   super.onDestroy();  } } 

1、声明一个LocationClient 定位服务的客户端。宿主程序在客户端声明此类,并调用(客户端通过这个控制定位)

// 定位相关

LocationClient mLocClient ;

在onCreate方法里

@Overridepublic void onCreate(Bundle savedInstanceState) {

// 定位初始化

mLocClient= new LocationClient(this);

mLocClient.registerLocationListener(myListener);

方法解析

registerLocationListener  public void registerLocationListener(BDLocationListener listener) 注册定位监听函数 参数: BDLocation类 -
LocationClientOption option = new LocationClientOption();         option.setOpenGps(true);// 打开gps         option.setCoorType("bd09ll"); // 设置坐标类型         option.setScanSpan(1000);         mLocClient.setLocOption(option);         mLocClient.start();

上面就是设置gps坐标类型添加到 mLocClient 里面

在listener里编写定位信息

public class MyLocationListenner implements BDLocationListener {  @Override  public void onReceiveLocation(BDLocation location) {   // map view 销毁后不在处理新接收的位置   if (location == null || mMapView == null)    return;   MyLocationData locData = new MyLocationData.Builder()     .accuracy(location.getRadius())     // 此处设置开发者获取到的方向信息,顺时针0-360     .direction(100).latitude(location.getLatitude())     .longitude(location.getLongitude()).build();   mBaiduMap.setMyLocationData(locData);   if (isFirstLoc) {    isFirstLoc = false;    LatLng ll = new LatLng(location.getLatitude(),      location.getLongitude());    MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);    mBaiduMap.animateMapStatus(u);   }  }  public void onReceivePoi(BDLocation poiLocation) {  } } 

然后需要添加一句

// 开启定位图层mBaiduMap.setMyLocationEnabled(true);

这句很重要,时间紧迫,有点粗略,以后修改讲解。

正文到此结束
Loading...