1

我剛剛創建了一些標記的地圖活動,同時點擊標記位置的標記提取地址爲教育目的,一切工作正常。谷歌地圖提取地址甚至android設備如何離線

即使設備未連接到互聯網,它仍在提取地址。我沒有給出清單中的互聯網許可。 ACCESS_FINE_LOCATION是唯一授予該應用的權限。

我的問題是:應用程序如何能夠獲得地址甚至設備處於離線模式?背後有什麼想法或技術!!?

問題:我從來沒有在我的代碼中使用過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(); 
      } 
     } 
    }); 

    } 
} 
+0

所以你說谷歌的地圖應用程序可以在沒有在線訪問的情況下工作,並且可以找到地址,顯示地圖,找到2點之間的路線等等。 – pskink

+0

我沒有檢查路線,但地圖工作正常,沒有互聯網。 –

+0

我們在談論地址,對吧?比如'英國倫敦SW1A 2AA威斯敏斯特10號唐寧街10號,而不是'51.503396°N 0.127640°W' – pskink

回答

3

它似乎緩存內存是答案:

,然後從緯度和經度,你可以使用地理編碼器類爲獲得完整的地址。我已經檢查了第一次與互聯網,所以應用程序商店它緩存多數民衆贊成爲什麼我仍然得到地圖加載和獲取相同的經緯度和設備離線地址。

問題中提到的代碼正常工作。感謝@pskink。

0

你可以得到緯度和經度它通過網絡,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(); 
+0

我的代碼工作正常,我可以得到地址。它背後的想法是什麼。我從未在我的代碼中使用{getLastKnownLocation}。 Wifi,gps,互聯網關閉,沒有插入SIM卡。 –

+0

他們可以使用離線工作的地理編碼類獲取語言 – Ashish

+1

這意味着地理編碼器無需任何這些gps,wifi和互聯網的幫助工作? –