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

來源:互聯網
上載者:User

上一篇:http://www.bkjia.com/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(); 
    } 
     
     
     
     
} 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.