Android Study Notes (): using Google Map API v2

Source: Internet
Author: User
Tags certificate fingerprint asymmetric encryption

This document describes how to use the Google Map service. Main reference source: https://developers.google.com/maps/documentation/android/start? Hl = zh-CN. It still takes a long time to connect the simulator and the actual mobile phone. Latest Google
Maps Android API v2 has integrated the map service into Google Play services. However, Chinese mobile phones do not provide Google Play store or Google Play services. Does domestic mobile phones require CTS testing? In other words, this study has no practical significance for providing applications in the Chinese market, because you cannot require additional installation, but will learn how to use Internet services for mobile apps. There are many steps to run the Google map service on your mobile phone.

I. Prepare the development environment

1. Install Google Play Services SDK on Eclipse. Located in extra, see. After installation, go to SDK/extras/Google/google_play_services/


2. After installing the SDK, copy the Lib project of make a copy of the Google Play services to our workspace to introduce the Lib in our project. In ecliseFile-> Import, SelectAndroid-> Existing Android Code into Workspace,
Under the SDK installation directory, select the Lib (google-play-services_lib) and import it.


3. To make our project use Google Play Service Lib, it is a common operation to add the library project to the android project, to use the shared code from the Lib project in the developed application. In eclipse package browsing (usually on the left), select our project and right-click-> Properties->SelectAndroid, SelectLibrary
->
InProject SelectionClickAdd->Select a target from the optional lib project. For example


2. Prepare the commissioning Environment

Google Play storegion and com.google.android.gms.apk (Google Play services). However, these two packages are not included in the simulator, and domestic mobile phones do not contain two APK packages. That is why I sigh at the beginning. To install these two files, you need to search them online. Zookeeper com.google.andriod.gms.apk was updated on April 9, May 30, 2013. Because it is related to services provided by Google online, this package must be up-to-date to match existing online services. Check in the Code:

Googleplayservicesutil.Isgoogleplayservicesavailable(GetApplicationContext ())


If ConnectionResult: SUCCESS is returned, it can be used normally. Other possible responses include SERVICE_MISSING, SERVICE_VERSION_UPDATE_REQUIRED, SERVICE_DISABLED, and SERVICE_INVALID. The following figure shows SERVICE_MISSING in the left and SERVICE_VERSION_UPDATE_REQUIRED in the right. The Map service cannot be used normally. Although there are two buttons above, they do not actually work. It is estimated that they cannot be connected to the server. It is said that Xiaomi's app store can jump to Google
Play, maybe Xiaomi and Google have reached some agreements, maybe there is no problem on Xiaomi's mobile phone.


It is the most troublesome to install this apk. The final solution is to first install Huawei's Zhihui cloud on the simulator and find the two applications above. Because the application source is not formal, it is not recommended to install it on a real mobile phone. Installing apk on the simulator is not as complicated as it is on the Internet. You need to enter adb shell for processing. Open the relevant web page in the browser of the simulator, download the apk, and click to install it. It is no different from installing it on your mobile phone. After the installation is successful, you can see that Google Settings and Play Store are added to the App.

There is also a simpler method. Log on to your gmail account and find the application to download. Google Map is a secure official version.


3. Get the key of the Google map API

To access Google's online Map service through the Google Map API, Google Map API Key is required. This Key is free of charge. Developers only need to have a Gmail mailbox. All Android apps must be issued by digital certificates, and developers must hold private keys. Because the digital certificate is unique, as the unique identifier of the application, this is the App Store that can track your application and track your application using the Google Map service.

The Maps API key is an application-based digital certificate.SHA-1 fingerprint. It can be divided into debug certificates and release certificates. For our experiments, we use debug. The debug keystore file is located in ~ /. Android/, if Windows is located in C: \ Users \ your_user_name \. android \. Linux and use the following command to obtain the certificate. In Windows, the-keystore parameter is "C: \ Users \ your_user_name \. android \ debug. keystore ".

Keytool-list-v-keystore ~ /. Android/debug. keystore-alias androiddebugkey-storepass Android-keypass android

Keytool is a Java data certificate management tool. Keytool stores keys and certificates in a file called keystore in the keystore, which contains two types of data: key entity-secret key, private Key, and paired Public key (asymmetric encryption) trusted certificate entity (trusted certificate entries)-contains only the public Key. -list: displays the certificate information in the keystore. keytool-list-v-keystore specifies the keystore-storepass password-alias generated alias. Each keystore is associated with this unique alias, this alias is usually case insensitive-keystore specifies the keystore-file specifies the certificate location to be exported and the Certificate Name-storepass password-keypass specifies the password of the keystore (the password required for obtaining keystore Information)


Record the certificate fingerprint and use it to obtain the Map Key on the Google APIs Console. Google API Console needs to be logged on with a Google account, that is, our Gmail account. This website does not seem to be very stable. Sometimes the request needs to be sent multiple times to be successful and may be intercepted intermittently, please be patient.




OK. The Google Map API Key is obtained successfully.


4. Use the Google map service

After the above steps, the entire development environment is finally ready to enter the code writing stage. Before writing coding, you must first set the relevant license in AndroidManifest. xml as follows:

<? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android" package = "com. wei. android. beginning" android: versionCode = "1" android: versionName = "1.0">
<Uses-sdk android: minSdkVersion = "14" android: targetSdkVersion = "17"/>
<! --STEP (1)Set Google map API V2 key, where com. Wei. Android. Beginning is the package name -->
<Permission Android: Name = "com. Wei. Android. Beginning. Permission. maps_receive" Android: protectionlevel = "signature"/>
<Uses-Permission Android: Name = "com. Wei. Android. Beginning. Permission. maps_receive"/>

<! --Step 3)Set the license required to use the Google map service -->
<Uses-permission android: name = "android. permission. INTERNET"/>
<! -- Google Map is an Internet service that allows the internet -->
<Uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE"/>
<! -- Allow the API to check the network connection status and determine whether data can be downloaded. -->
<Uses-permission android: name ="Android. Permission. write_external_storage"/><! -- Allow Google map data to be cached on the SD card -->
<Uses-permission android: name ="Com. Google. Android. providers. GSF. Permission. read_gservices"/><! -- Allow APIs to access Google's web-based services -->
<Uses-permission android: name = "android. permission. ACCESS_COARSE_LOCATION"/>
<! -- Map applications are often related to positioning and allow rough positioning -->
<Uses-permission android: name = "android. permission. ACCESS_FINE_LOCATION"/> <! -- Allow precise positioning -->
<! --Step (4)Google map V2 requires OpenGL ES, which must be declared in uses-feature -->
<Uses-feature Android: glsversion = "0x00020000" Android: required = "true"/>

<Application...>
<! --Step 2)Set Google map API V2 key -->
<Meta-data Android: Name = "com. Google. Android. Maps. v2.api _ key" Android: value = "aizasydpovidlcwanpb3d3rwdqyxfn8jikgxey0"/>
<Activity...>... </activity>
</Application>
</Manifest>

The simplest example is displayed in Activitiy. The layout. xml file referenced in Activitiy is as follows:


<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.MapFragment"/>

You only need to introduce the layout in the activity.


public class MainActivity extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

I spent a lot of effort to show the results. The area with 0 longitude and 0 dimension is displayed. See Africa ......


Since some netizens asked where to install Google Services, there are several more images. 1 On the left: The application running status of Google Services is not installed; 2 on the left: locate the required information through the Application mall described above, see the bottom two. Reproduce Africa.


Related links:
My Android development articles

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.