Register an Android map API key
Run: keytool-list-keystore ~ /. Android/debug. keystore
After registration, you will get the following webpage:
Your key is:
Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
This key applies to all applications that use the certificate corresponding to the following fingerprint for verification:
XX: XX
The following is an xml format example to help you understand the map function:
<Com. google. android. maps. MapView
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent" android: apiKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
/>
Simulator settings
Create a simulator "Google Inc.: Google APIs: 3.
Mobile Terminals must support Google Add-ons
As a mobile phone terminal manufacturer that cooperates with Google, if you want to integrate some Google applications, such as map, market, picasa, and sync, on your Android terminal, you need to sign a contract with Google, in this way, Google will have a dedicated person to help you integrate this series of services into your platform.
As a manufacturer of mobile phone terminals that do not cooperate with Google, we can use the method of cracking to integrate the Runtime Library in Add-ons into our own framework (build to the system of the mobile phone terminal. in img ). The procedure is as follows:
1. in Android, Google Add-ons (Google Maps) is supported. You need to extract the following files (pull from the standard SDK or pull from GPhone) package it into System. img. Google Add-ons only requires 1--2, and Goolge Maps (Google Map Program) requires 1--5:
1) system/framework com. google. android. maps. jar
2) system/etc/permissions com. google. android. maps. xml
3) system/framework com. google. android. gtalkservice. jar
4) system/etc/permissions com. google. android. gtalkservice. xml
5) system/app Maps.apk (Google Maps v3.0)
2. Source Code: Android Cupcake Release (tested and applicable to the donut platform as well .)
1) Compile the source code (to generate the most primitive Android content in the out/target directory)
2) Add the above files to the out/target/product/generic Directory, and use make snod to package the files in the source code root directory to obtain the new system. img
Permission and Map Library settings
Set full permissions in manifest. xml, for example:
<Uses-permission android: name = "android. permission. ACCESS_COARSE_LOCATION"/>
<Uses-permission android: name = "android. permission. INTERNET"/>
Add the Map Library to be used in manifest. xml:
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "com. example. package. name">
...
<Application android: name = "MyApplication">
<Uses-library android: name = "com. google. android. maps"/>
...
</Application>
...
</Manifest>
Map Library Analysis
The Maps Library provides more than a dozen classes. For details, refer to the examples.
(1) MapController
Controls map movement and scaling, centers around a GPS coordinate, controls view components in MapView, manages Overlay, and provides basic View functions. Use multiple Map modes (Map mode (some cities can update traffic conditions in real time), satellite mode, street view mode) to view Google Map.
Common Methods: animateTo (GeoPoint point) setCenter (GeoPoint point) setZoom (int zoomLevel.
(2) MapView
Mapview is used to display the view of a map. It is derived from android. view. ViewGroup. When MapView gets the focus, you can control the movement and scaling of the map.
Maps can be displayed in different forms, such as street view mode and satellite mode. The setSatellite (boolean) setTraffic (boolean) and setStreetView (boolean) methods are used.
MapView can only be created by MapActivity. This is because mapview needs to be connected to the network or file system through background threads, which must be managed by mapActivity.
In android 1.5, map zoom adopts the built-in mechanism. You can use setBuiltInZoomControls (boolean) to set whether to display the zoom control on the map.
Common Methods: getController () getOverlays () setSatellite (boolean) setTraffic (boolean), setStreetView (boolean) setBuiltInZoomControls (boolean.
(3) MapActivity
Manage the lifecycle of the Activity, and establish and cancel the connection to the map service for the mapview.
MapActivity is an abstract class. Any activity that wants to display MapView must be derived from MapActivity. In addition, a MapView instance must be created in onCreate () of its derived class. You can create a MapView instance by MapViewconstructor (and then add it to ViewGroup. addView (View) in View) or by layout XML.
(4) Overlay
Overlay is the top layer that covers MapView. You can extend its ondraw interface to display some of your own items in MapView. MapView manages Overlay through MapView. getOverlays.
In addition to the basic Overlay class, Google also extends two more useful Overlay classes.
1) MylocationOverlay-integrates the interface for receiving current coordinates in Android. location, and integrates the CompassSensor interface in SersorManager
We only need enableMyLocation () and enableCompass to enable our program to have real-time MyLocation and Compass functions (Activity. onResume ).
2) ItemlizedOverlay-manages an OverlayItem linked list and marks images and other resources with the same style on the map.
(5) Projection: the conversion between GPS coordinates and device coordinates (GeoPoint and Point) in MapView ).
A simple small example
Use a small program to demonstrate the development of the map function in android. The main function is to scale the map and add a menu so that you can manually select the display mode of the map.
Step 1: Create an android project. Note that the buildtarget to be selected here is "GoogleAPIs"
Step 2: Modify the menifest file:
<? Xmlversion = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "com. map. prac"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
<Uses-permission android: name = "android. permission. ACCESS_COARSE_LOCATION"/>
<Uses-permission android: name = "android. permission. INTERNET"/>
<Application android: icon = "@ drawable/icon" android: label = "@ string/app_name">
<Uses-library android: name = "com. google. android. maps"/>
<Activity android: name = ". MapViewPrac2"
Android: label = "@ string/app_name">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
</Application>
<Uses-sdk android: minSdkVersion = "3"/>
</Manifest>
Step 3: Modify the layout file, main. xml
<? Xmlversion = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: id = "@ + id/main"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<Com. google. android. maps. MapView
Android: id = "@ + id/map"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: enabled = "true"
Android: clickable = "true"
Android: apiKey = "??????????????????????????????????? "
/>
</LinearLayout>
Here, you need to set ???????????? Changed to the api key you applied.
Step 4: modify the code:
Package feisky. navigation;
Import com. google. android. maps. GeoPoint;
Import com. google. android. maps. MapActivity;
Import com. google. android. maps. MapController;
Import com. google. android. maps. MapView;
Import android. app. AlertDialog;
Import android. app. Dialog;
Import android. content. DialogInterface;
Import android. OS. Bundle;
Import android. view. KeyEvent;
Import android. view. Menu;
Import android. view. MenuItem;
Public class MainActivity extends MapActivity {
// Definition of Map Display control variables
Private MapView map = null;
Private MapController mapCon;
// Menu item
Final private int menuMode = Menu. FIRST;
Final private int menuExit = Menu. FIRST + 1;
Private int chooseItem = 0;
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
// Obtain MapView
Map = (MapView) findViewById (R. id. map );
// Set the Display Mode
Map. setTraffic (true );
Map. setSatellite (false );
Map. setStreetView (true );
// Set to zoom
Map. setBuiltInZoomControls (true );
// Set the central position of the initial map
GeoPoint geoBeijing = new GeoPoint (int) (39.95*1000000), (int) (116.37*1000000 ));
MapCon = map. getController ();
MapCon. setCenter (geoBeijing );
}
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
// Create a menu
Menu. add (0, menuMode, 0, "map mode ");
Menu. add (0, menuExit, 1, "quit ");
Return super. onCreateOptionsMenu (menu );
}
@ Override
Public boolean onKeyDown (int keyCode, KeyEvent event ){
Return super. onKeyDown (keyCode, event );
}
@ Override
Public boolean onMenuItemSelected (int featureId, MenuItem item ){
Switch (item. getItemId ()){
Case menuExit:
Finish ();
Break;
Case menuMode:
Dialog dMode = new AlertDialog. Builder (this)
. SetTitle ("map Mode settings ")
. SetSingleChoiceItems (R. array. MapMode, chooseItem, new DialogInterface. OnClickListener ()
{
@ Override
Public void onClick (DialogInterface dialog, int which ){
ChooseItem = which;
}
})
. SetPositiveButton ("OK", new DialogInterface. OnClickListener ()
{
@ Override
Public void onClick (DialogInterface dialog, int which ){
Switch (which ){
Case 0:
Map. setSatellite (true );
// Map. setTraffic (false );
// Map. setStreetView (false );
Break;
Case 1:
// Map. setSatellite (false );
Map. setTraffic (true );
// Map. setStreetView (false );
Break;
Case 2:
// Map. setSatellite (false );
// Map. setTraffic (false );
Map. setStreetView (true );
Break;
Default:
Break;
}
}
})
. SetNegativeButton ("cancel", new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
}
})
. Create ();
DMode. show ();
Break;
Default:
Break;
}
Return super. onMenuItemSelected (featureId, item );
}
@ Override
Protected boolean isRouteDisplayed (){
Return false;
}
}