1

私はちょうどいくつかのマーカーで地図の活動を作成し、マーク目的地のマークされた位置のアドレスを取得するマーカータイトルをクリックしながら、すべてうまくいきます。どのようにグーグルマップを取得するアドレスもアンドロイドデバイスはオフラインです

デバイスがインターネットに接続されていない場合でも、アドレスを取得しています。私はマニフェストでインターネット許可を与えなかった。 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(); 
      } 
     } 
    }); 

    } 
} 
+0

Googleの 'Maps'アプリは、オンラインアクセスなしで動作し、アドレスを見つけ、地図を表示し、2つのポイント間のルートなどを見つけることができると言っていますか? – pskink

+0

私は経路をチェックしなかったが、マップはインターネットなしで正常に動作する。 –

+0

私達は住所について話しています、そうですか? 「51.503396°N 0.127640°W」ではなく「10 Downing St、Westminster、London SW1A 2AA」のようなものです。 – pskink

答えて

3

が答えです。私はインターネットで最初にチェックしたので、アプリストアはキャッシュしているので、地図の読み込みと同じLatのアドレスとデバイスのオフラインのアドレスを取得するのはなぜですか。

質問に記載されているコードが正常に動作します。 @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}を使ったことはありません。無線LAN、GPS、インターネットはオフであり、SIMカードは挿入されていません。 –

+0

オフラインで作業するジオコードクラスを使用して言語を取得できます – Ashish

+1

これは、ジオコーダがこれらのgps、wifi、およびインターネットの助けなしで動作することを意味しますか? –

関連する問題