会员登录 - 用户注册 - 设为首页 - 加入收藏 - 网站地图 57自学网———旗下安卓自学网站专注Android系统开发教程!
当前位置:主页 > 第三方集成 > 百度地图集成 > 正文

android平台的百度地图的开发(一)

时间:2016-02-29 16:52 来源:网络整理 作者:安卓自学网 阅读:

2、下载完毕后,我们在申请一个密钥,网址:

3、在AndroidManifest.xml文件中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>

<supports-screens
        android:anyDensity="true"
        android:largeScreens="true"
        android:normalScreens="true"
        android:resizeable="true"
        android:smallScreens="true" />

在你的layout文件中,加入地图显示控件

<com.baidu.mapapi.MapView
        android:id="@+id/bmapView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true" />

将库文件引入工程:baidumapapi.jar 和 libs/armeabi/libBMapApiEngine_v1_3_3.so(这一步很重要)

再编写对应的activity类:注意点--继承的类不是Activity而是MapActivity

代码如下:

public class MainActivity extends MapActivity
{

private MapView mapView;
private BMapManager bMapManager;
private MapController controller;
private String key = "你申请的key";

@Override
public void onCreate(Bundle savedInstanceState)
{
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

mapView = (MapView) this.findViewById(R.id.bmapView);
  bMapManager = new BMapManager(this);

// init bmapmanager
  bMapManager.init(key, new MKGeneralListener()
  {

@Override
   public void onGetPermissionState(int arg0)
   {
    if (arg0 == 300)
    {
     Toast.makeText(MainActivity.this, "key error !", Toast.LENGTH_LONG).show();
    }
   }

@Override
   public void onGetNetworkState(int arg0)
   {
    // TODO Auto-generated method stub

}
  });

//一定要在使用mapcontroller之前初始化
  this.initMapActivity(bMapManager);

// config map
  mapView.setBuiltInZoomControls(true);
  controller = mapView.getController();
  GeoPoint point = new GeoPoint((int) (39.915 * 1E6), (int) (116.404 * 1E6));

Log.i("msg", mapView == null ? "mapview is null" : " mapview is not null ");
  Log.i("msg", controller == null ? "controller is null" : "controller is not null ");
  controller.setCenter(point);
  controller.setZoom(12);

//显示交通状况
  mapView.setTraffic(true);
  //显示卫星地图
  mapView.setSatellite(true);

//地图覆盖物
 

}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
  return true;
}

@Override
protected boolean isRouteDisplayed()
{
  return false;
}

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

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

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

(责任编辑:www.57zxw.com)

顶一下
(0)
0%
踩一下
(0)
0%