Anny_Lin 2015-07-05 11:15 采纳率: 0%
浏览 2523
已结题

android studio 配置百度SDK定位失败

图片说明

就实现基础的定位功能,下面是主activity

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
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.mapapi.SDKInitializer;

public class MapsActivity extends Activity {
private LocationClientOption option;
public LocationClient mLocationClient = null;
private LocationClientOption.LocationMode tempMode = LocationClientOption.LocationMode.Battery_Saving;
private String tempcoor="bd09ll";
private TextView textView1;
private Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.this_one);
    mLocationClient = new LocationClient(getApplicationContext());     //声明LocationClient类
    mLocationClient.registerLocationListener(new  MyLocationListener() );
    textView1= (TextView) findViewById(R.id.textView1);
    button= (Button) findViewById(R.id.startLocation);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            InitLocation();
            mLocationClient.start();
        }
    });

}

private void InitLocation(){
     option = new LocationClientOption();
    option.setLocationMode(tempMode);//设置定位模式
    option.setCoorType(tempcoor);//返回的定位结果是百度经纬度,默认值gcj02
    int span=10000;
    option.setScanSpan(span);

    //设置发起定位请求的间隔时间为5000ms
    option.setIsNeedAddress(true);
    mLocationClient.setLocOption(option);
}
@Override
protected void onStop() {

    mLocationClient.stop();
    super.onStop();
}
public class MyLocationListener implements BDLocationListener {

    @Override
    public void onReceiveLocation(BDLocation location) {
        if (location==null){
            return;
        }
        //Receive Location
        StringBuffer sb = new StringBuffer(256);
        sb.append("time : ");
        sb.append(location.getTime());
        sb.append("\nerror code : ");
        sb.append(location.getLocType());
        sb.append("\nlatitude : ");
        sb.append(location.getLatitude());
        sb.append("\nlontitude : ");
        sb.append(location.getLongitude());
        sb.append("\nradius : ");
        sb.append(location.getRadius());
        if (location.getLocType() == BDLocation.TypeGpsLocation){
            sb.append("\nspeed : ");
            sb.append(location.getSpeed());
            sb.append("\nsatellite : ");
            sb.append(location.getSatelliteNumber());
            sb.append("\ndirection : ");
            sb.append("\naddr : ");
            sb.append(location.getAddrStr());
            sb.append(location.getDirection());
        } else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
            sb.append("\naddr : ");
            sb.append(location.getAddrStr());
            //运营商信息
            sb.append("\noperationers : ");
            sb.append(location.getOperators());
        }
        mLocationClient.stop();
        logMsg(sb.toString());
        Log.i("BaiduLocationApiDem", sb.toString());
    }


}


/**
 * 显示请求字符串
 * @param str
 */
public void logMsg(String str) {
    try {
        if (textView1 != null)
            textView1.setText(str);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

到底哪里不行,好郁闷啊,大神求解答

  • 写回答

1条回答

  • boy_nihao 2015-07-17 06:45
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序