android获取网络定位_获取定位信息

在Android中,获取网络定位信息可以通过以下步骤实现:

1、添加权限

在AndroidManifest.xml文件中添加以下权限:



2、检查权限

在代码中检查是否已经获得了定位权限,如果没有则请求权限。

private static final int LOCATION_PERMISSION_REQUEST_CODE = 1;
private void checkLocationPermission() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ||
            ContextCompat.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}, LOCATION_PERMISSION_REQUEST_CODE);
    } else {
        getLocation();
    }
}

3、请求权限回调

重写onRequestPermissionsResult方法处理权限请求结果。

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == LOCATION_PERMISSION_REQUEST_CODE) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
            getLocation();
        } else {
            Toast.makeText(this, "定位权限被拒绝", Toast.LENGTH_SHORT).show();
        }
    }
}

4、获取位置信息

使用GoogleApiClient和LocationServices来获取位置信息。

private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
private FusedLocationProviderClient mFusedLocationClient;
private LocationCallback mLocationCallback;
private LatLng currentLatLng;
private MarkerOptions markerOptions;
private GoogleMap googleMap;
private List locationList = new ArrayList<>();
private Polyline polyline;
private boolean isFirstLoc = true;
private int index = 0;
private float distance = 0;
private float totalDistance = 0;
private float speed = 0;
private long startTime = 0;
private long endTime = 0;
private long duration = 0;
private boolean isTracking = false;
private boolean isFinished = false;
private boolean isPaused = false;
private boolean isPlaying = false;
private boolean isResumed = false;
private boolean isCancelled = false;
private boolean isSaved = false;
private boolean isDeleted = false;
private boolean isRestored = false;
private boolean isCleared = false;
private boolean isStarted = false;
private boolean isStopped = false;
private boolean isResumeFailed = false;
private boolean isPauseFailed = false;
private boolean isCancelFailed = false;
private boolean isSaveFailed = false;
private boolean isDeleteFailed = false;
private boolean isRestoreFailed = false;
private boolean isClearFailed = false;
private boolean isStartFailed = false;
private boolean isStopFailed = false;
private boolean isResumeSucceeded = false;
private boolean isPauseSucceeded = false;
private boolean isCancelSucceeded = false;
private boolean isSaveSucceeded = false;
private boolean isDeleteSucceeded = false;
private boolean isRestoreSucceeded = false;
private boolean isClearSucceeded = false;
private boolean isStartSucceeded = false;
private boolean isStopSucceeded = false;

分享文章:android获取网络定位_获取定位信息
本文URL:http://www.mswzjz.cn/qtweb/news0/467200.html

攀枝花网站建设、攀枝花网站运维推广公司-贝锐智能,是专注品牌与效果的网络营销公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 贝锐智能