Android--google Map API V2 use

Source: Internet
Author: User

First, get the API Key

1. Get SHA-1 fingerprint first

There are two types of digital certificates, one is debug and the other is release. The former can only be used for testing, and the latter may be used for actual products.

Debug: Enter the command at the command line: keytool-list-v-keystore "C:\Users\your_user_name\.android\debug.keystore"-alias Androiddebugkey-storepass Android-keypass Android

Release: Replace the signature file path, and replace the password

SHA-1 fingerprint can also be obtained in Eclipse,

2. Login website Get API Key

URL:https://code.google.com/apis/console/ login with Gmail's account, if this is the first time, you need to create a project, which by default creates a project called API project.

Click Services on the left, you will see a lot of APIs and services in the middle, find Google Maps Android API v2, then set it to on, you need to accept some terms of service.

In the left navigation bar, select API Access. On the Out page, select Create New Android Key... You can generate the key:

3. Create a project

Declaring permissions in Androidmanifest.xml

<uses-permissionAndroid:name= "Android.permission.INTERNET"/><uses-permissionAndroid:name= "Android.permission.ACCESS_NETWORK_STATE"/><uses-permissionAndroid:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permissionAndroid:name= "Com.google.android.providers.gsf.permission.READ_GSERVICES"/><!--The following permissions is not required to use Google Maps for Android API v2, but is recommended. -<uses-permissionAndroid:name= "Android.permission.ACCESS_COARSE_LOCATION"/><uses-permissionAndroid:name= "Android.permission.ACCESS_FINE_LOCATION"/>

Declaring API Key

 <!--  Google Map Key  -->  <  android:name   = "Com.google.android.maps.v2.API_KEY"   Android:value   />  <  meta-data  android:name< /span>= "Com.google.android.gms.version"   Android:value  = "4030500"  

Since Google Maps v2 requires support for OpenGL ES v2, the following elements must be added to the Androidmanifest.xml

< uses-feature         android:glesversion = "0x00020000"         android:required= "true"/>

Import Google-play-services.jar

Load map to use Supportmapfragment

Supportmapfragment fragment = supportmapfragment.newinstance (); Getsupportfragmentmanager (). BeginTransaction ( ). Add (R.id.ll_map, Fragment). commit ();

4. Add Marker marker

The default marker

Private= ((mapfragment) Getfragmentmanager (). Findfragmentbyid (R.id.map)). Getmap (); Mmap.addmarker (new  markeroptions ()        . Position (new latlng (0, 0))        . Title ("Hello World");

Custom Marker

 private  static  Span style= "Color:rgb (0, 0, 255); >final  latlng MELBOURNE = new  latlng ( -37.81319, 144.96298private  Marker Melbourne = mmap.addmarker (new                              Markeroptions (). Position (MELBOURNE)  . Title ( "Melbourne" " Population: 4,137,400 "". Icon (Bitmapdescriptorfactory.fromresource (r.dr Awable.arrow)));  

Custom marker when using Bitmapdescriptorfactory to create a custom marker icon, before using Bitmapdescriptorfactory, take a look at its documentation before using any method of this class. You must do one of the following to ensure that the class is initialized: 1: Add a googlemap from Mapfragment or Mapview, and you can determine whether the return is empty by calling the Googlemap.getmap () method. 2: Call the Mapsinitializer.initialize (Context) method, as long as it does not throw out Com.google.android.gms.common.GooglePlayServicesNotAvailableException Googleplayservicesnotavailableexception This exception can be started normally.

There are also marker click events, Googlemap.setonmarkerclicklistener (); Marker drag event, Googlemap.setonmarkerdraglistener (), (marker cannot be dragged by default, set Melbourne.setdraggable (true) as draggable when creating marker)

      Click Marker will default pop-up Information window info windows, you can use the Infowindowadapter interface, Call Googlemap.setinfowindowadapter () to customize the info Windows,infowindowadapter to implement the two methods  getinfowindow (Marker) and Getinfocontents (Marker). The API calls the Getinfowindow (Marker) method first, if NULL is returned, and then continues to call the Getinfocontents (Marker) method, and if you continue to return NULL,API the default info windows will be called. Info Windows has click event Googlemap.setoninfowindowclicklistener () just like marker.

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.