先贴代码(拿过去就可以用):
MainActivity:
package com.anjoyo.autolocation;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
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.location.LocationClientOption.LocationMode;
public class MainActivity extends Activity {
Button btn;
TextView item_text;
/**定位相关**/
public LocationClient mClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();//初始化控件
initLocation();//初始化定位
mClient.start();
}
/**控件初始化**/
private void initViews() {
btn = (Button) findViewById(R.id.btn);
// btn.setOnClickListener(mOnClickListener);
item_text = (TextView) findViewById(R.id.item_text);
}
// /**监听btn的点击事件**/
// private OnClickListener mOnClickListener = new OnClickListener() {
//
// @Override
// public void onClick(View v) {
//
// mClient.start();//如果有返回结果,则会调用客户端的监听器 mListener
// }
// };
/**定位相关的东西**/
private void initLocation() {
//初始化客户端
mClient = new LocationClient(getApplicationContext());//声明LocationClient类
//给客户端创建一个"设置"
LocationClientOption option = new LocationClientOption();
/**设置定位参数**/
option.set