[Android learning 16th] -- feature 1: map mobile phone

Source: Internet
Author: User

 Disclaimer: The book "Secrets of Android Application Development", which records the logs of the book, references the relevant code and summary, and has no commercial use, it is completely a record of self-learning, and many problems will inevitably occur in learning just now. If there are any mistakes, please criticize them a lot.

I. map mobile phone map

GoogleMap is an important tool for finding addresses on PCs. It also provides MAP implementation on mobile phones in Android systems.

 

Step 1: apply for a certificate for using Android Map

The following figure shows how to implement GoogleMap for mobile phones. You need to use the android map API. before using the android map API, you need to apply for an android map API key. You must prepare a Google account, you can also use a Gmail account. The application procedure is as follows:

Step 1. Find your Debug. keystore File

The file path is generally:

C: \ Documents ents and Settings \ administrator. NJC-11-0000 (current user) \. Android \ Debug. keystore. Of course, the simplest way is to open eclipse and choose windows> preference> Android> build. The default debug keystore value is the Debug. keystore path.

Step 2. Obtain the MD5 value of DEBUG. keystore

Enter the path of the Debug. keystore file at the command prompt and run: keytool-list-keystore Debug. keystore. You will be prompted to enter the password. Enter the default password: Android to obtain the MD5 value. For example:

Step 3. Apply for the android map API key

Use a browser to log on to the website:

Obtain the obtained API key.

For example, the page is garbled, but it does not affect.

 

The corresponding API key value obtained in the above three steps is saved here for later use in the program, which is equivalent to a map authentication certificate that Google gave you to this account.

 

Step 2: Create a Google API-based AVD and a Google APIs-based project.

 

This step is critical because the target of AVD running in our previous project is anrdoid 2.2, and the target of the AVD we created now is Google APIs.

This is the AVD list after creation,

When creating an android project, you must select the corresponding Google APIS project.

 

I'm sure you will ask why this is the case or what is the difference with Android 2.2. here we need to explain that the Google API type will contain maps. jar is a jar package related to Google Maps. This function is supported in the project or in AVD. Otherwise, an error is returned.

 

[Problem 1] I tried it myself and found maps. jar in my local installation directory: D: \ Android-SDK-Windows \ add-ons \ addon_google_apis_google_inc_8 \ libs.

Then, the Android 2.2 project type has been added for development, installation, and running. An error will be reported when it is installed in AVD.

[2011-11-16 09:57:12 - GoogleMaps] Installing GoogleMaps.apk...[2011-11-16 09:57:14 - GoogleMaps] Installation error: INSTALL_FAILED_MISSING_SHARED_LIBRARY[2011-11-16 09:57:14 - GoogleMaps] Please check logcat output for more details.[2011-11-16 09:57:14 - GoogleMaps] Launch canceled!

 

Step 3: Use the Google map API to complete instance Development

 

[Essential Learning] before completing an instance, we need to understand several key classes in the com. Google. Android. Maps package. Other functions:

Mapactivity and mapview: to display GoogleMap and perform related operations, these two classes must be used. mapview is created from the view of the displayed map and derived from the viewgroup. Mapactivity must be used with mapactivity and can only be created by mapactivity. This is because mapview needs to connect to the network or file system through background threads, while mapactivity is an abstract class that provides connections to the underlying network, any activity of mapview to be displayed must be derived from mapactivity.

Mapcontroller: used to control the movement and scaling of maps.

Overlay: plotting objects that can be displayed on a map.

Geopoint: an object that contains the longitude and latitude positions

 

Instance analysis:

To use the Google map API, you must configure the Map Library and related permissions in the androidmanifest. xml file:

<? XML version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android" package = "com. jercy. Android. GoogleMap" Android: versioncode = "1" Android: versionname = "1.0"> <! -- Use permission, which is indispensable --> <uses-Permission Android: Name = "android. permission. internet "/> <uses-SDK Android: minsdkversion =" 8 "/> <application Android: icon =" @ drawable/ic_launcher "Android: label = "@ string/app_name"> <! -- Use the library description, which is indispensable --> <uses-library Android: Name = "com. google. android. maps "/> <activity Android: Label =" @ string/app_name "Android: Name = ". googlemapactivity "> <intent-filter> <action Android: Name =" android. intent. action. main "/> <category Android: Name =" android. intent. category. launcher "/> </intent-filter> </activity> </Application> </manifest>

The UI configuration corresponding to main. XML is relatively simple,Note that this is the apikey we applied.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent">    <com.google.android.maps.MapView        android:id="@+id/MapView01"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:apiKey="0l3YQSTUctypL1EJVBlhjJi4uol-4zK2ZuJB0wg"    /></RelativeLayout>

 

The most important thing is to implement the googlemapactivity class of mapactivity:

Public class googlemapactivity extends mapactivity {private mapview mmapview; private mapcontroller mmapcontroller; private geopoint mgeopoint; Public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); mmapview = (mapview) findviewbyid (R. id. mapview01); // mmapview. settraffic (true); // set it to traffic mode mmapview. setstreetview (false); // set it to street view mode. This mode is not supported currently. If it is set to true, mmapview will be displayed on the interface. setsatellite (false); // if it is true, it is set to satellite mode mmapcontroller = mmapview. getcontroller (); // get the mapcontroller object (control mapview) mmapview. setenabled (true); // set it to the activated mmapview. setclickable (true); mmapview. setbuiltinzoomcontrols (true); // set the map to support scaling. // set the starting point to Chengdu mgeopoint = new geopoint (INT) (30.659259*1000000), (INT) (104.065762*1000000); mmapcontroller. animateto (mgeopoint); // locate Chengdu mmapcontroller. setzoom (12); // you must add overlay between 1 and 21 to display the annotation information mylocationoverlay = new mylocationoverlay (); List <overlay> List = mmapview. getoverlays (); list. add (mylocationoverlay) ;}@ overrideprotected Boolean isroutedisplayed () {// todo auto-generated method stubreturn false;} class mylocationoverlay extends overlay {@ overridepublic Boolean draw (canvas, mapview, boolean shadow, long when) {super. draw (canvas, mapview, shadow); paint = new paint (); point myscreencoords = new point (); // converts longitude and latitude to the actual screen coordinate mapview. getprojection (). topixels (mgeopoint, myscreencoords); paint. setstrokewidth (1); paint. setargb (255,255, 0, 0); paint. setstyle (paint. style. stroke); bitmap BMP = bitmapfactory. decoderesource (getresources (), R. drawable. home); canvas. drawbitmap (BMP, myscreencoords. x, myscreencoords. y, paint); canvas. drawtext ("Tianfu Square", myscreencoords. x, myscreencoords. y, paint); Return true ;}}}

Instance effect:

[Problem 2]

Set mmapview. setstreetview (true); If the street view mode is set to true, a cross-box will appear in the map. Later, I asked my friends online, but the street view mode in China is not supported.

The above is the learning of map.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.