谷歌地图提取地址甚至android设备如何离线
我刚刚创建了一些标记的地图活动,同时点击标记位置的标记提取地址为教育目的,一切工作正常。谷歌地图提取地址甚至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();
}
}
});
}
}
它似乎缓存内存是答案:
,然后从纬度和经度,你可以使用地理编码器类为获得完整的地址。我已经检查了第一次与互联网,所以应用程序商店它缓存多数民众赞成为什么我仍然得到地图加载和获取相同的经纬度和设备离线地址。
问题中提到的代码正常工作。感谢@pskink。
你可以得到纬度和经度它通过网络,GPS或location.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)(不包括互联网)。
看到这个https://*.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();
我的代码工作正常,我可以得到地址。它背后的想法是什么。我从未在我的代码中使用{getLastKnownLocation}。 Wifi,gps,互联网关闭,没有插入SIM卡。 –
他们可以使用离线工作的地理编码类获取语言 – Ashish
这意味着地理编码器无需任何这些gps,wifi和互联网的帮助工作? –
所以你说谷歌的地图应用程序可以在没有在线访问的情况下工作,并且可以找到地址,显示地图,找到2点之间的路线等等。 – pskink
我没有检查路线,但地图工作正常,没有互联网。 –
我们在谈论地址,对吧?比如'英国伦敦SW1A 2AA威斯敏斯特10号唐宁街10号,而不是'51.503396°N 0.127640°W' – pskink