百度地图 定位加覆盖物(笔记,方便查阅)

[java]view plaincopy

packagecom.mymapmarker;

importandroid.net.Uri;

importandroid.os.Bundle;

importandroid.support.v7.app.AppCompatActivity;

importandroid.util.Log;

importcom.baidu.location.BDLocation;

importcom.baidu.location.BDLocationListener;

importcom.baidu.location.LocationClient;

importcom.baidu.location.LocationClientOption;

importcom.baidu.mapapi.SDKInitializer;

importcom.baidu.mapapi.map.BaiduMap;

importcom.baidu.mapapi.map.BitmapDescriptorFactory;

importcom.baidu.mapapi.map.MapStatus;

importcom.baidu.mapapi.map.MapStatusUpdateFactory;

importcom.baidu.mapapi.map.MapView;

importcom.baidu.mapapi.map.MarkerOptions;

importcom.baidu.mapapi.map.MyLocationConfiguration;

importcom.baidu.mapapi.map.MyLocationData;

importcom.baidu.mapapi.model.LatLng;

importcom.google.android.gms.appindexing.Action;

importcom.google.android.gms.appindexing.AppIndex;

importcom.google.android.gms.appindexing.Thing;

importcom.google.android.gms.common.api.GoogleApiClient;

publicclassMainActivityextendsAppCompatActivity {

MapView mMapView =null;

BaiduMap mBaiduMap;

booleanisFirstLoc =true;// 是否首次定位

MapStatus ms;

//    private ClusterManager mClusterManager;

//    private LocationMode mCurrentMode;

MyLocationConfiguration.LocationMode mCurrentMode;

publicLocationClient mLocationClient =null;

publicBDLocationListener myListener =newMyLocationListener();

/**

* ATTENTION: This was auto-generated to implement the App Indexing API.

* See https://g.co/AppIndexing/AndroidStudio for more information.

*/

privateGoogleApiClient client;

@Override

protectedvoidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

SDKInitializer.initialize(getApplicationContext());

setContentView(R.layout.activity_main);

mMapView = (MapView) findViewById(R.id.bmapView);

mBaiduMap = mMapView.getMap();

//        // 开启定位图层

mBaiduMap.setMyLocationEnabled(true);

//        // 定位初始化

mLocationClient =newLocationClient(this);

mLocationClient.registerLocationListener(myListener);

location();

//        LocationClientOption option = new LocationClientOption();

//        option.setOpenGps(true); // 打开gps

//        option.setCoorType("bd09ll"); // 设置坐标类型

//        option.setScanSpan(1000);

//        mLocationClient.setLocOption(option);

//        mLocationClient.start();

// ATTENTION: This was auto-generated to implement the App Indexing API.

// See https://g.co/AppIndexing/AndroidStudio for more information.

client =newGoogleApiClient.Builder(this).addApi(AppIndex.API).build();

}

privatevoidlocation() {

// 设置正常模式

mCurrentMode = MyLocationConfiguration.LocationMode.NORMAL;

// 地图初始化

mMapView = (MapView) findViewById(R.id.bmapView);

// mMapView.setVisibility(View.VISIBLE);

mBaiduMap = mMapView.getMap();

mBaiduMap.setMapStatus(MapStatusUpdateFactory.zoomTo(15));

/*** ? *************************/

// 设置默认图标

// // 开启定位图层

mBaiduMap.setMyLocationEnabled(true);

LocationClientOption option =newLocationClientOption();

option.setOpenGps(true);// 打开gps

option.setCoorType("bd09ll");// 设置坐标类型

option.setScanSpan(0);// 一秒刷新一次

mLocationClient.setLocOption(option);

mLocationClient.start();

}

/**

* ATTENTION: This was auto-generated to implement the App Indexing API.

* See https://g.co/AppIndexing/AndroidStudio for more information.

*/

publicAction getIndexApiAction() {

Thing object =newThing.Builder()

.setName("Main Page")// TODO: Define a title for the content shown.

// TODO: Make sure this auto-generated URL is correct.

.setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))

.build();

returnnewAction.Builder(Action.TYPE_VIEW)

.setObject(object)

.setActionStatus(Action.STATUS_TYPE_COMPLETED)

.build();

}

@Override

publicvoidonStart() {

super.onStart();

// ATTENTION: This was auto-generated to implement the App Indexing API.

// See https://g.co/AppIndexing/AndroidStudio for more information.

client.connect();

AppIndex.AppIndexApi.start(client, getIndexApiAction());

}

@Override

publicvoidonStop() {

super.onStop();

// ATTENTION: This was auto-generated to implement the App Indexing API.

// See https://g.co/AppIndexing/AndroidStudio for more information.

AppIndex.AppIndexApi.end(client, getIndexApiAction());

client.disconnect();

}

publicclassMyLocationListenerimplementsBDLocationListener {

@Override

publicvoidonReceiveLocation(BDLocation location) {

// map view 销毁后不在处理新接收的位置

Log.e("songwinter", location +"");

if(location ==null|| mMapView ==null) {

return;

}

MyLocationData locData =newMyLocationData.Builder()

.accuracy(location.getRadius())

// 此处设置开发者获取到的方向信息,顺时针0-360

.direction(100).latitude(location.getLatitude())

.longitude(location.getLongitude()).build();

mBaiduMap.setMyLocationData(locData);

if(isFirstLoc) {

isFirstLoc =false;

LatLng ll =newLatLng(location.getLatitude(),

location.getLongitude());

LatLng ll2 =newLatLng(location.getLatitude()+0.00035,

location.getLongitude()+0.00205);

LatLng ll3 =newLatLng(location.getLatitude()+0.0005,

location.getLongitude()+0.0005);

MapStatus.Builder builder =newMapStatus.Builder();

builder.target(ll).zoom(18.0f);

mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));

MarkerOptions options=newMarkerOptions();

options.position(ll3).title("swt test").icon(BitmapDescriptorFactory.fromResource(R.drawable.common_plus_signin_btn_text_dark_focused));

options.animateType(MarkerOptions.MarkerAnimateType.drop);

mBaiduMap.addOverlay(newMarkerOptions().position(ll).icon(BitmapDescriptorFactory.fromResource(R.drawable.common_plus_signin_btn_text_dark_focused)).animateType(MarkerOptions.MarkerAnimateType.drop));

mBaiduMap.addOverlay(newMarkerOptions().position(ll2).icon(BitmapDescriptorFactory.fromResource(R.drawable.common_plus_signin_btn_text_dark_focused)).animateType(MarkerOptions.MarkerAnimateType.drop));

mBaiduMap.addOverlay(options);

}

//

//            final Marker marker = mBaiduMap.addMarker(new MarkerOptions().position(latLng).title("北京").snippet("DefaultMarker"));

}

publicvoidonReceivePoi(BDLocation poiLocation) {

}

}

@Override

protectedvoidonPause() {

mMapView.onPause();

super.onPause();

}

@Override

protectedvoidonResume() {

mMapView.onResume();

super.onResume();

}

@Override

protectedvoidonDestroy() {

// 退出时销毁定位

mLocationClient.stop();

// 关闭定位图层

mBaiduMap.setMyLocationEnabled(false);

mMapView.onDestroy();

mMapView =null;

super.onDestroy();

}

}

[java]view plaincopy

[java]view plaincopy

[java]view plaincopy

Marker的点击事件

[java]view plaincopy

[java]view plaincopy

[java]view plaincopy

/**

* 响应点击图标事件

*

* @author Administrator

*

*/

publicclassMyOnMarkerClickListenerimplementsOnMarkerClickListener {

// TODO

@Override

publicbooleanonMarkerClick(Marker marker) {

booleanflag = list.contains(marker);

if(flag) {

intindex = list.indexOf(marker);

Log.e("mark","点击的位置是"+ index);

showPopupWindow(index);

}

returntrue;

}

}

推荐阅读更多精彩内容