Hello, this is the first in a series of posts that will do the following:

  • I. Map: Use Autonavi SDK to achieve the effect of Didi Chuxing

  • Ii. Navigation: Using Autonavi SDK to achieve the effect of Didi Driver (customized navigation interface)

Nowadays, maps are widely used in various apps, such as Didi Chuxing and Mobike. Map requirements you may meet, I use Gaode map for two years, I believe I can make your map development journey less encounter some bugs.

Integrate autonavi SDK

First, we visited autonavi development platform to download the SDK

Download from lbs.amap.com/api/android…

Because we’re going to use it later

Map, navigation, location, reverse geocoding (search function SDK) please select these to download the image above has been selected.

Although autonavi 6.0 to demonstrate, but due to autonavi SDK6.0 version has a crash bug problem (only navigation SDK6.0 has) so I use an old version to develop here to explain, to avoid causing misunderstanding, the old SDK in the codeCopy the code

Build the project integration SDK configuration build.gradle file

Let’s create a new Android project. As follows:

After this, we will create a Module on this project. Please select Android Library and name it AmapLibrary

Please select Android Library where we will place the Autonavi SDK

It’s for later portability. And a lot of good things. I’ll tell you about it later.

All right. Our app project and Library are ready.

Unzip the autonavi SDK you just downloaded and the folder is as follows:

Although the following uses gaode 6.0 to demonstrate, but because gaode SDK6.0 version has a crash bug problem (only navigation SDK6.0 has) so I use an old version to develop here to explain, to avoid causing everyone's misunderstanding, the old SDK in the codeCopy the code

Copy these two files to the libs directory in AmapLibrary

As follows:

Gradle file in the build.gradle file and in the AmapLibrary

The android TAB is as follows:

sourceSets {
    main {
        jniLibs.srcDirs = ['libs']
    }
}
Copy the code

This was added because we put the Armeabi library under libs

Then add it under the defaultConfig TAB

ndk {
    abiFilters 'armeabi'
}
Copy the code

Build. Gradle as follows:

Note that build.gradle under app should also be added

Then add AmapLibrary to build. Gradle

implementation fileTree(include: ['*.jar'], dir: 'libs')
Copy the code

replace

compile fileTree(include: ['*.jar'], dir: 'libs')
Copy the code

So that the app can use the LIBS library in the AmapLibrary project (very important)

dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') implementation' com. Android. Support: appcompat - v7:26.1.0 'testImplementation junit: junit:' 4.12 ' AndroidTestImplementation 'com. Android. Support. Test: runner:' 1.0.1 androidTestImplementation 'com. Android. Support. Test. Espresso: espresso - core: 3.0.1'}Copy the code

The configuration of AndroidManifest

Androidmanifest.xml file under AmapLibrary project

Please note that we only need to add in the Androidmanifest.xml file under the AmapLibrary project

You don’t need to add it to the Androidmanifest.xml file in the app project (because these things are automatically packaged into an Androidmanifest.xml file when you package them)

The androidmanifest.xml for AmapLibrary is shown below

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.yisingle.amap.lib" > <! -- Permissions required for maps --> <! - allows programs to open the network socket - > < USES - permission android: name = "android. Permission. INTERNET" / > <! <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <! - allows programs to access network status - > < USES - permission android: name = ". Android. Permission ACCESS_NETWORK_STATE "/ > <! - allows programs to access the WiFi network information - > < USES - permission android: name = "android. Permission. ACCESS_WIFI_STATE" / > <! -- Allows applications to read and write phone status and identity --> <uses-permission Android :name="android.permission.READ_PHONE_STATE" /> <! - allows programs to access CellID or WiFi hotspot to get rough location - > < USES - permission android: name = "android. Permission. ACCESS_COARSE_LOCATION" / > <! - used to access the GPS - > < USES - permission android: name = "android. Permission. ACCESS_FINE_LOCATION" / > <! - to get wifi access permissions, wifi positioning information will be used to carry out network - > < USES - permission android: name = "android. Permission. CHANGE_WIFI_STATE" / > <! - used to apply for call - A - GPS module > < USES - permission android: name = "android. Permission. ACCESS_LOCATION_EXTRA_COMMANDS" / > <! - used to apply for BLUETOOTH information indoor positioning - > < USES - permission android: name = "android. Permission. BLUETOOTH" / > < USES - the permission android:name="android.permission.BLUETOOTH_ADMIN" /> <! Android :name="android.permission.WAKE_LOCK" /> <application> <meta-data Android :name="com.amap.api.v2.apikey" Android :value=" application "/> </manifest> <meta-data Android :name="com.amap.api.v2.apikey" Android :value=" Need to apply on Autonavi open platform "/> After we finish this work, we will integrate autonavi SDKCopy the code

Apply the apikey of autonavi SDK

Applying for the Autonavi SDK’s APIKey requires two key things.

  1. One is the package name of the application
  2. One is sh1 for the application’s packaged signature file

Application package name:

In this project please note that we are taking the package name of app not the package name of AmapLirary (remember)

App package name is: com. Yisingle. Study the map. One

The diagram below:

Package sh1 for signature files

The diagram below:

Click Build generate singed apk in AndroidStudio to generate the signature

Then click next to see the picture below:

Click ok

Generate good

Create a folder named key in the project and put the signature file in it as shown below:

Then configure the package to use the generated signature in build.gradle in your app.

Add it under the Android TAB in the build.gradle file of app

signingConfigs { signinfo { keyAlias 'key0' keyPassword '123456789' storeFile file('.. /key/studyone.jks') storePassword '123456789' } }Copy the code

Modify the code under the buildTypes tag as follows:

buildTypes {
    release {
        minifyEnabled false
        signingConfig signingConfigs.signinfo
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    debug {
        minifyEnabled false
        signingConfig signingConfigs.signinfo
      
    }
}
Copy the code

Note that signingConfigs comes before buildTypes.

This allows us to package the debug file with the generated signature. Even if the computer project is changed, there will not be the problem of incorrect signature.

The build.gradle file under app project is as follows:

apply plugin: 'com.android.application' android { compileSdkVersion 26 defaultConfig { applicationId "com.yisingle.study.map.one" 18 targetSdkVersion 26 versionCode minSdkVersion 1 versionName testInstrumentationRunner "1.0" "android.support.test.runner.AndroidJUnitRunner" ndk { abiFilters 'armeabi' } } signingConfigs { signinfo { keyAlias 'key0' keyPassword '123456789' storeFile file('.. /key/studyone.jks') storePassword '123456789' } } buildTypes { release { minifyEnabled false signingConfig signingConfigs.signinfo proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } debug { minifyEnabled false signingConfig signingConfigs.signinfo } } sourceSets { main { jniLibs.srcDirs = ['libs'] } } } dependencies { implementation fileTree(dir: 'libs', include: [' *. Jar ']) implementation 'com. Android. Support: appcompat - v7:26.1.0' implementation 'com. Android. Support. The constraint, the constraint - layout: 1.0.2' testImplementation junit: junit: '4.12' androidTestImplementation 'com. Android. Support. Test: runner:' 1.0.1 androidTestImplementation 'com. Android. Support. Test. Espresso: espresso - core: 3.0.1'}Copy the code

The obtain signature file command can also be used on Windows

Copy:

38:86:88:CF:78:05:5C:C4:F5:D5:B4:0A:3C:24:E9:11:3A:58:12:85

This is SHA1 for my signature file

Alright

Register an autonavi development platform account

Here I will not introduce in detail please see the autonavi development platform registration process is very simple

Enter the application management page. Click Application Management. Click Create an application

Then the following screen will pop up and you can just fill it in

Once created, click Add new Key

Then enter SHA1 and the package name and click Submit

And then we’re going to get key

Fill it in under project AmapLirary

<meta-data
    android:name="com.amap.api.v2.apikey"
    android:value="7a91a3b1ccdf395223bcf4f511d5697c" />
Copy the code

Then we import the AmapLibrary project as a library for our app and do this

Add it to build. Gradle of your app project

implementation project(':AmapLibrary')
Copy the code

validation

Ok, let’s verify that the configuration is successful.

Write the following code in the app MainActivity interface

package com.yisingle.study.map.one; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.TextView; import com.amap.api.services.core.LatLonPoint; import com.amap.api.services.route.BusRouteResult; import com.amap.api.services.route.DriveRouteResult; import com.amap.api.services.route.RideRouteResult; import com.amap.api.services.route.RouteSearch; import com.amap.api.services.route.WalkRouteResult; import com.yisingle.amap.lib.GaoDeErrorUtils; public class MainActivity extends AppCompatActivity { private RouteSearch routeSearch; private TextView tvInfo; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvInfo = findViewById(R.id.tvInfo); routeSearch = new RouteSearch(this); routeSearch.setRouteSearchListener(new RouteSearch.OnRouteSearchListener() { @Override public void onBusRouteSearched(BusRouteResult busRouteResult, int i) { } @Override public void onDriveRouteSearched(DriveRouteResult driveRouteResult, Int I) {/ / verification success tvInfo setText (GaoDeErrorUtils. GetErrorInfo (I)); } @Override public void onWalkRouteSearched(WalkRouteResult walkRouteResult, int i) { } @Override public void onRideRouteSearched(RideRouteResult rideRouteResult, int i) { } }); startConfimGaode(); } public void startConfimGaode() {LatLonPoint from = new LatLonPoint(30.537107, 104.06951); LatLonPoint to = new LatLonPoint(30.657349, 104.065837); RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(from, to); RouteSearch.DriveRouteQuery query = new RouteSearch.DriveRouteQuery(fromAndTo, RouteSearch.DRIVING_SINGLE_SHORTEST, null, null, ""); routeSearch.calculateDriveRouteAsyn(query); Tvinfo.settext (" Using map in path planning "); } @Override protected void onDestroy() { super.onDestroy(); } public void test(View view) { startConfimGaode(); } } package com.yisingle.amap.lib; import java.util.HashMap; /** * @author jikun * Created by jikun on 2018/3/8. */ //http://lbs.amap.com/api/android-sdk/guide/map-tools/error-code/ Public class GaoDeErrorUtils {private static HashMap<Integer, String> errorMAP = new HashMap<>(); Static {errormap. put(1000, "request ok" + "service call ok, result returned "); Errormap. put(1001, "developer signature failed "); Errormap. put(1002, "User Key is incorrect or expired "); Errormap. put(1003, "no permission to use corresponding interface "); Errormap. put(1008, "MD5 security code failed to be verified "); Errormap. put(1009, "Request Key does not match binding platform "); Errormap. put(1012, "insufficient permission, service request rejected "); Errormap. put(1013, "This Key was deleted "); Errormap. put(1100, "engine service response error "); Errormap. put(1101, "engine return error "); Errormap. put(1102, "Autonavi server request timeout "); Errormap. put(1103, "read service result timeout "); Errormap. put(1200, "Request parameter is invalid "); Errormap. put(1201, "required parameter missing in request condition "); Errormap. put(1202, "service request protocol is invalid "); Errormap. put(1203, "server unknown error "); Errormap. put(1800, "Server added error "); Errormap. put(1801, "protocol resolution error "); Errormap. put(1802, "Socket connection timeout - SocketTimeoutException"); errormap. put(1802," Socket connection timeout - SocketTimeoutException"); Errormap. put(1803, "URL exception - MalformedURLException"); errormap. put(1803," URL exception - MalformedURLException"); Errormap. put(1804, "Unknown host - UnKnowHostException"); Errormap. put(1806, "HTTP or socket connection failure - ConnectionException"); Errormap. put(1900, "unknown error "); Errormap. put(1901, "parameter invalid "); Errormap. put(1902, "I/O exception - IOException"); Errormap. put(1903, "NullPointException - NullPointException"); Errormap. put(2000, "Tableid format is not correct "); Errormap. put(2001, "Data ID does not exist "); Errormap. put(2002, "Cloud retrieval server under maintenance "); Errormap. put(2003, "Key corresponding tableID does not exist "); Errormap. put(2100, "userID cannot be found "); Errormap. put(2101, "App Key not enabled "nearby function "); Errormap. put(2200, "Clear tables while enabling automatic upload or enable single point upload "); Errormap. put(2201, "USERID is invalid "); Errormap. put(2202, "NearbyInfo object is empty "); Errormap. put(2203, "The interval between two single uploads is less than 7 seconds "); Errormap. put(2204, "Point is empty or the same as last upload "); Errormap. put(3000, "Planning points (including starting point, ending point and passing point) are not within the mainland of China "); Errormap. put(3001, "No road can be found near the planning point (including starting point, ending point and passing point) "); Errormap. put(3002, "Route calculation failed, usually due to road connectivity "); Errormap. put(3003, "Failed to calculate the path by walking because the distance between the start and end is too long." ); Errormap. put(4000, "Short string sharing authentication failed "); Errormap. put(4001, "Short string request failed "); } public static String getErrorInfo(int code) { if (null ! = errorMAP.get(code)) { return errorMAP.get(code); } else {return "unknown error "; }}}Copy the code

Okay, so if the request is fine and the service call is fine, and the result returns a code of 1000 then we’ve integrated successfully.

Download address of the above code:

Gitee.com/justforgame…