即使Android设备离线,谷歌地图如何获取地址

时间:2017-10-06 06:26:16

标签: android google-maps android-permissions google-maps-api-2

我刚刚创建了一个带有一些标记的地图活动,并且点击标记标题获取标记位置的地址用于教育目的,一切正常。

即使设备未连接到互联网,它也会提取地址。我没有在清单中给予互联网许可。 ACCESS_FINE_LOCATION是应用程序的唯一权限。

我的问题是:应用程序如何获取地址甚至设备处于离线模式?它背后的想法或技术是什么!!?

Nb: 我的代码中从未使用getLastKnownLocation方法。 Wifi和gps都关闭,没有插入SIM卡。

这是我的代码:

    public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    TextView mapPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        mapPosition = (TextView) findViewById(R.id.textplace);

        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }


    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
       // double lat = Double.valueOf(8.524139);
        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151); LatLng ind = new LatLng(8.524139, 76.936638);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.addMarker(new MarkerOptions().position(ind).title("Marker in Trivandrum"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(ind));

        mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {






                return false;
            }
        });

    mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
        @Override
        public void onInfoWindowClick(Marker marker) {
            Geocoder geocoder;
            List<Address> addresses;
            geocoder = new Geocoder(MapsActivity.this, Locale.getDefault());

            try {
                addresses = geocoder.getFromLocation(marker.getPosition().latitude, marker.getPosition().longitude, 1);

                // Here 1 represent max location result to returned, by documents it recommended 1 to 5

                String address = addresses.get(0).getAddressLine(0);
                // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
                String city = addresses.get(0).getLocality();
                String state = addresses.get(0).getAdminArea();
                String country = addresses.get(0).getCountryName();
                String postalCode = addresses.get(0).getPostalCode();
                String knownName = addresses.get(0).getFeatureName();
                String countrycode = addresses.get(0).getCountryCode();

                mapPosition.setText(""+ address);

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });

    }
}

2 个答案:

答案 0 :(得分:3)

它似乎是缓存内存的答案。我已经第一次使用互联网进行了检查,因此应用程序存储它缓存,这就是为什么我仍然可以获取地图加载并获取设备离线时相同lat和lng的地址。

问题中提到的代码工作正常。感谢@pskink。

答案 1 :(得分:1)

您可以通过网络,gps或location.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)(无互联网)获取纬度和经度。

https://stackoverflow.com/a/22085632/3864698。也许它可以帮助你。

然后从纬度和经度,您可以使用地理编码器类获取完整地址:

    Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());

addresses = geocoder.getFromLocation(latitude, longitude, 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5

String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
String city = addresses.get(0).getLocality();
String state = addresses.get(0).getAdminArea();
String country = addresses.get(0).getCountryName();
String postalCode = addresses.get(0).getPostalCode();
String knownName = addresses.get(0).getFeatureName();