Updated 29 May 2017
While working on a Chinese project. We faced a problem which is in China the Google services are blocked. Yes, you will not have any of the important Google services like Firebase, maps etc.
We found an alternative for the maps which is AMaps. AMaps actually works just like google maps but it’s actually a Chinese product. So to get the locations, maps, and routes in China you can use AMaps as an alternative.
Let’s get started with showing a basic map on your in your activity.
First, you need to setup your AMaps and for that, you need to register yourself here and get a Key to use it’s all functionality in the application. Key is not required to show the maps, locations, and markers but it is essential for showing routes and other functionalities similar to it.
After getting the Key from the site. You need to download SDKs from here. You can also download the sample code from here which is very helpful to understand and use in your application.
Install the libraries in your project and add the key (if you have) in your AndroidManifest.xml file in meta-data tag.
1 2 3 |
<meta-data android:name="com.amap.api.v2.apikey" android:value="your_key"/> |
And also the required permission to access internet and location and if you want to work with offline maps you can also add read-write store permission.
1 2 3 4 5 6 |
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> |
Now when you are all set to work with AMaps. Let’s begin with creating a layout for maps.
To get the maps you need to add the maps tag in your XML layout file like the below code.
1 2 3 4 |
<com.amap.api.maps.MapView android:id="@+id/your_map_id" android:layout_width="match_parent" android:layout_height="match_parent"/> |
You can always change the width and height attributes to change map view this will represent the area in which the map will be displayed.
After adding the maps in your layout you can instantiate it in onCreate method of your Activity like the below code.
1 2 3 4 5 6 7 8 9 |
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.your_activity_layout); MapView your_map_view = (MapView) findViewById(R.id.your_map_id); your_map_view.onCreate(savedInstanceState); AMap your_amap = your_map_view.getMap(); } |
That’s all !!! Connect your device and start working with AMaps. We will discuss some other features of AMaps in the next blog.
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.