Android Studio中的地图片段中的当前位置
创始人
2024-08-18 10:00:37
0

要在Android Studio中获取设备的当前位置,可以使用Android的定位服务。以下是一个示例代码,演示如何在Android Studio中获取设备的当前位置并在地图上显示出来:

  1. 首先,确保在AndroidManifest.xml文件中添加以下权限:

  1. 在你的Activity或Fragment中,添加以下代码来获取设备的当前位置:
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.widget.Toast;

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

import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback {

    private static final int LOCATION_PERMISSION_REQUEST_CODE = 1;
    private GoogleMap mMap;
    private FusedLocationProviderClient mFusedLocationClient;
    private LocationCallback mLocationCallback;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            mMap.setMyLocationEnabled(true);
            startLocationUpdates();
        } else {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    LOCATION_PERMISSION_REQUEST_CODE);
        }
    }

    private void startLocationUpdates() {
        LocationRequest locationRequest = LocationRequest.create();
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationRequest.setInterval(5000); // 5 seconds

        mLocationCallback = new LocationCallback() {
            @Override
            public void onLocationResult(LocationResult locationResult) {
                if (locationResult == null) {
                    return;
                }
                for (Location location : locationResult.getLocations()) {
                    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
                    mMap.clear(); // remove previous marker
                    mMap.addMarker(new MarkerOptions().position(latLng));
                    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15));
                }
            }
        };

        mFusedLocationClient.requestLocationUpdates(locationRequest, mLocationCallback, null);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == LOCATION_PERMISSION_REQUEST_CODE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                        == PackageManager.PERMISSION_GRANTED) {
                    mMap.setMyLocationEnabled(true);
                    startLocationUpdates();
                }
            } else {
                Toast.makeText(this, "Location permission denied. Please grant the permission to use this feature.",
                        Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mLocationCallback != null) {
            mFusedLocationClient.removeLocationUpdates(mLocationCallback);
        }
    }
}

在这个示例中,我们使用了Google Maps API来显示地图,并使用了FusedLocationProviderClient来获取设备的当前位置。首先,我们检查是否已授予了定位权限。如果已授予权限,我们启用地图的我的位置功能,并开始定位更新。如果没有授予权限,我们会请求用户授予权限。

在startLocationUpdates()方法中,我们创建了一个LocationRequest对象,设置了定位的优先级和更新间隔。然后,我们使用FusedLocationProviderClient的requestLocationUpdates()方法来请求位置更新。当位置更新时,回调函数onLocationResult()会被调用。在这个回调函数中,我们获取最新的位置,并

相关内容

热门资讯

透视透视!wepoker辅助器... 透视透视!wepoker辅助器安装包定制,购买wepoker模拟器(透视)扑克教程(有挂揭秘);1)...
透视透视挂!wepoker能不... 透视透视挂!wepoker能不能透视,wepoker辅助器是真的吗(透视)透牌教程(有挂黑科技)所有...
透视透视挂!we poker辅... 透视透视挂!we poker辅助器v3.3,wepoker怎么买辅助(透视)专业教程(有挂解密);1...
透视智能ai!wepoker怎... 透视智能ai!wepoker怎么破解游戏,WePoKer辅助器(透视)可靠教程(有挂教程)1、首先打...
透视透视!wepoker游戏下... 透视透视!wepoker游戏下载,wepoker有辅助器吗(透视)总结教程(有挂辅助)1、wepok...
透视玄学!wepoker私人局... 透视玄学!wepoker私人局有透视吗,wepoker祈福有用吗(透视)解密教程(有挂介绍)wepo...
透视玄学!wepoker分析,... 透视玄学!wepoker分析,wepoker透视器免费(透视)AI教程(有挂方法);一、wepoke...
透视新版!wepoker底牌透... 透视新版!wepoker底牌透视,wepoker数据分析工具(透视)详细教程(有挂黑科技)1、这是跨...
透视线上!wepoker脚本下... 透视线上!wepoker脚本下载,wepoker轻量版透视方法(透视)线上教程(有挂教程);1、很好...
透视真的!wepokerplu... 透视真的!wepokerplus外挂,wepoker有透视吗(透视)透明教程(有挂工具)1、wepo...