Android development and learning-Implementation of AMAP, android AMAP
1. First, make the following preparations:
1.1 http://lbs.amap.com/registered account
1.2 download positioning sdk and map sdk
This is the case after the download.
1.3 decompress the downloaded package
Add them in, right-click each jar and choose-Add As Library.
In this case, build. gradle is generated
dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.0.0' testCompile 'junit:junit:4.12' compile files('libs/Amap_2DMap_V4.2.0_20170209.jar') compile files('libs/AMap_Location_V3.3.0_20170118.jar')}
1.4 apply for API Key.
First, go to the console:
Create your own application
Create an application and obtain the corresponding key for reference to the following http://lbs.amap.com/faq/top/hot-questions/249
The development environment is configured.
2. display the map
2.1 Add the key applied for by the user key to AndroidManifest. xml
<Application android: allowBackup = "true" android: icon = "@ mipmap/ic_launcher" android: label = "@ string/app_name" android: supportsRtl = "true" android: theme = "@ style/AppTheme"> <meta-data android: name = "com. amap. api. v2.apikey "// This name value remains unchanged android: value =" "// value is the key applied above> </meta-data>
2.2 Add the required permissions in AndroidManifest. xml
// Basic permissions required for the map package and search package <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: n Ame = "android. permission. ACCESS_COARSE_LOCATION "/> // additional permissions required for locating packages and navigation packages (Note: basic permissions are also required) <uses-permission android: name =" android. permission. ACCESS_FINE_LOCATION "/> <uses-permission android: name =" android. permission. ACCESS_LOCATION_EXTRA_COMMANDS "/> <! -- <Uses-permission android: name = "android. permission. ACCESS_MOCK_LOCATION"/> --> <uses-permission android: name = "android. permission. CHANGE_WIFI_STATE"/>
2.3 add in xml
<com.amap.api.maps2d.MapView android:id="@+id/map_view" android:layout_width="match_parent" android:layout_height="match_parent" />
2.4 MainActivity: Manage the life cycle of a map
Public class MainActivity extends AppCompatActivity {private MapView mMapView = null; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // obtain the map control reference mMapView = (MapView) findViewById (R. id. map_view); // execute mMapView when onCreate is executed in the activity. onCreate (savedInstanceState) to implement map lifecycle management mMapView. onCreate (savedInstanceState);} @ Override protected void onDestroy () {super. onDestroy (); // execute mMapView when onDestroy is executed in activity. onDestroy () to implement mMapView for map lifecycle management. onDestroy () ;}@ Override protected void onResume () {super. onResume (); // execute mMapView when onResume is executed in the activity. onResume () to implement mMapView for map lifecycle management. onResume () ;}@ Override protected void onPause () {super. onPause (); // execute mMapView when onPause is executed in activity. onPause () enables mMapView for map lifecycle management. onPause () ;}@ Override protected void onSaveInstanceState (Bundle outState) {super. onSaveInstanceState (outState); // execute mMapView when onSaveInstanceState is executed in the activity. onSaveInstanceState (outState) enables mMapView for map lifecycle management. onSaveInstanceState (outState );}}
Run: