使用带有地址的Google API时获取一些空位置信息

时间:2018-04-04 10:03:08

标签: java android google-maps google-maps-api-3 google-geocoder

我一直在使用Google Map API并且已经获得用户当前位置,并且他们可以搜索其他位置。显示的信息用作地址。

当他们使用地图时,地址中的某些对象是空的,例如subThoroughfare或adminArea。其他时候,当用户搜索另一个位置时,它们不是空的并且包含信息。

我需要在不使用addressLine方法的情况下向用户显示地址,因为我需要将信息单独存储在我的数据库中。

方法getDeviceCurrentLocation,获取用户当前位置并在类中设置类变量,将PlaceInformation设置为来自obatined地址的数据

   /**
     * Get the devices current location after the user has agreed to that permission
     */
    private void getDeviceCurrentLocation()
    {
        Log.d(TAG, "getDeviceCurrentLocation: Getting the devices current location");
        fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(getActivity());
        try
        {
            if (locationPermissionGranted)
            {
                final Task location = fusedLocationProviderClient.getLastLocation();
                location.addOnCompleteListener(new OnCompleteListener()
                {
                    @Override
                    public void onComplete(@NonNull Task task)
                    {
                        if (task.isSuccessful())
                        {
                            // Found Location
                            Log.d(TAG, "onComplete: Found Location");
                            Location devicesCurrentLocation = (Location) task.getResult();
                            List<Address> list = new ArrayList<>();
                            Geocoder geocoder = new Geocoder(getActivity());
                            try
                            {
                                list = geocoder.getFromLocation(devicesCurrentLocation.getLatitude(), devicesCurrentLocation.getLongitude(), 1);
                            } catch (IOException io)
                            {
                                Log.e(TAG, io.getMessage());
                            }

                            if (list.size() > 0)
                            {
                                Address address = list.get(0);

                                placeInfo.setSubThoroughfare(address.getSubThoroughfare());
                                placeInfo.setThoroughfare(address.getThoroughfare());
                                placeInfo.setLocality(address.getLocality());
                                placeInfo.setPostCode(address.getPostalCode());
                                placeInfo.setPhoneNumber(address.getPhone());
                                placeInfo.setWebsiteUrl(address.getUrl());

                                moveCamera(new LatLng(devicesCurrentLocation.getLatitude(), devicesCurrentLocation.getLongitude()), DEFAULT_ZOOM, placeInfo);
                            } else
                            {
                                Toast.makeText(getActivity(), "Could'nt Find Location, Please Try Again", Toast.LENGTH_SHORT).show();
                                return;
                            }
                        } else
                        {
                            // Can't Find
                            Log.d(TAG, "onComplete: Can't Find Location");
                            Toast.makeText(getActivity(), "Unable To Find Your Current Location, Please Try Again", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        } catch (SecurityException e)
        {
            Log.e(TAG, "getDeviceLocation: SecurityException: " + e.getMessage());
        }
    }

屏幕截图,显示存储在地址中的信息。注意AddressLine如何包含完整地址。但其余信息不同,我无权访问

Image of stored address info

我可以使用更好的方法来获取当前的地址信息吗?

0 个答案:

没有答案