我剛剛創建了一些標記的地圖活動,同時點擊標記位置的標記提取地址爲教育目的,一切工作正常。谷歌地圖提取地址甚至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();
}
}
});
}
}
所以你說谷歌的地圖應用程序可以在沒有在線訪問的情況下工作,並且可以找到地址,顯示地圖,找到2點之間的路線等等。 – pskink
我沒有檢查路線,但地圖工作正常,沒有互聯網。 –
我們在談論地址,對吧?比如'英國倫敦SW1A 2AA威斯敏斯特10號唐寧街10號,而不是'51.503396°N 0.127640°W' – pskink