java - 设置当前位置

标签 java android

这是更新的内容,需要使用不同的方法还是其他方法来完成?

我想在 Google map 上获取我的当前位置,现在正在做的事情如下。

创建 GoogleMaps Activity , 从 Android SDK 管理器下载 GooglePlay 服务, 在 AndroidManifest 中授予互联网和位置权限,

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

授予在 OnMapReady 中使用我的位置的权限

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

    mMap.setMapType(googleMap.MAP_TYPE_TERRAIN);

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    mMap.setMyLocationEnabled(true);
}

当我尝试在我的设备 LG Spirit H440 上运行此代码时,我没有得到我期望的结果,它应该让我可以选择在 map 上使用蓝点获取当前位置。

我还在 Google API 控制台中创建了 API KEY

我做错了什么?

最佳答案

尝试使用此代码

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


            locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
            locationListener = new LocationListener() {
                @Override
                public void onLocationChanged(Location location) {
                    mMap.clear();
                    LatLng userLocation = new LatLng(location.getLatitude(), location.getLongitude());
                    mMap.addMarker(new MarkerOptions().position(userLocation).title("Your Location"));
                    mMap.moveCamera(CameraUpdateFactory.newLatLng(userLocation));
                }

                @Override
                public void onStatusChanged(String s, int i, Bundle bundle) {

                }

                @Override
                public void onProviderEnabled(String s) {

                }

                @Override
                public void onProviderDisabled(String s) {

                }
            };

if (ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);
            } else {
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
                Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

                mMap.clear();
                LatLng userLocation = new LatLng(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude());
                mMap.addMarker(new MarkerOptions().position(userLocation).title("Your Location"));
                mMap.moveCamera(CameraUpdateFactory.newLatLng(userLocation));
            }

以及 onRequestPermissionsResult:

public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == 1) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
                }
            }
        }
    }

此外,当您创建 API key 时,我相信您已将其包含在 AndroidManifest 中。

来源罗伯·珀西瓦尔/尼克·沃尔特。

关于java - 设置当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48135604/

相关文章:

java RMI 新手——关于 SSL 和 auth/.rate 限制 RMI 服务的一些基本问题

java - 安装 Java 时在 Ubuntu 上安装 Elastic Search 失败

java - 如何将现有的Java程序转移到Android? (要导入什么)

java - 以编程方式创建布局 - 两个基本问题

java - 如何使用按钮从 EditText 中删除最后一个字母?

java - 创建元素时插入语法错误

java - 我无法让我的图标显示在我的 JLabel swing 组件中

android - 正确动画删除 ListView 中的行?

android - Kotlin 1.5.0 不适用于 Dagger 2?

java - 一起使用GroupLayout和FlowLayout : OutPut is not appearing in Center