Android 百度地图 定位

来源:互联网 发布:mac动态桌面壁纸软件 编辑:程序博客网 时间:2024/04/27 19:51

最近搞的项目中需要用到地图,因为以前是用的google地图,由于一些原因现在改成百度地图

首先,从百度地图下载Android平台下面的API开发包,示例代码和http://dev.baidu.com/wiki/imap/index.php

  解压示例代码,导入eclipse运行,如下图:

   

  但是没有我们所需要的代码,所以还是要自己动手来实现一个DEMO,创建一个Android工程,在manifest加入百度地图所需权限:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">
 <com.baidu.mapapi.MapView
         android:id="@+id/bmapsView"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:clickable="true" />
 
 </LinearLayout>
 
MainActivity中的代码

package com.android.view;

import android.location.Location;
import android.os.Bundle;
import android.view.Window;
import android.widget.Toast;

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

public class MainActivity extends MapActivity {

 private MapView mapView = null;// 显示地图控件
 private MapController mapController = null;// 地图控制
 private String keyStr = "9DACC215A91274A603B11345959C51D44DA34009";// key
 private BMapManager bMapManager = null;// 百度地图引擎
 private LocationListener mLocationListener = null;// onResume时注册此listener,onPause时需要Remove
 private MyLocationOverlay mLocationOverlay = null; // 定位图层

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  setContentView(R.layout.main);
  initWidget();
 }

 private void initWidget() {
  mapView = (MapView) findViewById(R.id.b_mapView);
  bMapManager = new BMapManager(MainActivity.this);
  bMapManager.init(keyStr, new MKGeneralListener() {

   @Override
   public void onGetPermissionState(int arg0) {
    if (300 == arg0) {
     Toast.makeText(MainActivity.this, R.string.tishi,
       Toast.LENGTH_SHORT).show();
    }
   }

   @Override
   public void onGetNetworkState(int arg0) {

   }
  });

  initMapActivity(bMapManager);
  mapController = mapView.getController();
  // 设置在缩放动画过程中也显示overlay,默认为不绘制
  mapView.setDrawOverlayWhenZooming(true);
  // mapView.setBuiltInZoomControls(true);// 设置启动内部的缩放控件
  mapController.setZoom(12);
  // 添加我的位置定位图层
  mLocationOverlay = new MyLocationOverlay(this, mapView);
  mapView.getOverlays().add(mLocationOverlay);
  mLocationListener = new LocationListener() {

   @Override
   public void onLocationChanged(Location location) {
    if (null != location) {
     GeoPoint pt = new GeoPoint(
       (int) (location.getLatitude() * 1E6),
       (int) (location.getLongitude() * 1E6));
     mapController.animateTo(pt);
     mapController.setZoom(15);
    }

   }
  };

 }

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

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

 @Override
 protected void onPause() {
  if (null != bMapManager) {
   bMapManager.getLocationManager().requestLocationUpdates(
     mLocationListener);
   mLocationOverlay.disableMyLocation();
   mLocationOverlay.disableCompass(); // 关闭指南针

   bMapManager.stop();
  }
  super.onPause();
 }

 @Override
 protected void onResume() {
  super.onResume();
  if (null != bMapManager) {
   // 注册定位事件,定位后将地图移动到定位点
   bMapManager.getLocationManager().requestLocationUpdates(
     mLocationListener);
   mLocationOverlay.enableMyLocation();
   mLocationOverlay.enableCompass(); // 打开指南针

   bMapManager.start();

  }
 }

}