学习一门新的技术,最好参考官方的文档。
这是百度地图官方参考资料:http://lbsyun.baidu.com/index.php?title=androidsdk/guide/hellobaidumap
一、实现普通地图、卫星地图、实时交通
大体实现步骤: 1、将百度地图引入到我们的app中去 2、通过菜单选项实现普通地图、卫星地图、实时交通的开关 源码实现: /baidudemo/res/layout/activity_main.xml<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"/baidudemo/res/menu/main.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<com.baidu.mapapi.map.MapView
android:id="@+id/bmapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
</RelativeLayout>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >/baidudemo/AndroidManifest.xml
<item
android:id="@+id/id_map_common"
android:showAsAction="never"
android:title="普通地图"/>
<item
android:id="@+id/id_map_site"
android:showAsAction="never"
android:title="卫星地图"/>
<item
android:id="@+id/id_map_traffic"
android:showAsAction="never"
android:title="实时交通(off)"/>
</menu>
<?xml version="1.0" encoding="utf-8"?>/baidudemo/src/com/xbmu/baidudemo/MainActivity.java
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xbmu.baidudemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="MGpSDi21hlltOmLk74cuDZmgnSCF9cCt" />
<activity
android:name="com.xbmu.baidudemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package com.xbmu.baidudemo;运行效果:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
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;
public class MainActivity extends Activity {
private MapView mMapView;
private BaiduMap mBaiduMap;
@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);
initView();
}
private void initView() {
mMapView = (MapView) findViewById(R.id.bmapView);
mBaiduMap = mMapView.getMap();
MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(15.0f);
mBaiduMap.setMapStatus(msu);
}
@Override
protected void onPause() {
super.onPause();
// 在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
mMapView.onPause();
}
@Override
protected void onResume() {
super.onResume();
// 在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
mMapView.onResume();
}
@Override
protected void onDestroy() {
super.onDestroy();
// 在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
mMapView.onDestroy();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.id_map_common:
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()){//为true,可以进行实时交通。
//菜单项再点击一次就不能进行实时交通,并修改显示信息
mBaiduMap.setTrafficEnabled(false);
item.setTitle("实时交通(off)");
}else{
mBaiduMap.setTrafficEnabled(true);
item.setTitle("实时交通(on)");
}
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
二、定位功能,定位我的位置
实现所需的类: LocationClient、LocationClientOption进行定位的一些设置、BDLocationListener、BDLocation、MyLocationData。 代码实现: /baidudemo/res/menu/main.xml<menu xmlns:android="http://schemas.android.com/apk/res/android" >/baidudemo/AndroidManifest.xml
<item
android:id="@+id/id_map_common"
android:showAsAction="never"
android:title="普通地图"/>
<item
android:id="@+id/id_map_site"
android:showAsAction="never"
android:title="卫星地图"/>
<item
android:id="@+id/id_map_traffic"
android:showAsAction="never"
android:title="实时交通(off)"/>
<item
android:id="@+id/id_map_location"
android:showAsAction="never"
android:title="我的位置"/>
</menu>
<service/baidudemo/src/com/xbmu/baidudemo/MainActivity.java
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote" >
<intent-filter>
<action android:name="com.baidu.location.service_v2.2" >
</action>
</intent-filter>
</service>
package com.xbmu.baidudemo;运行效果:
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;
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.MyLocationData;
import com.baidu.mapapi.model.LatLng;
public class MainActivity extends Activity {
private MapView mMapView;
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 msu = MapStatusUpdateFactory.zoomTo(15.0f);
mBaiduMap.setMapStatus(msu);
}
@Override
protected void onPause() {
super.onPause();
// 在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
mMapView.onPause();
}
@Override
protected void onStart() {
super.onStart();
// 开启定位
mBaiduMap.setMyLocationEnabled(true);
if (!mLocationClient.isStarted())
mLocationClient.start();
}
@Override
protected void onResume() {
super.onResume();
// 在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
mMapView.onResume();
}
@Override
protected void onStop() {
super.onStop();
// 关闭定位
mBaiduMap.setMyLocationEnabled(false);
mLocationClient.stop();
}
@Override
protected void onDestroy() {
super.onDestroy();
// 在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
mMapView.onDestroy();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.id_map_common:
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()) {// 为true,可以进行实时交通。
// 菜单项再点击一次就不能进行实时交通,并修改显示信息
mBaiduMap.setTrafficEnabled(false);
item.setTitle("实时交通(off)");
} else {
mBaiduMap.setTrafficEnabled(true);
item.setTitle("实时交通(on)");
}
break;
case R.id.id_map_location:
centerToMyLocation();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
/**定位到我的位置*/
private void centerToMyLocation() {
LatLng latLng = new LatLng(mLatitude, mLongtitude);
MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);
mBaiduMap.animateMapStatus(msu);
}
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();
if (isFirstIn) {
LatLng latLng = new LatLng(location.getLatitude(),
location.getLongitude());
MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);
mBaiduMap.animateMapStatus(msu);
isFirstIn = false;
Toast.makeText(context, location.getAddrStr(), 0).show();
}
}
}
}
自定义定位图标
MainActivity.javapackage com.xbmu.baidudemo;
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;
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.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
public class MainActivity extends Activity {
private MapView mMapView;
private BaiduMap mBaiduMap;
private Context context;
// 定位相关
private LocationClient mLocationClient;
private MyLocationListener mLocationListener;
private boolean isFirstIn = true;
private double mLatitude;
private double mLongtitude;
// 自定义定位图标
private BitmapDescriptor mIconLocation;
@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);
// 初始化图标
mIconLocation = BitmapDescriptorFactory
.fromResource(R.drawable.navi_map_gps_locked);
}
private void initView() {
mMapView = (MapView) findViewById(R.id.bmapView);
mBaiduMap = mMapView.getMap();
MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(15.0f);
mBaiduMap.setMapStatus(msu);
}
@Override
protected void onPause() {
super.onPause();
// 在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
mMapView.onPause();
}
@Override
protected void onStart() {
super.onStart();
// 开启定位
mBaiduMap.setMyLocationEnabled(true);
if (!mLocationClient.isStarted())
mLocationClient.start();
}
@Override
protected void onResume() {
super.onResume();
// 在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
mMapView.onResume();
}
@Override
protected void onStop() {
super.onStop();
// 关闭定位
mBaiduMap.setMyLocationEnabled(false);
mLocationClient.stop();
}
@Override
protected void onDestroy() {
super.onDestroy();
// 在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
mMapView.onDestroy();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.id_map_common:
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()) {// 为true,可以进行实时交通。
// 菜单项再点击一次就不能进行实时交通,并修改显示信息
mBaiduMap.setTrafficEnabled(false);
item.setTitle("实时交通(off)");
} else {
mBaiduMap.setTrafficEnabled(true);
item.setTitle("实时交通(on)");
}
break;
case R.id.id_map_location:
centerToMyLocation();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
/** 定位到我的位置 */
private void centerToMyLocation() {
LatLng latLng = new LatLng(mLatitude, mLongtitude);
MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);
mBaiduMap.animateMapStatus(msu);
}
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);
// 设置自定义图标
MyLocationConfiguration config = new MyLocationConfiguration(
LocationMode.NORMAL, true, mIconLocation);
mBaiduMap.setMyLocationConfigeration(config);
// 更细经纬度
mLatitude = location.getLatitude();
mLongtitude = location.getLongitude();
if (isFirstIn) {
LatLng latLng = new LatLng(location.getLatitude(),
location.getLongitude());
MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);
mBaiduMap.animateMapStatus(msu);
isFirstIn = false;
Toast.makeText(context, location.getAddrStr(), 0).show();
}
}
}
}
引入方向传感器:
SensorManager、SenSor、BDLoctionListener对方向进行设置/baidudemo/src/com/xbmu/baidudemo/MyOrientationListener.java
package com.xbmu.baidudemo;MainActivity.java
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
/**
* 自定义我的方向传感器
*/
public class MyOrientationListener implements SensorEventListener {
private SensorManager mSensorManager;
private Context mContext;
private Sensor mSensor;
private float lastX;
public MyOrientationListener(Context context) {
super();
this.mContext = context;
}
@SuppressWarnings("deprecation")
public void start() {
mSensorManager = (SensorManager) mContext
.getSystemService(Context.SENSOR_SERVICE);
if (mSensorManager != null) {
//获得方向传感器
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
}
if(mSensor!=null){
//注册监听器
mSensorManager.registerListener(this, mSensor,
SensorManager.SENSOR_DELAY_UI);
}
}
public void stop() {
mSensorManager.unregisterListener(this);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
if(event.sensor.getType() == Sensor.TYPE_ORIENTATION){
float x = event.values[SensorManager.DATA_X];
if(Math.abs(x-lastX) > 1.0){
if(mOnOrientationListener!=null){
mOnOrientationListener.onOrientationChanged(x);
}
}
lastX = x;
}
}
private OnOrientationListener mOnOrientationListener;
public void setOnOrientationListener(
OnOrientationListener mOnOrientationListener) {
this.mOnOrientationListener = mOnOrientationListener;
}
public interface OnOrientationListener{
void onOrientationChanged(float x);
}
}
package com.xbmu.baidudemo;运行效果:
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;
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.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.xbmu.baidudemo.MyOrientationListener.OnOrientationListener;
public class MainActivity extends Activity {
private MapView mMapView;
private BaiduMap mBaiduMap;
private Context context;
// 定位相关
private LocationClient mLocationClient;
private MyLocationListener mLocationListener;
private boolean isFirstIn = true;
private double mLatitude;
private double mLongtitude;
// 自定义定位图标
private BitmapDescriptor mIconLocation;
private MyOrientationListener myOrientationListener;
private float mCurrentX;
@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);
// 初始化图标
mIconLocation = BitmapDescriptorFactory
.fromResource(R.drawable.navi_map_gps_locked);
myOrientationListener = new MyOrientationListener(context);
myOrientationListener.setOnOrientationListener(new OnOrientationListener() {
@Override
public void onOrientationChanged(float x) {
mCurrentX = x;
}
});
}
private void initView() {
mMapView = (MapView) findViewById(R.id.bmapView);
mBaiduMap = mMapView.getMap();
MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(15.0f);
mBaiduMap.setMapStatus(msu);
}
@Override
protected void onPause() {
super.onPause();
// 在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
mMapView.onPause();
}
@Override
protected void onStart() {
super.onStart();
// 开启定位
mBaiduMap.setMyLocationEnabled(true);
if (!mLocationClient.isStarted())
mLocationClient.start();
//开启方向传感器
myOrientationListener.start();
}
@Override
protected void onResume() {
super.onResume();
// 在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
mMapView.onResume();
}
@Override
protected void onStop() {
super.onStop();
// 关闭定位
mBaiduMap.setMyLocationEnabled(false);
mLocationClient.stop();
//停止方向传感器
myOrientationListener.stop();
}
@Override
protected void onDestroy() {
super.onDestroy();
// 在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
mMapView.onDestroy();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.id_map_common:
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()) {// 为true,可以进行实时交通。
// 菜单项再点击一次就不能进行实时交通,并修改显示信息
mBaiduMap.setTrafficEnabled(false);
item.setTitle("实时交通(off)");
} else {
mBaiduMap.setTrafficEnabled(true);
item.setTitle("实时交通(on)");
}
break;
case R.id.id_map_location:
centerToMyLocation();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
/** 定位到我的位置 */
private void centerToMyLocation() {
LatLng latLng = new LatLng(mLatitude, mLongtitude);
MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);
mBaiduMap.animateMapStatus(msu);
}
private class MyLocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
// 构造定位数据
MyLocationData data = new MyLocationData.Builder()//
.direction(mCurrentX)//
.accuracy(location.getRadius())//
.latitude(location.getLatitude())//
.longitude(location.getLongitude()).build();
mBaiduMap.setMyLocationData(data);
// 设置自定义图标
MyLocationConfiguration config = new MyLocationConfiguration(
LocationMode.NORMAL, true, mIconLocation);
mBaiduMap.setMyLocationConfigeration(config);
// 更细经纬度
mLatitude = location.getLatitude();
mLongtitude = location.getLongitude();
if (isFirstIn) {
LatLng latLng = new LatLng(location.getLatitude(),
location.getLongitude());
MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);
mBaiduMap.animateMapStatus(msu);
isFirstIn = false;
Toast.makeText(context, location.getAddrStr(), 0).show();
}
}
}
}
模式切换:
普通模式、跟随模式、罗盘模式。大概方法:MyLocationConfiguration config = new MyLocationConfiguration(mLocationMode,true,mIconLocation);mBaiduMap.setMyLocationConfigeration(config); 代码实现: main.xml<menu xmlns:android="http://schemas.android.com/apk/res/android" >MainActivity.java
<item
android:id="@+id/id_map_common"
android:showAsAction="never"
android:title="普通地图"/>
<item
android:id="@+id/id_map_site"
android:showAsAction="never"
android:title="卫星地图"/>
<item
android:id="@+id/id_map_traffic"
android:showAsAction="never"
android:title="实时交通(off)"/>
<item
android:id="@+id/id_map_location"
android:showAsAction="never"
android:title="我的位置"/>
<item
android:id="@+id/id_map_mode_common"
android:showAsAction="never"
android:title="普通模式"/>
<item
android:id="@+id/id_map_mode_following"
android:showAsAction="never"
android:title="跟随模式"/>
<item
android:id="@+id/id_map_mode_compass"
android:showAsAction="never"
android:title="罗盘模式"/>
</menu>
package com.xbmu.baidudemo;运行效果:
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;
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.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.xbmu.baidudemo.MyOrientationListener.OnOrientationListener;
public class MainActivity extends Activity {
private MapView mMapView;
private BaiduMap mBaiduMap;
private Context context;
// 定位相关
private LocationClient mLocationClient;
private MyLocationListener mLocationListener;
private boolean isFirstIn = true;
private double mLatitude;
private double mLongtitude;
// 自定义定位图标
private BitmapDescriptor mIconLocation;
private MyOrientationListener myOrientationListener;
private float mCurrentX;
private LocationMode mLocationMode;
@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() {
mLocationMode = LocationMode.NORMAL;
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);
// 初始化图标
mIconLocation = BitmapDescriptorFactory
.fromResource(R.drawable.navi_map_gps_locked);
myOrientationListener = new MyOrientationListener(context);
myOrientationListener.setOnOrientationListener(new OnOrientationListener() {
@Override
public void onOrientationChanged(float x) {
mCurrentX = x;
}
});
}
private void initView() {
mMapView = (MapView) findViewById(R.id.bmapView);
mBaiduMap = mMapView.getMap();
MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(15.0f);
mBaiduMap.setMapStatus(msu);
}
@Override
protected void onPause() {
super.onPause();
// 在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
mMapView.onPause();
}
@Override
protected void onStart() {
super.onStart();
// 开启定位
mBaiduMap.setMyLocationEnabled(true);
if (!mLocationClient.isStarted())
mLocationClient.start();
//开启方向传感器
myOrientationListener.start();
}
@Override
protected void onResume() {
super.onResume();
// 在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
mMapView.onResume();
}
@Override
protected void onStop() {
super.onStop();
// 关闭定位
mBaiduMap.setMyLocationEnabled(false);
mLocationClient.stop();
//停止方向传感器
myOrientationListener.stop();
}
@Override
protected void onDestroy() {
super.onDestroy();
// 在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
mMapView.onDestroy();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.id_map_common:
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()) {// 为true,可以进行实时交通。
// 菜单项再点击一次就不能进行实时交通,并修改显示信息
mBaiduMap.setTrafficEnabled(false);
item.setTitle("实时交通(off)");
} else {
mBaiduMap.setTrafficEnabled(true);
item.setTitle("实时交通(on)");
}
break;
case R.id.id_map_location:
centerToMyLocation();
break;
case R.id.id_map_mode_common:
mLocationMode = LocationMode.NORMAL;
break;
case R.id.id_map_mode_following:
mLocationMode = LocationMode.FOLLOWING;
break;
case R.id.id_map_mode_compass:
mLocationMode = LocationMode.COMPASS;
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
/** 定位到我的位置 */
private void centerToMyLocation() {
LatLng latLng = new LatLng(mLatitude, mLongtitude);
MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);
mBaiduMap.animateMapStatus(msu);
}
private class MyLocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
// 构造定位数据
MyLocationData data = new MyLocationData.Builder()//
.direction(mCurrentX)//
.accuracy(location.getRadius())//
.latitude(location.getLatitude())//
.longitude(location.getLongitude()).build();
mBaiduMap.setMyLocationData(data);
// 设置自定义图标
MyLocationConfiguration config = new MyLocationConfiguration(
mLocationMode, true, mIconLocation);
mBaiduMap.setMyLocationConfigeration(config);
// 更细经纬度
mLatitude = location.getLatitude();
mLongtitude = location.getLongitude();
if (isFirstIn) {
LatLng latLng = new LatLng(location.getLatitude(),
location.getLongitude());
MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);
mBaiduMap.animateMapStatus(msu);
isFirstIn = false;
Toast.makeText(context, location.getAddrStr(), 0).show();
}
}
}
}