This section describes how to use Google map on Android phones. We generally use Google map API + location API, that is, map API + status API, if we add some server and network knowledge, we can create very creative applications, and these are the popular lbs, the main technologies involved include map API + status API + javaee SERVER + mobile phone communication + XMPP protocol (mainly instant communication ). We can use our imagination to make a good and creative application. For example, we saw a beautiful girl in the car and were embarrassed to say hello, but she also had the LBS application on her mobile phone, then we can query the number of people using this application in the current range of meters, and then lock her on the car. Then, you know. Or if we are on a business trip somewhere, open the app and identify the map to find the food we like. All these are rooted in the basic map service, as long as we integrate resources properly.
Well, let's get started with Google map programming.
1: download requiredGoogle MapExtended LibraryThe download method is through AVD, which is the same as downloading the latest SDK.
2:ApplicationAndroid map API key:
Enter the path address under the command line,Go to c: \ Users \ User Name \. Android:Enter
Keytool-list-v-keystore Debug. keystore,At this time, it will usually prompt you to enter the password. The default password isAndroid,You enter Android,If the Android app is not displayed, press Enter.In this way, all three fingerprint certificates are listed, including MD5, sha1, and sha256. (The key password is Android)
The certificate fingerprint is displayed as follows:
3:Write down the authentication fingerprint and goHttp://code.google.com/android/maps-api-signup.html,
After checking the check box, enter your authentication fingerprint and clickGenerate API keyThen you applied for yourKey, of course, the premise is that you must log on. (You may need a Google account or have registered a Google market ),
When you apply for the Google Maps API, You need to verify the MD5 fingerprint certificate. Then, enter the MD5 certificate in the text box (note that it is MD5 instead of sha1)
The generated key is:
Now that the key is available, encode the Code as follows:
1: Create a project and select Google APIs.
2: Must be inAndroidmanifest. xmlFile declaration:<Uses-library Android: Name = "com. Google. Android. Maps"/>
3: Of course, we need to access the network, or we also need WiFi location, so we should add some permissions.
View code
< Uses-Permission Android: Name = "Android. Permission. access_wifi_state" />
< Uses-Permission Android: Name = "Android. Permission. access_checkin_properties" />
< Uses-Permission Android: Name = "Android. Permission. access_coarse_location" />
< Uses-Permission Android: Name = "Android. Permission. access_network_state" />
< Uses-Permission Android: Name = "Android. Permission. Internet" />
< Uses-Permission Android: Name = "Android. Permission. access_fine_location" />
4: Create a layout file and add:
View code
<? XML version = "1.0" encoding = "UTF-8" ?>
< Linearlayout Xmlns: Android = "Http://schemas.android.com/apk/res/android"
Android: layout_width = "Fill_parent"
Android: layout_height = "Fill_parent"
Android: Orientation = "Vertical" >
< Com. Google. Android. Maps. mapview
Xmlns: Android = "Http://schemas.android.com/apk/res/android"
Android: ID = "@ + ID/mapview"
Android: layout_width = "Fill_parent"
Android: layout_height = "Fill_parent"
Android: apikey = "0xfqxjaipzqeutu7mtm2pqdwplhcfk93iagli0w"
Android: clickable = "True" >
</ Com. Google. Android. Maps. mapview >
</ Linearlayout >
5: Define activity to inherit from mapactivity. Here we can define menus to switch between different map modes: Street, satellite, transportation, etc.
Here we need to know three objects: mapview display map mapcontroller control map, of course, the most important thing is how to locate a map point, according to the junior high school geography, you can use the longitude and latitude to define any point of the Earth, the geopoint object of Google API is used here.
Below isCode:
View code
Package Com. Dongzi;
Import Android. content. context;
Import Android. Location. location;
Import Android. Location. locationmanager;
Import Android. OS. Bundle;
Import Android. View. Menu;
Import Android. View. menuitem;
Import Android. View. window;
Import Com. Google. Android. Maps. geopoint;
Import Com. Google. Android. Maps. mapactivity;
Import Com. Google. Android. Maps. mapcontroller;
Import Com. Google. Android. Maps. mapview;
/**
* The location API and map API are combined to locate through the location API and then display the map in the map API:
* @ Author Lenovo
*
*/
Public Class Googlemapdemoactivity Extends Mapactivity {
Private Static Final String tag = googlemapdemoactivity. Class . Getname ();
Private Mapview = Null ;
Private Mapcontroller = Null ;
Int Latitudee6 = 39*1000000;
Int Longitudee6 = 116*1000000;
Int Zoomlevel = 12;
@ Override
Public Void Oncreate (bundle savedinstancestate ){
Super . Oncreate (savedinstancestate );
Requestwindowfeature (window. feature_no_title );
Setcontentview (R. layout. Main );
Mapview = (mapview) findviewbyid (R. Id. mapview );
// Set the street mode to zoom in
Mapview. setstreetview ( True ); // Mapview. settraffic (true); mapview. setsatellite (false );
Mapview. setbuiltinzoomcontrols ( True );
// Get Map Controller
Mapcontroller = mapview. getcontroller ();
Mapcontroller. setzoom (zoomlevel );
// The default value is Beijing longitude and latitude.
Geopoint gpoint = New Geopoint (latitudee6, longitudee6 ); // Beijing longitude and latitude are displayed by default.
Mapcontroller. animateto (gpoint );
}
@ Override
Protected Boolean Isroutedisplayed (){
Return False ;
}
@ Override
Public Boolean Oncreateoptionsmenu (menu ){
Super . Oncreateoptionsmenu (menu );
Menu. Add (0, 0, 0, "Sub-District ");
Menu. Add (0, 1, 0, "Traffic ");
Menu. Add (0, 2, 0, "satellite ");
Return True ;
}
@ Override
Public Boolean Onoptionsitemselected (menuitem item ){
Super . Onoptionsitemselected (item );
Switch (Item. getitemid ()){
Case 0:
// Show street chart
Mapview. setstreetview ( True );
Mapview. settraffic ( False );
Mapview. setsatellite ( False );
Break ;
Case 1:
// Show traffic chart
Mapview. setstreetview ( False );
Mapview. settraffic ( True );
Mapview. setsatellite ( False );
Break ;
Case 2:
// Show satellite map
Mapview. setstreetview ( False );
Mapview. settraffic ( False );
Mapview. setsatellite ( True );
Break ;
Default : Break ;
}
Return True ;
}
// Obtain the current longitude and latitude based on the position API and return the geopoint object.
Private Geopoint getcurrentgeopoint (){
Locationmanager = (locationmanager) getsystemservice (context. location_service );
Location = locationmanager. getlastknownlocation (locationmanager. network_provider );
Return New Geopoint (( Int ) (Location. getlatitude () * 1e6 ),( Int ) (Location. getlongpolling () * 1e6 ));
}
}
Of course, we must also use the status API to obtain the longitude and latitude based on the current location of the mobile phone, and then display it in the map.
Note: We can set the default language of our mobile phone to Chinese so that the route will be displayed in Chinese.
RunProgram
The most important classes in the Google map API package.
1. mapactivity: this class is an activity class used to display map. It needs to connect to the underlying network.
2. mapview: A View component used to display a map. It must be used with mapactivity.
3. mapcontroller: used to control the movement of a map.
4. Overlay: This is a plotting object that can be displayed on a map.
5. geopoint: an object that contains the longitude and latitude positions.
Android location API:
1. locationmanager: This class provides the ability to access the location service and to obtain the best location provider. In addition, the near alert function.
2. locationprovider: this class is the abstract class of the positioning provider. The location provider can periodically report the geographical location of devices.
3. locationlistener: Provides the callback function when the location information changes. You must register the listener object in the locating manager in advance.
4. Criteria: This class enables the application to select an appropriate location provider by setting attributes in the locationprovider.
How can we obtain the longitude and latitude of the Local Machine? We can useGPS sensor,