标签:
package com.example.maplianx; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import com.baidu.location.BDLocation; import com.baidu.location.BDLocationListener; import com.baidu.location.LocationClient; import com.baidu.location.LocationClientOption; import com.baidu.mapapi.SDKInitializer; 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.MarkerOptions; import com.baidu.mapapi.map.MyLocationData; import com.baidu.mapapi.map.OverlayOptions; import com.baidu.mapapi.model.LatLng; public class MainActivity extends Activity implements OnClickListener { MapView mMapView = null; private Button bt1,bt2,bt3; BaiduMap mBaiduMap; BitmapDescriptor bdB; BitmapDescriptor bdC; //定位 // 定位相关声明 public LocationClient locationClient = null; //自定义图标 BitmapDescriptor mCurrentMarker = null; boolean isFirstLoc = true;// 是否首次定位 public BDLocationListener myListener = new 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.newLatLngZoom(ll, 16); //设置地图中心点以及缩放级别 mBaiduMap.animateMapStatus(u); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SDKInitializer.initialize(getApplicationContext()); setContentView(R.layout.activity_main); //获取地图控件引用 mMapView = (MapView) findViewById(R.id.bmapView); bt1 = (Button) findViewById(R.id.button1); bt2 = (Button) findViewById(R.id.button2); bt3 = (Button) findViewById(R.id.button3); bt1.setOnClickListener(this); bt2.setOnClickListener(this); bt3.setOnClickListener(this); mBaiduMap = mMapView.getMap(); //定义Maker坐标点 LatLng point = new LatLng(39.963175, 116.400244); //构建Marker图标 BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.drawable.icon_marka); //构建MarkerOption,用于在地图上添加Marker OverlayOptions option = new MarkerOptions().position(point).icon(bitmap); //在地图上添加Marker,并显示 mBaiduMap.addOverlay(option); //开启定位图层 mBaiduMap.setMyLocationEnabled(true); locationClient = new LocationClient(getApplicationContext()); // 实例化LocationClient类 locationClient.registerLocationListener(myListener); // 注册监听函数 this.setLocationOption(); //设置定位参数 locationClient.start(); // 开始定位 } @Override protected void onDestroy() { super.onDestroy(); //在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理 locationClient.stop(); mBaiduMap.setMyLocationEnabled(false); mMapView.onDestroy(); mMapView = null; } @Override protected void onResume() { super.onResume(); //在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理 mMapView.onResume(); } @Override protected void onPause() { super.onPause(); //在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理 mMapView.onPause(); } @Override public void onClick(View arg0) { // TODO Auto-generated method stub switch (arg0.getId()) { case R.id.button1://普通 mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL); break; case R.id.button2://卫星 mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE); break; case R.id.button3://地图 mBaiduMap.setTrafficEnabled(true); break; default: break; } } /** * 设置定位参数 */ @SuppressWarnings("deprecation") private void setLocationOption() { LocationClientOption option = new LocationClientOption(); option.setOpenGps(true); // 打开GPS option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);// 设置定位模式 option.setCoorType("bd09ll"); // 返回的定位结果是百度经纬度,默认值gcj02 option.setScanSpan(5000); // 设置发起定位请求的间隔时间为5000ms option.setIsNeedAddress(true); // 返回的定位结果包含地址信息 option.setNeedDeviceDirect(true); // 返回的定位结果包含手机机头的方向 option.setPriority(LocationClientOption.NetWorkFirst);//设置定位优先级 option.setIsNeedAddress(true); locationClient.setLocOption(option); } } |
|
xml
<com.baidu.mapapi.map.MapView android:id="@+id/bmapView" android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="true" android:layout_weight="1" /> |
|
标签:
原文地址:http://www.cnblogs.com/nanze/p/5460487.html