2012年5月24日星期四

在百度地图(Baidu Map)顯示我的位置(MyLocation)和指南針(Compass)

只要在百度地图(Baidu Map)的地圖視圖(MapView)添加我的位置覆蓋(MyLocationOverlay), 便可以很容易顯示我的位置和指南針.

示例代碼:

        MKLocationManager mLocationManager = mBMapMan.getLocationManager();
        mLocationManager.enableProvider(MKLocationManager.MK_NETWORK_PROVIDER);
        mLocationManager.enableProvider(MKLocationManager.MK_GPS_PROVIDER);
     
        MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mMapView);
        myLocationOverlay.enableMyLocation();
        myLocationOverlay.enableCompass();
        mMapView.getOverlays().add(myLocationOverlay);

在百度地图(Baidu Map)顯示我的位置和指南針


修改前文"編寫簡單的百度地图(Baidu Map)應用程序"的 BaiduMapActivity.java

package com.BaiduMap;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.MKLocationManager;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapController;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.MyLocationOverlay;

import android.os.Bundle;

public class BaiduMapActivity extends MapActivity {
 
 //使用你自己的密鑰
 //參考準備工作 http://goo.gl/j6HkK
 final static String MY_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
 
 BMapManager mBMapMan;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mBMapMan = new BMapManager(getApplication());
        mBMapMan.init(MY_KEY, null);
        super.initMapActivity(mBMapMan);
         
        MapView mMapView = (MapView) findViewById(R.id.bmapsView);
        mMapView.setBuiltInZoomControls(true);
         
        MapController mMapController = mMapView.getController();
        GeoPoint point = new GeoPoint((int) (39.915 * 1E6),
                (int) (116.404 * 1E6));
        mMapController.setCenter(point);
        mMapController.setZoom(12);
        
        //Add MyLocationOverlay
        MKLocationManager mLocationManager = mBMapMan.getLocationManager();
        mLocationManager.enableProvider(MKLocationManager.MK_NETWORK_PROVIDER);
        mLocationManager.enableProvider(MKLocationManager.MK_GPS_PROVIDER);
        
        MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mMapView);
        myLocationOverlay.enableMyLocation();
        myLocationOverlay.enableCompass(); 
        mMapView.getOverlays().add(myLocationOverlay);
    }

 @Override
 protected boolean isRouteDisplayed() {
  // TODO Auto-generated method stub
  return false;
 }

 @Override
 protected void onDestroy() {
  if (mBMapMan != null) {
         mBMapMan.destroy();
         mBMapMan = null;
     }
  super.onDestroy();
 }

 @Override
 protected void onPause() {
  if (mBMapMan != null) {
         mBMapMan.stop();
     }
  super.onPause();
 }

 @Override
 protected void onResume() {
  if (mBMapMan != null) {
         mBMapMan.start();
     }
  super.onResume();
 }

}



沒有留言:

發佈留言