📜  Android Google Map显示当前位置(1)

📅  最后修改于: 2023-12-03 14:39:07.767000             🧑  作者: Mango

Android Google Map显示当前位置

Google Map是一款常用的地图应用程序,我们可以使用它在我们的应用程序中显示地图和当前位置。在本篇文章中,我们将介绍如何使用Google Map API在Android应用程序中显示当前位置。

环境配置

在开始之前,您需要确保您的环境已正确地配置了Google Map API。

首先,您需要在Google开发者控制台中创建一个 Google Maps API 密钥,并将其添加到您的 Android 应用程序中。

其次,您需要在您的AndroidManifest.xml文件中添加以下代码片段:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application>
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="YOUR_API_KEY" />
</application>

ACCESS_FINE_LOCATION 权限允许应用程序访问精确位置信息,YOUR_API_KEY 应该被替换为您在Google Developers Console中创建的 API 密钥。

使用Google Map API显示当前位置

我们将使用Google Maps Android API在地图上显示当前位置。首先,我们将在布局文件中添加一个 MapView

<com.google.android.gms.maps.MapView
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/toolbar"
    android:layout_marginTop="8dp"
    app:liteMode="false"
    app:mapType="normal" />

接下来,我们需要在Java代码中初始化 MapView

private MapView mapView;
private GoogleMap googleMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mapView = findViewById(R.id.mapView);
    mapView.onCreate(savedInstanceState);
    mapView.getMapAsync(this);
}

我们需要在onMapReady()方法中获取当前位置并在地图上显示它。

@Override
public void onMapReady(GoogleMap googleMap) {
    this.googleMap = googleMap;
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE);
        return;
    }
    googleMap.setMyLocationEnabled(true); // 显示当前位置
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
    googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15)); // 设置地图中心点和缩放级别
}

我们使用 setMyLocationEnabled(true) 方法来显示当前位置。然后使用 getLastKnownLocation() 方法获取设备上次记录的位置信息。最后使用经纬度创建一个 LatLng 对象,并使用 moveCamera() 方法在地图上显示。

权限请求处理

我们需要在应用程序中请求用户授予定位权限。如果权限被拒绝,我们将无法显示当前位置。

private static final int REQUEST_CODE = 1;

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == REQUEST_CODE) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            onMapReady(googleMap);
        } else {
            Toast.makeText(this, "Location permission is required", Toast.LENGTH_SHORT).show();
            finish();
        }
    }
}
结论

在本文中,我们学习了如何使用Google Maps Android API在Android应用程序中显示当前位置。我们还对Google Maps Android API的其他功能进行了浏览。您可以使用这些功能来创建您自己的自定义地图应用程序。