(需要导入百度地图的API)
package com.kj.view;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.map.MKEvent;
import com.baidu.mapapi.map.MKMapViewListener;
import com.baidu.mapapi.map.MapController;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapView;
import com.baidu.platform.comapi.basestruct.GeoPoint;
import com.kj.app.R;
import android.app.Activity;
import android.graphics.Bitmap;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class MapActivity extends Activity {
private LocationManager manager;// 定位管理器
private Toast mToast;
private BMapManager mBMapManager;// 地图的管理器,必须在setContentView之前就完成初始化
private MapView mMapView = null;// 地图控件
private MapController mMapController = null;// 地图控制器
private double latitude;// 经度
private double longitude;// 纬度
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
manager = (LocationManager) getSystemService(LOCATION_SERVICE);// 获取手机位置信息
// 获取的条件
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);// 获取精准位置
criteria.setCostAllowed(true);// 允许产生开销
criteria.setPowerRequirement(Criteria.POWER_HIGH);// 消耗大的话,获取的频率高
criteria.setSpeedRequired(true);// 手机位置移动
criteria.setAltitudeRequired(true);// 海拔
// 获取最佳provider: 手机或者模拟器上均为gps
String bestProvider = manager.getBestProvider(criteria, true);// 使用GPS卫星
manager.requestLocationUpdates(bestProvider, 60000, 1,
new MyLocationListener());
mBMapManager = new BMapManager(this);// 地图的管理器,必须在setContentView之前就完成初始化
//"NTp0vf3TVXsjAzmrBg4N9wHT"这是密钥,具体怎么获取可参照http://lbsyun.baidu.com/apiconsole/key?application=key&qq-pf-to=pcqq.c2c
mBMapManager.init("NTp0vf3TVXsjAzmrBg4N9wHT", new MKGeneralListener() {
public void onGetPermissionState(int is) {
if (is == MKEvent.ERROR_PERMISSION_DENIED)
Log.e("wo", "API Key错误");
}
@Override
public void onGetNetworkState(int is) {
if (is == MKEvent.ERROR_NETWORK_CONNECT)
Log.e("wo", "网络错误");
}
});
// 加载布局
setContentView(R.layout.map_layout);
mMapView = (MapView) findViewById(R.id.bmapView);
mMapController = mMapView.getController();// 得到mMapView的控制权,可以用它控制和驱动平移和缩放
mMapController.enableClick(true);// 设置可点击
mMapController.setZoom(12);// 设置地图的zoom级别
mMapView.setBuiltInZoomControls(true);// 设置启用内置的缩放控件
// 用给定的经纬度构造一个GeoPoint,单位是微度 (度 * 1E6)-->北京东城区
GeoPoint point = new GeoPoint((int)(latitude* 1E6), (int)(longitude* 1E6));
mMapController.setCenter(point);// 设置地图中心点
mMapView.regMapViewListener(mBMapManager, new MKMapViewListener() {// 地图的状态改变监听
@Override
public void onMapMoveFinish() {
showToast("题图移动完毕");
}
@Override
public void onMapLoadFinish() {
showToast("地图载入完毕");
}
@Override
public void onMapAnimationFinish() {
}
@Override
public void onGetCurrentMap(Bitmap arg0) {
}
@Override
public void onClickMapPoi(MapPoi arg0) {
if (arg0 != null) {
showToast(arg0.strText);// 点击显示地址
}
}
});
}
public class MyLocationListener implements LocationListener {
/**
* 手机位置发生变动
*/
public void onLocationChanged(Location location) {
location.getAccuracy();// 精确度
latitude = (double) location.getLatitude();// 经度
longitude = (double) location.getLongitude();// 纬度
Log.e("m_tag", "^^^"+latitude);
Log.e("m_tag", "****"+longitude);
}
/**
* 当某个位置提供者的状态发生改变时
*/
public void onStatusChanged(String provider, int status, Bundle extras) {
}
/**
* 某个设备打开时
*/
public void onProviderEnabled(String provider) {
}
/**
* 某个设备关闭时
*/
public void onProviderDisabled(String provider) {
}
}
@Override
protected void onDestroy() {
// MapView的生命周期与Activity同步,当activity销毁时需调用MapView.destroy()
mMapView.destroy();
// 退出应用调用BMapManager的destroy()方法
if (mBMapManager != null) {
mBMapManager.destroy();
mBMapManager = null;
}
super.onDestroy();
}
@Override
protected void onPause() {
// MapView的生命周期与Activity同步,当activity挂起时需调用MapView.onPause()
mMapView.onPause();
super.onPause();
}
@Override
protected void onResume() {
// MapView的生命周期与Activity同步,当activity挂起时需调用MapView.onResume()
mMapView.onResume();
super.onResume();
}
public void showToast(String msg) {
if (mToast == null) {
mToast = Toast.makeText(this, msg, 500);
} else {
mToast.setText(msg);
mToast.setDuration(500);
}
mToast.show();
}
}
//对应的布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.baidu.mapapi.map.MapView
android:id="@+id/bmapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
</RelativeLayout>