Android development fragment embedded in the map of the German

Source: Internet
Author: User

Recently in doing projects to use the map, looked at the map of the gold of the API, and finally decided to use a gold map, and usually different, the map is to embed in the fragment, the study of the online code, and finally realized. Here's how to implement the 2D map.

1. First go to the map website of high Germany to register key, address is http://lbs.amap.com/api/android-sdk/summary/;

2. Download the required SDK according to the instructions.

3. Configuration Engineering

(1) Add key

Add a key to the project's "Androidmanifest.xml" file in the following code.

(1) Add permissions

<pre><code><uses-permission android:name= " Android.permission.INTERNET " /> <uses-permission android:name=" android.permission.WRITE_ External_storage " /> <uses-permission android:name=" Android.permission.ACCESS_COARSE_LOCATION "  /> <uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"  /> < Uses-permission android:name= "Android.permission.ACCESS_FINE_LOCATION"  /> <uses-permission  android:name= "Android.permission.READ_PHONE_STATE"  /> <uses-permission android:name= " Android.permission.CHANGE_WIFI_STATE " /> <uses-permission android:name=" Android.permission.ACCESS_WIFI_STATE " /> <uses-permission android:name=" Android.permission.CHANGE_CONFIGURATION " /> <uses-permission android:name=" Android.permission.WRITE_SETTINGS "&NBSP;/></CODE></PRE> 


4. layout file

<com.amap.api.maps2d.mapview

android:id= "@+id/map"

android:layout_width= "Match_parent"

android: layout_height= "Match_parent"/>

5. To achieve

 public class fragmentmap extends fragment{     private
 static FragmentMap fragment = null;       @ViewInject (R.id.map)      private MapView 
Mapview;
     private AMap aMap;
     private View mapLayout;        public static fragment newinstance ()  {            if  (fragment == null)  {               synchronized  (Fragmentmap.class)  {                if   (fragment == null)  {                    &nbSp; fragment = new fragmentmap ();                        }                          }          return
 fragment; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP}        public view oncreateview ( Layoutinflater inflater, viewgroup container,             bundle savedinstancestate)  {           if  (maplayout == null)  {           
Maplayout = inflater.inflate (R.layout.fragment_map, null);          viewutils.inject (This, maplayoUT);          mapview.oncreate (savedinstancestate);/must write             if  (amap == null)  {   
          amap = mapview.getmap ();               } else {                  if  ( Maplayout.getparent ()  != null)  {                         ((viewgroup)  
Maplayout.getparent ()). Removeview (Maplayout);                    &NBSP}             }               return maplayout; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}         @Override         public void onresume ()  {     
     super.onresume ();
          mapview.onresume ();   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}       /**     *   methods must rewrite the     * map lifecycle method     */    @ Override     public void onpause ()  {      
  super.onpause ();
        mapview.onpause ();   &NBSP;&NBSP;&NBSP;&NBSP}     /**    *  method must override    *  Life cycle method of map   */   @Override   puBlic void onsaveinstancestate (bundle outstate)  {       
Super.onsaveinstancestate (outstate);
       mapview.onsaveinstancestate (outstate); &NBSP;&NBSP}     /**    *  methods must override the    * map lifecycle method    */    @Override    public void ondestroy ()  {  
     super.ondestroy ();
       mapview.ondestroy ();     }   }


The effect chart is as follows:

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.