求高手指点    在调用百度 sdk 的时候,设定了5秒钟获取一次定位 在本地测试结果可以5秒取一次 但是在室外移动的时候。就不是5秒获取一次了。有时候几分钟能有一个结果。有时候要十分钟才会有一个结果回来啊package com.yookey.activity;import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;public class TestDemoActivity extends Activity {

private Context myContext;
private Button button;
private LocationClient mLocClient;
public MyLocationListenner myListener = new MyLocationListenner();
private int locationTime = 5000;
private String path;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.testdemo);
        
        myContext = this;
        try {
          mLocClient = new LocationClient( myContext );
             mLocClient.registerLocationListener( myListener );
             
             path = Environment.getExternalStorageDirectory().toString()+ "/Location";
             File file = new File(path);
             if(!file.exists()){
              file.mkdirs();
             }
             
             button = (Button)findViewById(R.id.test_Button01);
             button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
 setLocationOption();
             mLocClient.start();
}
});
             
} catch (Exception e) {
writeFileData(path,e.getMessage());
e.printStackTrace();
}
    }
    
protected void onDestroy() {
super.onDestroy();
mLocClient.stop();
}    
    
  //设置相关参数
   private void setLocationOption(){
   LocationClientOption option = new LocationClientOption();
   option.setOpenGps(true); //打开gps
   option.setCoorType("bd0911"); //设置坐标类型
   option.setScanSpan(locationTime); //设置定位模式,小于1秒则一次定位;大于等于1秒则定时定位
   mLocClient.setLocOption(option);
   }
  
/**
 * 写入
 * @param fileName
 * @param message
 */
public void writeFileData(String path, String content) { 
        try {
         File file = new File(path+"/test.txt"); 
         FileOutputStream fos = new FileOutputStream(file,true);
         fos.write(content.getBytes());
         fos.write("\r\n".getBytes());
         fos.close();
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 

   /**
 * 监听函数,又新位置的时候,格式化成字符串,输出到屏幕中
 */
public class MyLocationListenner implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
if (location.getAddrStr() == null){
System.out.println("没有获取到数据!");
writeFileData(path,"没有获取到数据!");
return ;
}

StringBuffer sb = new StringBuffer();
SimpleDateFormat  formatter  =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");     
String nowTime = formatter.format( new Date(System.currentTimeMillis()));//当前时间
sb.append("\n 纬度 : ");
sb.append(location.getLatitude());
sb.append("\n 经度 : ");
sb.append(location.getLongitude());
sb.append("\n 详细地址 : ");
sb.append(location.getAddrStr());
sb.append("\n 获取时间: ");
sb.append(nowTime);
Toast.makeText(getApplicationContext(), sb.toString(), 1).show();
writeFileData(path,sb.toString());
}

//发起POI查询请求调用
public void onReceivePoi(BDLocation poiLocation) {
if (poiLocation == null){
return ;
}
StringBuffer sb = new StringBuffer(256);
sb.append("Poi time : ");
sb.append(poiLocation.getTime());
sb.append("\nerror code : ");
sb.append(poiLocation.getLocType());
sb.append("\nlatitude : ");
sb.append(poiLocation.getLatitude());
sb.append("\nlontitude : ");
sb.append(poiLocation.getLongitude());
sb.append("\nradius : ");
sb.append(poiLocation.getRadius());
if (poiLocation.getLocType() == BDLocation.TypeNetWorkLocation){
sb.append("\naddr : ");
sb.append(poiLocation.getAddrStr());

if(poiLocation.hasPoi()){
sb.append("\nPoi:");
sb.append(poiLocation.getPoi());
}else{
sb.append("noPoi information");
}
writeFileData(path,sb.toString() + "onReceivePoi");

StringBuffer sBuffer = new StringBuffer();
SimpleDateFormat  formatter  =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");     
String nowTime = formatter.format( new Date(System.currentTimeMillis()));
sBuffer.append("\n onReceivePoi + 纬度 : ");
sBuffer.append(poiLocation.getLatitude());
sBuffer.append("\n onReceivePoi + 经度 : ");
sBuffer.append(poiLocation.getLongitude());
sBuffer.append("\n onReceivePoi + 详细地址 : ");
sBuffer.append(poiLocation.getAddrStr());
sBuffer.append("\n onReceivePoi + 获取时间: ");
sBuffer.append(nowTime);
Toast.makeText(getApplicationContext(), sBuffer.toString(), 1).show();
}
}

}
代码都赋上了,求高手指点   移动的时候就会出现获取的频率变慢。android百度 SDK 获取位置信息

解决方案 »

  1.   


    首先推荐楼主使用
    //混合了百度 与 手机本身的gps定位
    public class MixedGPS extends BaseGPS implements Runnable
    ……定位取值就根据他们的准确性了可以使用百度的定位监听
    // 开启百度位置监听
    public void openBaiduGPS()
    {
    mBaiduLocationManager
    .enableProvider(MKLocationManager.MK_NETWORK_PROVIDER);
    mBBaiduWifiOpen = true; mBaiduLocationManager.enableProvider(MKLocationManager.MK_GPS_PROVIDER); // 设置最小间隔
    // mBaiduLocationManager.setNotifyInternal(iMinSecond, iMinSecond); mBaiduLocationManager.requestLocationUpdates(mBaiduLocationListener);
    }手机的定位监听
    mPhoneLocationManager.requestLocationUpdates(
    LocationManager.GPS_PROVIDER, mMinTimeMillis, mMinDistance,
    mPhoneLocationListener); mPhoneLocationManager.requestLocationUpdates(
    LocationManager.NETWORK_PROVIDER, mMinTimeMillis, mMinDistance,
    mPhoneLocationListener); // 开启监听GPS状态
    mPhoneLocationManager.addGpsStatusListener(mPhoneGPSStatusListener);如果你觉得无法打到效果 你还可以使用a-gps 基站获取地址
    http://download.csdn.net/detail/banketree/4766383
    http://download.csdn.net/detail/banketree/4696737
    然后自己定时去获取……