使用百度SDK进行定位完整Demo

时间:2022-12-30 21:19:39

百度SDK定位Demo
我想对于开发者来说,最想看到的就是代码,所以就直接贴上代码:

package com.example.demo;

import com.baidu.mapapi.SDKInitializer;

import android.app.Application;

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        SDKInitializer.initialize(this);
    }
}
package com.example.demo;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements BDLocationListener,OnClickListener{
    /* 定位相关. */
    private LocationClient mLocationClient = null;
    private boolean isFirstLoc = true;
    /* UI显示. */
    private MapView mMapView = null;
    private BaiduMap mBaiduMap = null;
    /* UI操作. */
    private Button locBtn = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        locBtn = (Button) findViewById(R.id.locationBtn);
        mMapView = (MapView) findViewById(R.id.bmapView);
        locBtn.setOnClickListener(this);
        startLocation();
    }
    private void startLocation() {
        mBaiduMap = mMapView.getMap();
        /* 开启定位图层. */
        mBaiduMap.setMyLocationEnabled(true);
        /* 定位初始化. */
        mLocationClient = new LocationClient(this);
        /* 注册监听. */
        mLocationClient.registerLocationListener(this);
        /* 设置定位条件. */
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true);
        option.setIsNeedAddress(true);
        option.setCoorType("bd0911");
        option.setScanSpan(1000);
        option.setPriority(LocationClientOption.NetWorkFirst);
        mLocationClient.setLocOption(option);
        mLocationClient.start();
    }
    @Override
    protected void onResume() {
        super.onResume();
        mMapView.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        mMapView.onPause();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }
    @Override
    public void onReceiveLocation(BDLocation location) {
        if(mMapView == null || location == null){
            return;
        }
        Toast.makeText(MainActivity.this, location.getAddrStr(), Toast.LENGTH_LONG).show();
        MyLocationData data = new MyLocationData.Builder()
        .accuracy(location.getRadius())
        .direction(100)
        .latitude(location.getLatitude())
        .longitude(location.getLongitude())
        .build();
        mBaiduMap.setMyLocationData(data);
        if(isFirstLoc){
            isFirstLoc = false;
            LatLng ll = new LatLng(location.getLatitude(), location.getLongitude());
            MapStatusUpdate mu = MapStatusUpdateFactory.newLatLng(ll);
            mBaiduMap.animateMapStatus(mu);
        }
    }
    @Override
    public void onClick(View v) {
        startLocation();
    }
}

需要的权限:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.GET_TASKS" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <uses-permission android:name="android.permission.BAIDU_LOCATION_SERVICE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
    <uses-permission android:name="android.permission.READ_LOGS" />

代码已经过验证,请放心使用。
代码中需要的定位jar,和.so文件,请直接去百度定位API,进行下载。