1、下载locSDK4.1.zip
2、protected void onCreate(Bundle savedInstanceState)中加入以下代码
mMyLocationListener = new MyLocationListener();
locationClient = new LocationClient(this);
locationClient.registerLocationListener(mMyLocationListener);
mNotifyLister = new NotifyLister();
mVibrator =(Vibrator)getApplicationContext().getSystemService(Service.VIBRATOR_SERVICE);
3、创建类
public class MyLocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
// TODO Auto-generated method stub
EditText editjd=(EditText)findViewById(R.id.editJd);
EditText editwd=(EditText)findViewById(R.id.editWd);
if (location == null) {
//return;
editjd.setText("1");
editwd.setText("2");
} else
{
// EditText editjd=(EditText)findViewById(R.id.editJd);
// EditText editwd=(EditText)findViewById(R.id.editWd);
editjd.setText(location.getLongitude()+"");
editwd.setText(location.getLatitude()+"");
}
//logMsg(sb.toString());
//Log.i("BaiduLocationApiDem", sb.toString());
}
@Override
public void onReceivePoi(BDLocation arg0) {
}
}
/**
* 高精度地理围栏回调
* @author jpren
*
*/
public class NotifyLister extends BDNotifyListener{
public void onNotify(BDLocation mlocation, float distance){
mVibrator.vibrate(1000);
}
}
4、button事件中加入以下代码
btnCj.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
if (locationClient == null) {
EditText editjd=(EditText)findViewById(R.id.editJd);
EditText editwd=(EditText)findViewById(R.id.editWd);
//return;
editjd.setText("1");
editwd.setText("2");
return;
}
if (locationClient.isStarted()) {
// startButton.setText("Start");
locationClient.stop();
}else {
// startButton.setText("Stop");
locationClient.start();
/*
*当所设的整数值大于等于1000(ms)时,定位SDK内部使用定时定位模式。
*调用requestLocation( )后,每隔设定的时间,定位SDK就会进行一次定位。
*如果定位SDK根据定位依据发现位置没有发生变化,就不会发起网络请求,
*返回上一次定位的结果;如果发现位置改变,就进行网络请求进行定位,得到新的定位结果。
*定时定位时,调用一次requestLocation,会定时监听到定位结果。
*/
locationClient.requestLocation();
}
//地图定位添加图片
// GeoPoint point =new GeoPoint((int)(location.getLatitude()* 1E6),(int)(location.getLongitude()* 1E6));
// LoadMap(point,"采集坐标","开始采集坐标");
// }
// });
}
});
5、真机测试成功