Android 工作室获取地图的最后已知位置并使用微调器设置圆半径

Android studio get last known location for maps and using a spinner to set circle radius

我正在创建一个带有 google 地图和以用户位置为中心的圆半径的应用程序。我希望用户使用微调器选择所需的圆半径(1-10 公里半径)。但是当我使用微调器的值时,半径圆没有出现。

对于位置,我可以通过以下方式获得显示我外貌的蓝点:map.setMyLocationEnabled(true);我可以手动输入地图的位置进行放大。但是,当我尝试放大并在设备位置设置圆心时,出现 nullpointerexception 错误。下面是我的位置代码,吐司只是为了让我看到我得到了实际位置。

private void getDeviceLocation() {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]
                {Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);

        return;
    }
    Task<Location> task = mFusedLocationClient.getLastLocation();
    task.addOnSuccessListener(MainActivity.this, new OnSuccessListener<Location>() {
        @Override
        public void onSuccess(Location location) {
            if (location != null) {
                currentLocation = location;
                Toast.makeText(MainActivity.this, "Current Position " + currentLocation.getLongitude(), Toast.LENGTH_LONG).show(); //Just to make sure we have a value
                SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager()
                        .findFragmentById(R.id.map2);
                supportMapFragment.getMapAsync(MainActivity.this);
            }

        }
    });
}

然后我使用 userPosition 来放大地图并设置圆心

    public void onMapReady(GoogleMap map) {

    LatLng userPosition = new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude());

    mGoogleMap = map;
    map.setMyLocationEnabled(true);
    map.animateCamera(CameraUpdateFactory.newLatLngZoom(userPosition, 10f));

    map.addCircle(new CircleOptions()
            .center(userPosition)
            .radius(mapRadius)
            .strokeColor(0x330073FF)
            .fillColor(0x330073FF)
            .strokeWidth(2));

    map.getUiSettings().setZoomControlsEnabled(true);
    map.getUiSettings().setMyLocationButtonEnabled(true);
}

但是我在 userPosition 行收到错误:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 'double android.location.Location.getLatitude()' 在 com.example.test.MainActivity.onMapReady(MainActivity.java:362).

我从一个教程中获得了代码,它似乎对他们有用,并尝试了许多不同的教程,代码也略有不同,所以不确定我做错了什么。我试过搜索,知道其他人也有类似的问题,但仍然没有解决。

至于半径,当我手动设置位置并写入时效果很好如果我自己以米为单位写入半径则效果很好。但是,如果我尝试使用我的微调器和 mapRadius 值,当我 运行 模拟器时,半径圆根本不会显示。这是 onCreate

中的代码
spinnerRadius = findViewById(R.id.spinner_radius);
    ArrayAdapter<CharSequence> spinnerAdapter = ArrayAdapter.createFromResource(this,
            R.array.spinner_radius_option, android.R.layout.simple_spinner_item);
    spinnerRadius.setAdapter(spinnerAdapter);
    spinnerRadius.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            mapRadiusInput = parent.getItemAtPosition(position).toString();
            mapRadius = Double.parseDouble(mapRadiusInput)*1000; //converting to meters
            Toast.makeText(MainActivity.this, ""+mapRadius, Toast.LENGTH_LONG).show();
        }

我是编程初学者,android我在这两个问题上花了很多时间,如果很容易解决我也不会感到惊讶。任何帮助或指示将不胜感激。

添加圆圈时,将 return 值 (Circle) 保存在字段变量中,Circle myCircle

然后在 onItemSelected 中用值(以米为单位)更新圆半径。

因此,首先将变量声明为 class 字段变量(用于稍后的范围可见性)。我假设这是一个整体 class - 如果不是,您需要让 onItemSelected 回调实例可以访问此变量。

private Circle myCircle;

然后在加圆的时候,保存:

myCircle = map.addCircle(new CircleOptions()
        .center(userPosition)
        .radius(mapRadius)
        .strokeColor(0x330073FF)
        .fillColor(0x330073FF)
        .strokeWidth(2));

并在您的 onItemSelected 中更新圆(以米为单位)

myCircle.setRadius(mapRadius);

这是显示相同内容的另一个示例:

和 API 文档:https://developers.google.com/android/reference/com/google/android/gms/maps/model/Circle.html#setRadius(double)

关键是在创建时保存Circle。正如您从 API 中看到的那样,通过 Circle 参考,还有其他可以使用的设置器。