Android谷歌地图与当前位置
创始人
2024-10-08 04:30:36
0

要在Android应用中使用谷歌地图并获取当前位置,可以按照以下步骤进行操作:

  1. 在项目的AndroidManifest.xml文件中添加以下权限:



  1. build.gradle文件中添加以下依赖项:
implementation 'com.google.android.gms:play-services-maps:17.0.0'
implementation 'com.google.android.gms:play-services-location:17.0.0'
  1. 在布局文件中添加地图视图:

  1. MainActivity.java文件中添加以下代码:
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.widget.Toast;

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.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {

    private static final int PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1;
    private GoogleMap googleMap;
    private FusedLocationProviderClient fusedLocationProviderClient;
    private LocationCallback locationCallback;

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

        MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.mapFragment);
        mapFragment.getMapAsync(this);

        fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);

        locationCallback = new LocationCallback() {
            @Override
            public void onLocationResult(LocationResult locationResult) {
                if (locationResult == null) {
                    return;
                }
                for (Location location : locationResult.getLocations()) {
                    // 在此处获取到当前位置的经纬度信息
                    LatLng currentLocation = new LatLng(location.getLatitude(), location.getLongitude());
                    googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLocation, 15));
                }
            }
        };
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        this.googleMap = googleMap;
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            googleMap.setMyLocationEnabled(true);
        } else {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                if (ContextCompat.checkSelfPermission(this,
                        Manifest.permission.ACCESS_FINE_LOCATION)
                        == PackageManager.PERMISSION_GRANTED) {
                    googleMap.setMyLocationEnabled(true);
                    startLocationUpdates();
                }
            } else {
                Toast.makeText(this,
                        "需要授权定位权限以获取当前位置。",
                        Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void startLocationUpdates() {
        LocationRequest locationRequest = LocationRequest.create()
                .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                .setInterval(10000)
                .setFastestInterval(5000);
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            fusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, null);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            startLocationUpdates();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        fusedLocationProviderClient.removeLocationUpdates(locationCallback);
    }
}

上述代码中,我们首先申请了所需的权限,包括INTERNET、ACCESS_CO

相关内容

热门资讯

黑科技游戏(wepoke黑科技... 黑科技游戏(wepoke黑科技)wepower软件(wEpoke)总是真的有挂(有挂讲解)-哔哩哔哩...
黑科技辅助挂(wepoke透明... 黑科技辅助挂(wepoke透明黑科技)aapoker透明挂(wEPOKE)从前存在有挂(有挂解密)-...
黑科技辅助(wepoke辅助插... 黑科技辅助(wepoke辅助插件)wepower透视辅助真假(wepoKE)一贯是真的有挂(有挂辅助...
黑科技游戏(wepoke黑科技... 黑科技游戏(wepoke黑科技)WePoKe外挂(wEPOKE)总是真的是有挂(存在有挂)-哔哩哔哩...
黑科技数据(wepoke真的有... 黑科技数据(wepoke真的有挂)wepower伙牌(WEpoke)真是真的有挂(有挂技巧)-哔哩哔...
黑科技教学(wepoke真的有... 黑科技教学(wepoke真的有挂)wepoke透明挂辅助软件是真的吗(wepoker)切实是真的有挂...
黑科技游戏(wepoke真的有... 黑科技游戏(wepoke真的有挂)wepoke ai(wEpOke)其实真的有挂(确实有挂)-哔哩哔...
黑科技系统(wepoke辅助插... 《软件透明挂》是一款多人竞技的辅助透视游戏,你将微扑克对手来到同一个战场,为至高无上的荣耀进行一次自...
黑科技插件(wepoke辅助插... 黑科技插件(wepoke辅助插件)WePoKe外挂(wEpOke)总是是有挂(有挂功能)-哔哩哔哩;...
黑科技新版(wepoke智能a... 黑科技新版(wepoke智能ai)wepoke是机器发牌吗(wEpoKe)真是是真的有挂(有挂攻略)...