定位1

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wj993257104/article/details/52063841
package com.bwei.tt;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;

public class MainActivity extends Activity {
	MapView mMapView = null; 
	private BaiduMap mBaiduMap;
	private Context context;
//	 boolean useDefaultIcon = false;
	//定位相关
	private LocationClient mlocationClient;
	private MyLocationListener mLocationListener;
	private boolean isFirstIn=true;
	private double mLatitude;
	private double mLongtude;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		  //在使用SDK各组件之前初始化context信息,传入ApplicationContext  
        //注意该方法要再setContentView方法之前实现  
        SDKInitializer.initialize(getApplicationContext());  
		setContentView(R.layout.activity_main);
		//获取地图控件引用  
        mMapView = (MapView) findViewById(R.id.bmapView);  
        mBaiduMap = mMapView.getMap();
        this.context=this;

        //初始化定位
        initLocation();
//     

    }

	private void initLocation() {
		// TODO Auto-generated method stub
		  mlocationClient=new LocationClient(this);
		  mLocationListener=new MyLocationListener();
		  mlocationClient.registerLocationListener(mLocationListener);
		  
		  LocationClientOption option=new LocationClientOption();
		  option.setCoorType("bd09ll");
		  option.setIsNeedAddress(true);
		  option.setOpenGps(true);
		  option.setScanSpan(1000);
		  
		  mlocationClient.setLocOption(option);
	}

	  @Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		//开启定位
		mBaiduMap.setMyLocationEnabled(true);
		if(!mlocationClient.isStarted())
		mlocationClient.start();
	}
	  @Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		//停止定位
		mBaiduMap.setMyLocationEnabled(false);
		mlocationClient.stop();
	}
	@Override  
	    protected void onDestroy() {  
	        super.onDestroy();  
	        //在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理  
	        mMapView.onDestroy();  
	    }  
	    @Override  
	    protected void onResume() {  
	        super.onResume();  
	        //在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理  
	        mMapView.onResume();  
	        }  
	    @Override  
	    protected void onPause() {  
	        super.onPause();  
	        //在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理  
	        mMapView.onPause();  
	        }  
	    @Override
	    public boolean onOptionsItemSelected(MenuItem item) {
	    	// TODO Auto-generated method stub
	    	switch (item.getItemId()) {
			case R.id.id_map_location:
				centerLocation();
				
				break;

			default:
				break;
			}
	    	return super.onOptionsItemSelected(item);
	    }
	    ///定位我的位置
	    private void centerLocation() {
			// TODO Auto-generated method stub
	    	LatLng latlng=new LatLng(mLatitude, mLongtude);
			MapStatusUpdate msu=MapStatusUpdateFactory.newLatLng(latlng);
			mBaiduMap.animateMapStatus(msu);
		}

		private class MyLocationListener implements BDLocationListener{

			@Override
			public void onReceiveLocation(BDLocation location) {
				// TODO Auto-generated method stub
				MyLocationData data=new MyLocationData.Builder()//
				.accuracy(location.getRadius())//
				.latitude(location.getLatitude())//
				.longitude(location.getLongitude())//
				.build();
				mBaiduMap.setMyLocationData(data);
				mLatitude=location.getLatitude();
				mLongtude=location.getLongitude();
				//MyLocationConfiguration config=new MyLocationConfiguration(LocationMode.NORMAL, arg1, arg2);
				if(isFirstIn)
				{
					LatLng latlng=new LatLng(location.getLatitude(), location.getLongitude());
					MapStatusUpdate msu=MapStatusUpdateFactory.newLatLng(latlng);
					mBaiduMap.animateMapStatus(msu);
					isFirstIn=false;
					
					Toast.makeText(context, location.getAddrStr(), Toast.LENGTH_SHORT).show();
				}
				
			}
	    	
	    }
}

阅读更多
换一批

没有更多推荐了,返回首页