繁体   English   中英

如何使用谷歌地图 API 在 android 工作室找到我当前的位置?

[英]How to find my current location in android studio using Google Maps API?

基本上,我编写了用于查找当前位置的代码,我使用的是 Android 10.0 和 API 30 以及最新的 Android Studio 版本。 并且安装了所有 SDK 工具,但是当我启动程序时,它没有显示我的位置,它显示了谷歌的总部位置。 这是我的 function 找到我的位置:

private void getDeviceLocation()
{
        final LocationRequest locationRequest = new LocationRequest();
        locationRequest.setInterval(1000);
        locationRequest.setFastestInterval(3000);
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

        LocationServices.getFusedLocationProviderClient(MyMapActivity.this)
                .requestLocationUpdates(locationRequest, new LocationCallback()
                {
                    @Override
                    public void onLocationResult(LocationResult locationResult)
                    {
                        super.onLocationResult(locationResult);
                        LocationServices.getFusedLocationProviderClient(MyMapActivity.this)
                                .removeLocationUpdates(this);
                        if(locationResult != null && locationResult.getLocations().size() > 0)
                        {
                            int latestLocationIndex = locationResult.getLocations().size() -1;
                            double latitude = 
                               locationResult.getLocations().get(latestLocationIndex).getLatitude();
                            double longitude = 
                               locationResult.getLocations().get(latestLocationIndex).getLongitude();
                            moveCamera(new LatLng(latitude, longitude), DEFAULT_ZOOM);
                         }
                     }
                }, Looper.getMainLooper());
}

那是我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.myapplication">

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:usesCleartextTraffic="true"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:targetApi="m">


        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="AIzaSyB7NJtTOFDD9iWFI33EM0HRcoRx25YnGWE"/>

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <activity
            android:name=".MapsActivity"
            android:label="@string/title_activity_maps">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MyMapActivity"/>
    </application>

</manifest>

那是我的依赖

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.google.android.gms:play-services-maps:17.0.0'
    implementation "com.google.android.gms:play-services-location:17.0.0"
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

这就是我运行它时的程序

这就是我运行它时的程序

Android 的地图 SDK 提供了使用可用于 Android 设备的位置数据获取当前位置的指南。 您可以在此处查看文档: https://developers.google.com/maps/documentation/android-sdk/location

您还可以在 Github 上的 ApiDemos 存储库中下载代码示例: https://developers.google.com/maps/documentation/android-sdk/location#code_samples

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM