Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 技術總結(016)—— 使用百度地圖api獲取當前位置

Android 技術總結(016)—— 使用百度地圖api獲取當前位置

編輯:關於Android編程

上一篇:/kf/201208/151974.html
[java] 
package location.demo; 
 
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(); 
    } 
     
     
     
     

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved