Updated 30 June 2017
In the last part of the working with AMaps, we learned about adding marker click event and info windows. Now we will learn about adding Tile overlay on the Amap.
As we know that Amaps is a Chinese product and it provides services for China because China doesn’t support Google Services. In the default version of Amaps, you will not be able to see the detailed maps of the countries other than China. To be able to see the maps for the whole world you need to enable the tile overlay. TileOverlay actually covers the map with another image which contains a detailed map of the whole world.
To get the tile overlay on your Amap you need to use addTileOverlay() function of your Amap object. You can look at the code given below for an example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
final String mUrl = "http://a.tile.openstreetmap.org/%d/%d/%d.png"; your_amaps_object.addTileOverlay(new TileOverlayOptions().tileProvider(new UrlTileProvider(256, 256) { @Override public URL getTileUrl(int x, int y, int zoom) { try { return new URL(String.format(mUrl, zoom, x, y)); } catch (Exception e) { e.printStackTrace(); } return null; } }) .diskCacheEnabled(true) .diskCacheDir("/storage/emulated/0/amap/cache") .diskCacheSize(100000) .memoryCacheEnabled(true) .memCacheSize(100000)); |
this will automatically cache the images so that when you open the same area it won’t take much longer to load.
Secondly, In this blog, we will learn about the getting an address from the latitude and longitude and this is known as Regeocoding. For this, you need to implement an interface in your class GeocodeSearch.OnGeocodeSearchListener. The interface contains two methods which you need to override. First one is onGeocodeSearched(GeocodeResult geocodeResult, int i) which is used when you need to get the latitude and longitude of the location from its name and the other one is onRegeocodeSearched(RegeocodeResult result, int rCode) which is used to get the address from the Latitude and Longitude.
After implementing the interface, you need to initialize a LatLongPoint object and create an object of GeocodeSearch which is being used the get the lat long or the address. You need to set a listener on GeocodeSearch Object so that it returns the result in the onRegeocodeSearched function.
1 2 |
GeocodeSearch geocoderSearch = new GeocodeSearch(this); geocoderSearch.setOnGeocodeSearchListener(this); |
Now you need to create a RegeocodeQuery object and pass it to the getFromLocationAsyn function.
1 2 3 |
RegeocodeQuery query = new RegeocodeQuery(latLonPoint, 200, GeocodeSearch.AMAP); geocoderSearch.getFromLocationAsyn(query); |
Now if your Latitude and Longitude are correct it will return a list of addresses in your onRegeocodeSearched(RegeocodeResult result, int rCode) function and to get the result you can follow the below code.
1 2 3 4 5 6 7 8 9 |
@Override public void onRegeocodeSearched(RegeocodeResult result, int rCode) { if (rCode == AMapException.CODE_AMAP_SUCCESS) { if (result != null && result.getRegeocodeAddress() != null && result.getRegeocodeAddress().getFormatAddress() != null) { Log.d("TAG", result.getRegeocodeAddress().getFormatAddress()); } } } |
That’s all for this section, Use the locations and addresses as per your requirements.
Thank you very much, this is Vedesh Kumar signing off.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.