Android 百度地图定位指针(系列2)

java代码:

package com.example.baidu;

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;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {

	private LocationClient mLocationClient;
	private BaiduMap mBaiduMap;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		SDKInitializer.initialize(getApplicationContext());
		
		setContentView(R.layout.activity_main);

		mLocationClient = new LocationClient(this);
		LocationClientOption option = new LocationClientOption();
		//可选,默认gcj02,设置返回的定位结果坐标系
		option.setCoorType("bd09ll");
		mLocationClient.setLocOption(option);

		MapView mMapView = (MapView) findViewById(R.id.bmpView);

		mBaiduMap = mMapView.getMap();
		mBaiduMap.setMyLocationEnabled(true);

		MyLocationListener mMyLocationListener = new MyLocationListener();
		mLocationClient.registerLocationListener(mMyLocationListener);
		mLocationClient.start();

	}

	private class MyLocationListener implements BDLocationListener {

		@Override
		public void onReceiveLocation(BDLocation location) {
			
			//latitude纬度 longitude经度
			MyLocationData locData = new MyLocationData.Builder().latitude(location.getLatitude())
					.longitude(location.getLongitude()).build();

			mBaiduMap.setMyLocationData(locData);
			LatLng ll = new LatLng(location.getLatitude(), location.getLongitude());
			MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(ll, 16.0f);
			mBaiduMap.animateMapStatus(u);
		}

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		mLocationClient.stop();
	}
}

需要的布局文件:



    
        


AndroidMainfest,xmi:




    

    
        
            
                

                
            
        

        

        
        
    

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    




你可能感兴趣的:(android)