【问题标题】:How to find my current location in android studio using Google Maps API?如何使用 Google Maps API 在 android studio 中找到我的当前位置?
【发布时间】:2020-06-19 09:32:55
【问题描述】:

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

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'
}

这是我运行时的程序

【问题讨论】:

标签: java android google-maps google-location-services


【解决方案1】:

Maps SDK for Android 提供了一个指南,用于使用可用于 Android 设备的位置数据获取您的当前位置。你可以在这里查看文档:https://developers.google.com/maps/documentation/android-sdk/location

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-27
    • 2012-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多