APP类,直接复制百度的。
package com.baidumap; import android.app.Application; import android.widget.Toast; import com.baidu.mapapi.*; public class MapAPP extends Application { static MapAPP mDemoApp; // 百度MapAPI的管理类 BMapManager mBMapMan = null; // 授权Key // TODO: 请输入您的Key, // 申请地址:http://dev.baidu.com/wiki/static/imap/key/ String mStrKey = "05FC549E3839B28F2166EBEC6AFC8FBE696D0611"; boolean m_bKeyRight = true; // 授权Key正确,验证通过 // 常用事件监听,用来处理通常的网络错误,授权验证错误等 static class MyGeneralListener implements MKGeneralListener { @Override public void onGetNetworkState(int iError) { Toast.makeText(MapAPP.mDemoApp.getApplicationContext(), "您的网络出错啦!", Toast.LENGTH_LONG).show(); } @Override public void onGetPermissionState(int iError) { if (iError == MKEvent.ERROR_PERMISSION_DENIED) { // 授权Key错误: Toast.makeText(MapAPP.mDemoApp.getApplicationContext(), "请在BMapApiDemoApp.java文件输入正确的授权Key!", Toast.LENGTH_LONG).show(); MapAPP.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(); } }
Map类
package com.baidumap; import android.graphics.drawable.Drawable; import android.location.Location; import android.os.Bundle; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.Toast; import com.baidu.mapapi.BMapManager; import com.baidu.mapapi.GeoPoint; import com.baidu.mapapi.LocationListener; import com.baidu.mapapi.MKLocationManager; import com.baidu.mapapi.MapActivity; import com.baidu.mapapi.MapView; public class ShowMapT extends MapActivity { MapView mapView; MapAPP app; OverlayT overitem; private int onceMyPoint = 1; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); app = (MapAPP) this.getApplication(); if (app.mBMapMan == null) { app.mBMapMan = new BMapManager(getApplication()); app.mBMapMan.init(app.mStrKey, new MapAPP.MyGeneralListener()); } app.mBMapMan.start(); super.initMapActivity(app.mBMapMan); mapView = (MapView) findViewById(R.id.bmapView); mapView.setBuiltInZoomControls(true); mapView.setDrawOverlayWhenZooming(true); onceStartMyLocation(); showMyLocation(); mapView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { app.mBMapMan.getLocationManager().removeUpdates(locationListener); } return false; } }); } // 定位自己的位置,只定位一次 private LocationListener mLocationListener = new LocationListener() { @Override public void onLocationChanged(Location location) { if (location != null) { Double geolat = location.getLatitude() * 1E6; Double geoLng = location.getLongitude() * 1E6; // Toast.makeText(ShowMap.this, "纬度 :"+geolat+"经度: "+geoLng, // Toast.LENGTH_SHORT ).show(); GeoPoint mypoint1 = new GeoPoint(geolat.intValue(), geoLng.intValue()); app.mBMapMan.getLocationManager().removeUpdates(mLocationListener); onceMyPoint++; } } }; private void onceStartMyLocation() { if (onceMyPoint == 1) { app.mBMapMan.getLocationManager().enableProvider(MKLocationManager.MK_GPS_PROVIDER); app.mBMapMan.getLocationManager().enableProvider(MKLocationManager.MK_NETWORK_PROVIDER); app.mBMapMan.getLocationManager().requestLocationUpdates(mLocationListener); app.mBMapMan.getLocationManager().setNoitifyInternal(5, 2); } } // 一直定位自己的位置 private LocationListener locationListener = new LocationListener() { @Override public void onLocationChanged(Location location) { if (location != null) { Double geolat = location.getLatitude() * 1E6; Double geoLng = location.getLongitude() * 1E6; GeoPoint mypoint = new GeoPoint(geolat.intValue(), geoLng.intValue()); mapView.getController().animateTo(mypoint); if (overitem != null) { mapView.getOverlays().remove(overitem); } Drawable marker = getResources().getDrawable(R.drawable.iconmarka); overitem = new OverlayT(marker, mypoint,ShowMapT.this); mapView.getOverlays().add(overitem); } } }; private void showMyLocation() { app.mBMapMan.getLocationManager().enableProvider(MKLocationManager.MK_GPS_PROVIDER); app.mBMapMan.getLocationManager().enableProvider(MKLocationManager.MK_NETWORK_PROVIDER); app.mBMapMan.getLocationManager().requestLocationUpdates(locationListener); app.mBMapMan.getLocationManager().setNoitifyInternal(5, 2); } @Override protected void onResume() { app.mBMapMan.start(); super.onResume(); } @Override protected void onPause() { app.mBMapMan.getLocationManager().removeUpdates(locationListener); app.mBMapMan.stop(); super.onPause(); } @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; } // 返回键 private long exitTime = 0; @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == event.KEYCODE_BACK) { if ((System.currentTimeMillis() - exitTime) > 2000) { Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show(); exitTime = System.currentTimeMillis(); } else { finish(); System.exit(0); } return true; } return super.onKeyDown(keyCode, event); } }
Overlay
package com.baidumap; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Point; import android.graphics.drawable.Drawable; import android.widget.Toast; import com.baidu.mapapi.GeoPoint; import com.baidu.mapapi.ItemizedOverlay; import com.baidu.mapapi.MapView; import com.baidu.mapapi.OverlayItem; import com.baidu.mapapi.Projection; public class OverlayT extends ItemizedOverlay<OverlayItem> { private List<OverlayItem> mGeoList = new ArrayList<OverlayItem>(); private Drawable marker; private Context mContext; public OverlayT(Drawable arg0) { super(arg0); } public OverlayT(Drawable marker, GeoPoint point, Context context) { super(boundCenterBottom(marker)); this.mContext = context; this.marker = marker; mGeoList.add(new OverlayItem(point, "我", "点击出来")); populate(); } @Override public void draw(Canvas canvas, MapView mapView, boolean arg2) { super.draw(canvas, mapView, arg2); Projection projection = mapView.getProjection(); for (int index = size() - 1; index >= 0; index--) { OverlayItem overLayItem = getItem(index); String title = overLayItem.getTitle(); Point point = projection.toPixels(overLayItem.getPoint(), null); Paint paintText = new Paint(); paintText.setColor(Color.BLUE); paintText.setTextSize(15); canvas.drawText(title, point.x - 30, point.y, paintText); } boundCenterBottom(marker); } @Override public boolean onTap(GeoPoint arg0, MapView arg1) { // TODO Auto-generated method stub return super.onTap(arg0, arg1); } protected boolean onTap(int i) { setFocus(mGeoList.get(i)); GeoPoint pt = mGeoList.get(i).getPoint(); Toast.makeText(this.mContext, mGeoList.get(i).getSnippet(), Toast.LENGTH_SHORT).show(); return super.onTap(i); } @Override protected OverlayItem createItem(int i) { return mGeoList.get(i); } @Override public int size() { return mGeoList.size(); } }
manifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.baidumap" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <uses-permission android:name="android.permission.CALL_PHONE" /> <uses-permission android:name="android.permission.SEND_SMS" /> <application
android:name=".MapAPP"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".ShowMapT"
android:screenOrientation="portrait"
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>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >
</uses-permission>
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" >
</uses-permission>
<uses-permission android:name="android.permission.GET_TASKS" />
</manifest>
若是用了app那个类, 就需要在manifest里面添加。
可以下载百度自己的那个demo,看看,更好理解~
写了两个定位自己。一个是定位一次,且仅仅是为了获取这个坐标点,有时有用(可以不看~)
另一个是定位自己位置,并把该点显示在地图的中间。
有些代码不是我写的~
点击打开链接
下载连接
源码下载地址