调用百度地图实现在地图上定位

   下面我说说在百度地图上实现定位。

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.map.MapController;
import com.baidu.mapapi.map.MapView;
import com.baidu.platform.comapi.basestruct.GeoPoint;


public class MainActivity extends Activity {
	private TextView mTv = null;
	public LocationClient mLocationClient = null;
	public MyLocationListenner myListener = new MyLocationListenner();
	public Button ReLBSButton=null;
	public static String TAG = "msg";
	
	BMapManager mBMapMan = null;
	MapView mMapView = null;


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//地图
		mBMapMan=new BMapManager(getApplication());
    	mBMapMan.init(null); 
		
		setContentView(R.layout.activity_main);
		
		//地图
		mMapView=(MapView)findViewById(R.id.bmapsView);
    	mMapView.setBuiltInZoomControls(true);  //实现放大缩小地图的功能
    	mMapView.setTraffic(true);     //显示交通状况
    	//mMapView.setSatellite(true);   //显示卫星地图
    	MapController mMapController=mMapView.getController();
		// 得到mMapView的控制权,可以用它控制和驱动平移和缩放
		GeoPoint point =new GeoPoint((int)(39.915* 1E6),(int)(116.404* 1E6));
		//用给定的经纬度构造一个GeoPoint,单位是微度 (度 * 1E6)
    	mMapController.setCenter(point);//设置地图中心点
    	mMapController.setZoom(12);//设置地图zoom级别
		
		
		mTv = (TextView)findViewById(R.id.textview);
		ReLBSButton=(Button)findViewById(R.id.ReLBS_button);
		
		mLocationClient = new LocationClient( getApplicationContext() );

		/**——————————————————————————————————————————————————————————————————
		 * 这里的AK和应用签名包名绑定,如果使用在自己的工程中需要替换为自己申请的Key
		 * ——————————————————————————————————————————————————————————————————
		 */
//		mLocationClient.setAK("697f50541f8d4779124896681cb6584d");	 
//		mLocationClient.setAK("z4nqERrqxnhNzT5VOGNVRt80");
		mLocationClient.registerLocationListener( myListener );

		setLocationOption();//设定定位参数
		
		mLocationClient.start();//开始定位
System.out.println(1);
		
		// 重新定位
		ReLBSButton.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (mLocationClient != null && mLocationClient.isStarted()){
					mLocationClient.requestLocation();
System.out.println(3);			}	
				else
					Log.d("msg", "locClient is null or not started");
			}
		});
		
	} 
	
	//设置相关参数
	private void setLocationOption(){
		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(true);
		option.setAddrType("all");//返回的定位结果包含地址信息
		option.setCoorType("bd09ll");//返回的定位结果是百度经纬度,默认值gcj02
		option.setScanSpan(50000);//设置发起定位请求的间隔时间为5000ms
		option.disableCache(true);//禁止启用缓存定位
		option.setPoiNumber(5);    //最多返回POI个数   
		option.setPoiDistance(1000); //poi查询距离        
		option.setPoiExtraInfo(true); //是否需要POI的电话和地址等详细信息        
		mLocationClient.setLocOption(option);
		
	} 

	@Override
	public void onDestroy() {
		mLocationClient.stop();//停止定位
		 mMapView.destroy();
         if(mBMapMan!=null){
                 mBMapMan.destroy();
                 mBMapMan=null;
         }
		mTv = null;
		super.onDestroy();
	}


	/**
	 * 监听函数,有更新位置的时候,格式化成字符串,输出到屏幕中
	 */
	public class MyLocationListenner implements BDLocationListener {
		@Override
		//接收位置信息
		public void onReceiveLocation(BDLocation location) {
			if (location == null)
				return ;
			StringBuffer sb = new StringBuffer(256);
			sb.append("1时间 : ");
			sb.append(location.getTime());
			sb.append("\n1return code : ");
			sb.append(location.getLocType());
			sb.append("\n1latitude : ");
			sb.append(location.getLatitude());
			sb.append("\n1lontitude : ");
			sb.append(location.getLongitude());
			sb.append("\n1radius : ");
			sb.append(location.getRadius());
System.out.println(2);
			if (location.getLocType() == BDLocation.TypeGpsLocation){
				sb.append("\n2speed : ");
				sb.append(location.getSpeed());
				sb.append("\n2satellite : ");
				sb.append(location.getSatelliteNumber());
			} else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
				/**
				 * 格式化显示地址信息
				 */
				sb.append("\n3addr : ");
				sb.append(location.getAddrStr());
			}
			sb.append("\n4sdk version : ");
			sb.append(mLocationClient.getVersion());
			sb.append("\n4isCellChangeFlag : ");
			sb.append(location.isCellChangeFlag());
			mTv.setText(sb.toString());
			Log.i(TAG, sb.toString()); 
	mMapView.getController().setCenter(new GeoPoint((int)(29.52* 1E6), (int)(106.57* 1E6)));
	//mMapView.getController().setZoom(12);
	mMapView.refresh();
		}
		//接收POI信息函数,我不需要POI,所以我没有做处理
		public void onReceivePoi(BDLocation poiLocation) {
			if (poiLocation == null) {
				return;
			}
		}
	}


}

在这段代码里我没有进行实时定位,只是给地图传了一个经纬度在地图上显示而已,东西弄的比较水,看看效果吧


上面是定位的经纬度想,下面是地图。记住百度定位sdk4.0以上要申请密钥才能使用,还有,密钥不是在程序里给定而是在manifest文件中给定 <meta-data
            android:name="com.baidu.lbsapi.API_KEY"
            android:value="HP3YeKaxrdwZzZCDHC5sR6lo" />

下面给出manifest文件吧

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.bdgps"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        
         <meta-data
            android:name="com.baidu.lbsapi.API_KEY"
            android:value="HP3YeKaxrdwZzZCDHC5sR6lo" />
        <activity
            android:name="com.example.bdgps.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
         <service
            android:name="com.baidu.location.f"
            android:enabled="true"
            android:process=":remote" >
        </service>
    </application>
    
    
    
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >
    </uses-permission>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >
    </uses-permission>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >
    </uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_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.WRITE_EXTERNAL_STORAGE" >
    </uses-permission>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" >
    </uses-permission>
    <uses-permission android:name="android.permission.READ_LOGS" >
    </uses-permission>
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />

</manifest>













在.xml文件中记得定义好mapview,下面给出.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

    <TextView 
         android:id="@+id/textview"
        android:layout_width="194dp"
        android:layout_height="wrap_content"
        />
    <Button
        android:id="@+id/ReLBS_button"
        android:layout_width="194dp"
        android:layout_height="wrap_content"
        android:text="Update My Location" />
    
    
     <com.baidu.mapapi.map.MapView android:id="@+id/bmapsView"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:clickable="true" />

        
</LinearLayout>


源码下载:http://download.csdn.net/detail/u011833422/7221291

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值