2 anny lin Anny_Lin 于 2015.07.05 19:15 提问

android studio 配置百度SDK定位失败 3C

图片说明

就实现基础的定位功能,下面是主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个回答

u012975705
u012975705   2015.07.17 14:45
Csdn user default icon
上传中...
上传图片
插入图片