添加定位功能
package com.sjy.baidumap;
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.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.model.LatLng;
import com.baidu.mapapi.map.MyLocationData;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.widget.Toast;
public class MainActivity extends Activity {
private MapView mMapView = null;// 一个显示地图的视图(View)
private BaiduMap mBaiduMap;
private Context context;
// 定位相关
private LocationClient mLocationClient;
private MyLocationListener mLocationListener;
private boolean isFirstIn = true;
private double mLatitude;// 纬度
private double mLongtitude;// 经度
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
// 在使用SDK各组件之前初始化context信息,传入ApplicationContext
// 注意该方法要再setContentView方法之前实现
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
this.context = this;
initView();
initLocation();
}
private void initLocation() {
mLocationClient = new LocationClient(this);
mLocationListener = new MyLocationListener();
mLocationClient.registerLocationListener(mLocationListener);
LocationClientOption option = new LocationClientOption();
option.setCoorType("bd09ll");
option.setIsNeedAddress(true);
option.setOpenGps(true);
option.setScanSpan(1000);
mLocationClient.setLocOption(option);
}
private void initView() {
mMapView = (MapView) findViewById(R.id.bmapView);
mBaiduMap = mMapView.getMap();// 获取地图控制器
MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.zoomTo(15.0f);// 设置地图缩放级别
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
// 开始定位
mBaiduMap.setMyLocationEnabled(true);
if (!mLocationClient.isStarted())
mLocationClient.start();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
// 停止定位
mBaiduMap.setMyLocationEnabled(false);
mLocationClient.stop();
}
@Override
protected void onDestroy() {
super.onDestroy();
// 在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
mMapView.onDestroy();
}
@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 boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.id_map_commen:
mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
break;
case R.id.id_map_site:
mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);
break;
case R.id.id_map_traffic:
if (mBaiduMap.isTrafficEnabled()) {
mBaiduMap.setTrafficEnabled(false);
item.setTitle("实时交通(off)");
} else {
mBaiduMap.setTrafficEnabled(true);
item.setTitle("实时交通(on)");
}
break;
case R.id.id_map_location:
centerToMyLocation();
break;
}
return super.onOptionsItemSelected(item);
}
// 定位到我的位置
private void centerToMyLocation() {
LatLng latLng = new LatLng(mLatitude, mLongtitude);// 地理坐标基本数据结构
MapStatusUpdate msu // 描述地图状态将要发生的变化
= MapStatusUpdateFactory.newLatLng(latLng);// 设置地图新中心点
mBaiduMap.animateMapStatus(msu);// 以动画方式更新地图状态,动画耗时 300 ms
}
private class MyLocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
MyLocationData data = new MyLocationData.Builder()// 定位数据
.accuracy(location.getRadius())// 设置定位数据的精度信息,单位:米
.latitude(location.getLatitude())// 设置定位数据的纬度
.longitude(location.getLongitude())// 设置定位数据的经度
.build();// 构建生成定位数据对象
mBaiduMap.setMyLocationData(data);// 设置定位数据, 只有先允许定位图层后设置数据才会生效
mLatitude = location.getLatitude();
mLongtitude = location.getLongitude();
// MyLocationConfiguration config = new
// MyLocationConfiguration(LocationMode.NORMAL, ar, arg2)
if (isFirstIn) {
// LatLng latLng = new LatLng(location.getLatitude(),
// location.getLongitude());
// MapStatusUpdate msu =
// MapStatusUpdateFactory.newLatLng(latLng);
// mBaiduMap.animateMapStatus(msu);
centerToMyLocation();
isFirstIn = false;
Toast.makeText(context, location.getAddrStr(), Toast.LENGTH_SHORT).show();
}
}
}
}