Android fragment embedded in the German map location

Source: Internet
Author: User
Tags time interval

The


is already written in the previous article embedding a gold map, here is how to achieve positioning, and display the positioning of the icon.

public class fragmentmap extends fragment implements locationsource,  amaplocationlistener{       private static FragmentMap 

fragment = null;         @ViewInject (R.id.map)        

private mapview mapview;

       private AMap aMap;

       private View mapLayout;

       private OnLocationChangedListener mListener;

       private LocationManagerProxy mAMapLocationManager;

         public static fragment newinstance ()  {           if  (fragment == null)  {              synchronized  (fragmentmap.class)  {      

           if  (fragment == null)  {                   

    fragment = new fragmentmap ();

                   }            }        

 }        return fragment;    &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);            if  (amap == null)  {               amap = mapview.getmap (

);               amap.setlocationsource ( This);//  set positioning monitor                Amap.getuisettings (). Setmylocationbuttonenabled (True);//  set the default positioning button to display                amap.setmyloCationenabled (True);//  set to True to display the positioning layer and trigger positioning, false indicates that the hidden anchor layer does not trigger the location, the default is False                //  custom system positioning Blue Dot                MyLocationStyle myLocationStyle = new 

Mylocationstyle ();               //  Custom location Blue dot icon                mylocationstyle.mylocationicon (

Bitmapdescriptorfactory.fromresource (r.mipmap.content_btn_location));              //  will customize the   mylocationstyle  object added to map              

Amap.setmylocationstyle (Mylocationstyle);              //  Construction   Locationmanagerproxy&nbsP; Objects             mAMapLocationManager = 

Locationmanagerproxy.getinstance (Getactivity ());           }            } else {                   if  (Maplayout.getparent ()  != null)  {                       

   ((ViewGroup)  maplayout.getparent ()). Removeview (Maplayout);

                  }            }       return

 mapLayout;     &NBSP}     @Override    pUblic void onattach (activity activity)  {       

Super.onattach (activity);    &NBSP}       @Override    public void oncreate ( bundle savedinstancestate)  {        super.oncreate (

Savedinstancestate);    &NBSP}      @Override   public void onresume ()  { &

Nbsp;    super.onresume ();

      mapview.onresume ();   &NBSP}  /** *  method must override * map Lifecycle method */   @Override   public voi

D onpause ()  {       super.onpause ();

       mapview.onpause ();

       deactivate ();
 &NBSP}  /** *  method must override * map Lifecycle method */  @Override
 public void onsaveinstancestate (bundle outstate)  {     

 super.onsaveinstancestate (outstate);

      mapview.onsaveinstancestate (outstate); &NBSP}  /** *  method must override * map Lifecycle method */  @Override   public void ondestroy ()

 {     super.ondestroy ();

     mapview.ondestroy ();  &NBSP}     @Override  public void onlocationchanged (amaplocation amaplocation )  {      if  (mlistener != null && Amp; amaplocation != null)  {          if   (Amaplocation.getamapexception (). GetErrorCode ()  == 0)  {                mlistener.onlocationchanged (AMapLocation);//  display system small blue dot                  //Get location information              geolat =

 amaplocation.getlatitude ();              geoLng = 

Amaplocation.getlongitude ();              amap.movecamera (

Cameraupdatefactory.zoomto (14));

           &NBSP}       }  }   @Override public void onlocationchanged (location location)  { }   @Override P Ublic void onstatuschanged (String provider, int status, bundle extras)  { }   @Override public void onproviderenabled (string provider)  { } &N

Bsp @Override PUBLIC&NBSp;void onproviderdisabled (String provider)  { }  //activation positioning @Override public void  Activate (Onlocationchangedlistener listener)  {        

mlistener = listener;         if  (mamaplocationmanager == null)  {              mAMapLocationManager = 

Locationmanagerproxy.getinstance (Getactivity ());          //This method initiates a position request for every fixed time, in order to reduce the consumption of electricity or network traffic,           //Note Set the appropriate location time interval and call the Removeupdates () method at the appropriate time to cancel the location request           //calls the Destroy () method at the appropriate lifecycle after the end of the position           //If the interval is-1, the location is fixed once            Mamaplocationmanager.requestlocationdata (LocationpRoviderproxy.amapnetwork, 60 * 1000, 10, this);        &NBSP}  &nbsp  /stop positioning @Override public void 

Deactivate ()  {      mListener = null;       if  (mamaplocationmanager != null)  {   

        mamaplocationmanager.removeupdates (this);

           mamaplocationmanager.destroy ();        }      mamaplocationmanager = null

;    &NBSP}}

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.