百度地图API的开发(1)

以下demo实现使用百度API获取当前位置
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.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 
 
public class main extends Activity { 
     
    private LocationClient mLocationClient = null; 
    private Button mStartBtn = null; 
    private TextView mTextView = null; 
     
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
         
        setContentView(R.layout.main); 
         
        mStartBtn = (Button)findViewById(R.id.button1); 
        mTextView = (TextView)findViewById(R.id.textview1); 
         
        mLocationClient = new LocationClient(this); 
        LocationClientOption option = new LocationClientOption(); 
        option.setOpenGps(true);                                //打开gps 
        option.setCoorType("bd09ll");                           //设置坐标类型为bd09ll 
        option.setPriority(LocationClientOption.NetWorkFirst);  //设置网络优先 
        option.setProdName("locSDKDemo2");                      //设置产品线名称 
        option.setScanSpan(5000);                               //定时定位,每隔5秒钟定位一次。 
        mLocationClient.setLocOption(option); 
        mLocationClient.registerLocationListener(new BDLocationListener() { 
            @Override 
            public void onReceiveLocation(BDLocation location) { 
                if (location == null) 
                    return ; 
                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()); 
                } else if (location.getLocType() == BDLocation.TypeNetWorkLocation){ 
                    sb.append("\naddr : "); 
                    sb.append(location.getAddrStr()); 
                } 
                sb.append("\nsdk version : "); 
                sb.append(mLocationClient.getVersion()); 
                mTextView.setText(sb.toString()); 
            } 
             
            public void onReceivePoi(BDLocation location){ 
                //return ; 
            } 
        }); 
         
         
        mStartBtn.setOnClickListener(new OnClickListener() { 
            @Override 
            public void onClick(View v) { 
                if (mLocationClient == null) return ; 
                if (mLocationClient.isStarted()){ 
                    mLocationClient.stop(); 
                    mStartBtn.setText("开始"); 
                } else { 
                    mLocationClient.start(); 
                    mStartBtn.setText("关闭"); 
                } 
            } 
        }); 
         
    } 
     
    @Override 
    public void onDestroy(){ 
        if (mLocationClient != null && mLocationClient.isStarted()){ 
            mLocationClient.stop(); 
            mLocationClient = null; 
        } 
        super.onDestroy(); 
    } 
     
     
     
     
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值