程序代码如下所示,我是仿照网上的实例编写的,在manifests文件中已经添加了识别码以及百度定位的service,权限也按照百度的说明文档添加好了,在虚拟机中运行效果为无法获取地理位置,错误码62,真机运行则毫无反应,我完全找不到是什么地方出了问题,哪位大大可以帮我看一下吗?
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
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;
/**
* Created by GCloud on 2015/4/14.
*/
public class LocationPageFragment extends Fragment implements View.OnClickListener {
private static final String TAG = "dzt";
private TextView mText;
private LocationClient mLocationClient = null;
private BDLocationListener myListener = new MyLocationListener();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLocationClient = new LocationClient(getActivity().getApplicationContext()); // 声明LocationClient类
mLocationClient.registerLocationListener(myListener); // 注册监听函数
setLocationOption();
mLocationClient.start();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout, null);
mText = (TextView)view.findViewById(R.id.location_response);
Button btn = (Button)view.findViewById(R.id.location_request);
btn.setOnClickListener(this);
return view;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mLocationClient.stop();// 停止定位
}
/**
* 设置相关参数
*/
private void setLocationOption() {
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);
option.setIsNeedAddress(true);// 返回的定位结果包含地址信息
option.setAddrType("all");// 返回的定位结果包含地址信息
option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,默认值gcj02
option.setScanSpan(5000);// 设置发起定位请求的间隔时间为5000ms
mLocationClient.setLocOption(option);
}
public class MyLocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
if (location == null)
return;
StringBuffer sb = new StringBuffer(256);
sb.append("当前时间 : ");
sb.append(location.getTime());
sb.append("\n错误码 : ");
sb.append(location.getLocType());
sb.append("\n纬度 : ");
sb.append(location.getLatitude());
sb.append("\n经度 : ");
sb.append(location.getLongitude());
sb.append("\n半径 : ");
sb.append(location.getRadius());
if (location.getLocType() == BDLocation.TypeGpsLocation) {
sb.append("\n速度 : ");
sb.append(location.getSpeed());
sb.append("\n卫星数 : ");
sb.append(location.getSatelliteNumber());
}
else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
sb.append("\n地址 : ");
sb.append(location.getAddrStr());
}
mText.setText(sb.toString());
Log.d(TAG, "onReceiveLocation " + sb.toString());
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.location_request:
if (mLocationClient != null && mLocationClient.isStarted())
mLocationClient.requestLocation();
else
Log.d(TAG, "locClient is null or not started");
break;
default:
break;
}
}
}