百度地图开放平台:
http://developer.baidu.com/map/geosdk-android-developv4.0.htm
1. 导入库文件
工程属性->Java Build Path->Libraries中选择“Add JARs”,BaiduMap主包:
public class BaiduMap extends Activity {
public TextView tv1, tv2;
public BMapManager mapManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylocation);
tv1=(TextView)findViewById(R.id.result1);
tv2 = (TextView) this.findViewById(R.id.result2);
mapManager=new BMapManager(this);
mapManager.init("53351EE4BDE7BD870F41A0B4AF1480F1CA97DAF9", new MyMKGeneralListener());
mapManager.getLocationManager().setNoitifyInternal(20, 5);
mapManager.getLocationManager().requestLocationUpdates(new MyLocationListener());
mapManager.start();
// mapManager = new BMapManager(this);
// mapManager.init("EDB67AD764D300895C95ABA02A4DDC58D5485CCD",
// new MyMKGeneralListener());
// // 设置通知间隔:iMaxSecond - 最大通知间隔,单位:秒;iMinSecond - 最小通知间隔,单位:秒
// mapManager.getLocationManager().setNotifyInternal(20, 5);
//
// mapManager.getLocationManager().requestLocationUpdates(
// new MyLocationListener());
// mapManager.start();
}
// 定位自己的位置,只定位一次
class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location arg0) {
double jindu1 = arg0.getLatitude();
double weidu1 = arg0.getLongitude();
int jindu = (int) (arg0.getLatitude()*1000000);
int weidu = (int) (arg0.getLongitude()*1000000);
tv1.setText("经度:" + jindu1 + ",纬度:" + weidu1);
System.out.println("经度:" + jindu1 + ",纬度:" + weidu1);
MKSearch search = new MKSearch();
search.init(mapManager, new MyMKSearchListener());
search.reverseGeocode(new GeoPoint(jindu, weidu));
}
}
class MyMKSearchListener implements MKSearchListener {
@Override
public void onGetAddrResult(MKAddrInfo arg0, int arg1) {
if (arg0 == null) {
tv2.setText("没有获取想要的位置");
} else {
GeoPoint point = arg0.geoPt;
tv2.setText("地址:" + arg0.strAddr + ",坐标:"
+ point.getLatitudeE6() + "," + point.getLongitudeE6());
}
}
@Override
public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1) {
// TODO Auto-generated method stub
}
}
// 常用事件监听,用来处理通常的网络错误,授权验证错误等
class MyMKGeneralListener implements MKGeneralListener {
@Override
public void onGetNetworkState(int arg0) {
}
@Override
public void onGetPermissionState(int arg0) {
}
}
}
BMapApiDemoApp包:
public class BMapApiDemoApp extends Application {
static BMapApiDemoApp mDemoApp;
//百度MapAPI的管理类
BMapManager mBMapMan = null;
// 授权Key
// TODO: 请输入您的Key,
// 申请地址:http://dev.baidu.com/wiki/static/imap/key/
String mStrKey = "53351EE4BDE7BD870F41A0B4AF1480F1CA97DAF9";
boolean m_bKeyRight = true; // 授权Key正确,验证通过
// 常用事件监听,用来处理通常的网络错误,授权验证错误等
static class MyGeneralListener implements MKGeneralListener {
@Override
public void onGetNetworkState(int iError) {
Toast.makeText(BMapApiDemoApp.mDemoApp.getApplicationContext(), "您的网络出错啦!",
Toast.LENGTH_LONG).show();
}
@Override
public void onGetPermissionState(int iError) {
if (iError == MKEvent.ERROR_PERMISSION_DENIED) {
// 授权Key错误:
Toast.makeText(BMapApiDemoApp.mDemoApp.getApplicationContext(),
"请在BMapApiDemoApp.java文件输入正确的授权Key!",
Toast.LENGTH_LONG).show();
BMapApiDemoApp.mDemoApp.m_bKeyRight = false;
}
}
}
@Override
public void onCreate() {
mDemoApp = this;
mBMapMan = new BMapManager(this);
mBMapMan.init(this.mStrKey, new MyGeneralListener());
super.onCreate();
}
@Override
//建议在您app的退出之前调用mapadpi的destroy()函数,避免重复初始化带来的时间消耗
public void onTerminate() {
// TODO Auto-generated method stub
if (mBMapMan != null) {
mBMapMan.destroy();
mBMapMan = null;
}
super.onTerminate();
}
}