android百度地图定位开发
一、activity
import android.app.Activity;
import android.graphics.Point;
import android.graphics.PointF;
import android.os.Bundle;
import android.os.Message;
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.BaiduMapOptions;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.MapStatus;
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.map.Overlay;
import com.baidu.mapapi.model.LatLng;
/**
* 此demo用来展示如何结合定位SDK实现定位,并使用MyLocationOverlay绘制定位位置 同时展示如何使用自定义图标绘制并点击时弹出泡泡
*
*/
public class LocationDemo extends Activity {
// 定位相关
LocationClient mLocClient; //---------------first声明
MapView mMapView;
BaiduMap mBaiduMap;
private LocationMode mCurrentMode;
public MyLocationListenner myListener = new MyLocationListenner();
MyLocationData locData ;
BitmapDescriptor mCurrentMarker;
boolean isFirstLoc = true;// 是否首次定位
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
//requestLocButton.setText("普通");
mCurrentMode = LocationMode.FOLLOWING;
// 1 地图初始化
mMapView = (MapView) findViewById(R.id.bmapView);//经度120.219375,纬度30.259244 杭州
//mMapView = new MapView(this);
//mMapView.refreshDrawableState();
System.out.println("初始化成功啦,哈哈!");
mBaiduMap = mMapView.getMap();
// 2 开启定位图层
mBaiduMap.setMyLocationEnabled(true);
mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(
mCurrentMode, true, mCurrentMarker));
// 3 定位初始化
mLocClient = new LocationClient(this);
mLocClient.registerLocationListener(myListener); //---------------first注册定位监听
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);// 打开gps
option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
option.setCoorType("bd09ll"); // 设置坐标类型
option.setScanSpan(3000);
option.setIsNeedAddress(true);
mLocClient.setLocOption(option); //---------------first设置locationoption
mLocClient.start(); //---------------first启动sdk定位
mLocClient.requestLocation();
}
/**
* 定位SDK监听函数
*/
public class MyLocationListenner implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
// map view 销毁后不在处理新接收的位置
if (location == null || mMapView == null)
return;
locData= new MyLocationData.Builder()
.accuracy(location.getRadius())
// 此处设置开发者获取到的方向信息,顺时针0-360
.direction(location.getDirection()).latitude(location.getLatitude())
.longitude(location.getLongitude()).build();
System.out.println("location.lat"+location.getLatitude()+"&&&&&&&&"+"location.long"+location.getLongitude());
mBaiduMap.setMyLocationEnabled(true); //------1允许定位图层
mBaiduMap.setMyLocationData(locData); //------2允许定位图层后在设置定位数据
LatLng ll = new LatLng(location.getLatitude(),
location.getLongitude());
MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(ll,19);
mBaiduMap.animateMapStatus(u);
if (isFirstLoc) {
isFirstLoc = false;
// 设置地图缩放比例:17级100米
MapStatusUpdate ms = MapStatusUpdateFactory.newLatLngZoom(ll,19);
System.out.println("33333333333333333");
mBaiduMap.setMapStatus(ms);
}
}
}
@Override
protected void onPause() {
mMapView.onPause();
super.onPause();
}
@Override
protected void onResume() {
mMapView.onResume();
super.onResume();
}
@Override
protected void onDestroy() {
mLocClient.unRegisterLocationListener(myListener);
// 退出时销毁定位
mLocClient.stop();
// 关闭定位图层
mBaiduMap.setMyLocationEnabled(false);
mMapView.onDestroy();
mMapView = null;
super.onDestroy();
}
}
二、layout.xml (activity_location.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.baidu.mapapi.map.MapView
android:id="@+id/bmapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="80dip"
android:background="#D000"
android:minWidth="100dip"
android:orientation="vertical"
android:padding="2dp" >
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="定位icon" >
<RadioButton
android:id="@+id/defaulticon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="默认图标" >
</RadioButton>
<RadioButton
android:id="@+id/customicon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自定义图标" >
</RadioButton>
</RadioGroup>
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="25dp"
android:layout_marginTop="10dip" />
</RelativeLayout>
最后在配置文件中别忘了添加这个类。