MapBoxMap 之 定位

 

使用MapBoxMap的api进行实现定位

功能实现参考com.mapbox.mapboxsdk.location;包下LocationComponent类

直接上代码:

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.os.Looper;
import android.widget.Toast;

import com.example.myfirstapp.R;
import com.mapbox.android.core.location.LocationEngine;
import com.mapbox.android.core.location.LocationEngineCallback;
import com.mapbox.android.core.location.LocationEngineProvider;
import com.mapbox.android.core.location.LocationEngineRequest;
import com.mapbox.android.core.location.LocationEngineResult;

public class GetLocationActivity extends AppCompatActivity implements LocationEngineCallback {
    //位置更新之间的距离
    public static final float DEFAULT_DISPLACEMENT = 5.0f;
    //位置更新的最大等待时间(以毫秒为单位)。
    private static final long DEFAULT_MAX_WAIT_TIME = 10000L;
    //位置更新的最快间隔(以毫秒为单位)
    private static final long DEFAULT_FASTEST_INTERVAL = 2000L;
    //位置更新之间的默认间隔
    public static final long DEFAULT_INTERVAL = 5000L;

    private LocationEngineRequest mLocationEngineRequest = new LocationEngineRequest.Builder(DEFAULT_INTERVAL)
            //要求最准确的位置
            .setPriority(LocationEngineRequest.PRIORITY_HIGH_ACCURACY)
            //请求经过电池优化的粗略位置
//            .setPriority(LocationEngineRequest.PRIORITY_BALANCED_POWER_ACCURACY)
            //要求粗略〜10 km的准确位置
//            .setPriority(LocationEngineRequest.PRIORITY_LOW_POWER)
            //被动位置:除非其他客户端请求位置更新,否则不会返回任何位置
//            .setPriority(LocationEngineRequest.PRIORITY_NO_POWER)
            //设置位置更新之间的距离
            .setDisplacement(DEFAULT_DISPLACEMENT)
            //设置位置更新的最大等待时间(以毫秒为单位)。
            .setMaxWaitTime(DEFAULT_MAX_WAIT_TIME)
            //设置位置更新的最快间隔(以毫秒为单位)
            .setFastestInterval(DEFAULT_FASTEST_INTERVAL)
            .build();

    private LocationEngine mLocationEngine;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 

        Mapbox.getInstance(this, getString(R.string.access_token));

        setContentView(R.layout.activity_get_location);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //请求位置
        mLocationEngine = LocationEngineProvider.getBestLocationEngine(this);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 0);
            return;
        }
        mLocationEngine.requestLocationUpdates(mLocationEngineRequest, this, Looper.getMainLooper());
    }

    @Override
    protected void onPause() {
        super.onPause();
        //结束请求位置
        if (mLocationEngine != null) {
            mLocationEngine.removeLocationUpdates(this);
        }
    }

    //implements LocationEngineCallback 位置结果回调
    @Override
    public void onSuccess(LocationEngineResult result) {
        Location lastLocation = result.getLastLocation();
        if (lastLocation != null) {
            double latitude = lastLocation.getLatitude();
            double longitude = lastLocation.getLongitude();
            Toast.makeText(this, "onSuccess LatLng: " + latitude + "," + longitude, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onFailure(@NonNull Exception exception) {
        Toast.makeText(this, "onFailure : " + exception.getMessage(), Toast.LENGTH_SHORT).show();
    }
}

值得注意的是要处理位置权限。

你可能感兴趣的:(MapBoxMap,地图使用指南)