1 package com.aihunqin.test; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.widget.TextView; 6 7 import com.baidu.location.BDLocation; 8 import com.baidu.location.BDLocationListener; 9 import com.baidu.location.LLSInterface; 10 import com.baidu.location.LocationClient; 11 import com.baidu.location.LocationClientOption; 12 import com.baidu.location.LocationClientOption.LocationMode; 13 import com.example.aihunqin.R; 14 15 public class BaiduLocation extends Activity { 16 public LocationClient mLocationClient; 17 public MyLocationListener mMyLocationListener; 18 public TextView mLocationResult; 19 20 @Override 21 protected void onCreate(Bundle savedInstanceState) { 22 23 super.onCreate(savedInstanceState); 24 mLocationClient = new LocationClient(getApplicationContext()); 25 mMyLocationListener = new MyLocationListener(); 26 mLocationClient.registerLocationListener(mMyLocationListener); 27 setContentView(R.layout.baidulocation); 28 mLocationResult = (TextView) findViewById(R.id.mLocationResult); 29 InitLocation(); 30 mLocationClient.start(); 31 } 32 33 /** 34 * 实现定位回调监听 35 * 36 * @author Alex 37 * 38 */ 39 public class MyLocationListener implements BDLocationListener { 40 41 @Override 42 public void onReceiveLocation(BDLocation location) { 43 // Receive Location 44 StringBuffer sb = new StringBuffer(256); 45 sb.append("time:"); 46 sb.append(location.getTime()); 47 sb.append(" error code:"); 48 sb.append(location.getLocType()); 49 sb.append(" latitude:"); 50 sb.append(location.getLatitude()); 51 sb.append(" lontitude:"); 52 sb.append(location.getLongitude()); 53 sb.append(" radius:"); 54 sb.append(location.getRadius()); 55 if (location.getLocType() == BDLocation.TypeNetWorkLocation) { 56 sb.append(" addr:"); 57 sb.append(location.getAddrStr()); 58 // 运营商信息 59 sb.append(" operationers:"); 60 sb.append(location.getOperators()); 61 } 62 logMsg(sb.toString()); 63 } 64 } 65 66 /** 67 * 显示请求你字符串 68 * 69 * @param str 70 */ 71 public void logMsg(String str) { 72 try { 73 if (mLocationResult != null) 74 mLocationResult.setText(str); 75 } catch (Exception e) { 76 e.printStackTrace(); 77 } 78 } 79 80 private void InitLocation() { 81 LocationClientOption option = new LocationClientOption(); 82 option.setLocationMode(LocationMode.Battery_Saving);// 设置定位模式 83 option.setCoorType("bd09ll"); 84 option.setIsNeedAddress(true); 85 mLocationClient.setLocOption(option); 86 } 87 }