百度地圖api擷取經緯度和城市名稱,地圖api擷取經緯度

來源:互聯網
上載者:User

百度地圖api擷取經緯度和城市名稱,地圖api擷取經緯度
想整合百度API定位需要完成以下步驟:1:註冊開發人員
2:申請key
3:下載jar包
4:代碼整合先看下效果:

1:註冊開發人員最好提前申請,審核需要1,2天
2:申請key本人用的是Android Studio,所以用命令列查看SHA1碼
3:下載jar包只需下載"定位功能"的開發包即可
4:代碼整合(詳細可見官方講解,以下代碼以簡潔實用為主)

package com.union.roid.testroid;import android.app.Activity;import android.os.Bundle;import android.util.Log;import com.baidu.location.BDLocation;import com.baidu.location.BDLocationListener;import com.baidu.location.LocationClient;import com.baidu.location.LocationClientOption;public class MainActivity extends Activity {    public LocationClient mLocationClient = null;    public BDLocationListener myListener = new MyLocationListener();    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mLocationClient = new LocationClient(getApplicationContext());//聲明LocationClient類        mLocationClient.registerLocationListener(myListener);//註冊監聽函數        LocationClientOption option = new LocationClientOption();//佈建要求參數        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//設定定位元模式        option.setCoorType("bd09ll");//返回的定位結果是百度經緯度,預設值gcj02        option.setScanSpan(5000);//設定發起定位請求的間隔時間為5000ms        option.setIsNeedAddress(true);//返回的定位結果包含地址資訊        option.setNeedDeviceDirect(true);//返回的定位結果包含手機機頭的方向        mLocationClient.setLocOption(option);        mLocationClient.start();//啟動請求        if (mLocationClient != null && mLocationClient.isStarted())            mLocationClient.requestLocation();        else            Log.d("Location", "locClient is null or not started");    }    public class MyLocationListener implements 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());            }            Log.v("--->", "=========");            Log.v("--->", sb.toString());        }    }}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.