Baidu Map Learning (II) positioning of the-android end

Source: Internet
Author: User

Ah, experienced a small series of recent time research, my Baidu positioning finally succeeded, suddenly feel oneself Meng Meng da (~ ̄▽ ̄) ~

Words not much to say, directly into the topic:

First, let's look at the effect:

"Analytical positioning principle"

"Code Analysis"

1) The handler's manifest file:url (http://developer.baidu.com/map/index.php?title=android-locsdk/guide/v5-0 );

2) Add layout information: There is only one button and one text message

3) write Java code:

public class Mainactivity extends Activity {private Button btn_lbs=null;    Private TextView Show_lbs=null;    Private Locationclient Locationclient=null;  private static final int update_time=5000;    Update frequency private static int location_counts=0;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Btn_lbs= (Button) Findviewbyid (r.id.btn_lbs);                Show_lbs= (TextView) Findviewbyid (r.id.show_lbs);         Locationclient =new locationclient (this);        Set positioning conditions locationclientoption option = new Locationclientoption ();        Option.setlocationmode (locationmode.hight_accuracy);//Set high precision positioning mode Option.setcoortype ("Bd09ll");//set Baidu latitude and longitude coordinate system format        Option.setscanspan (update_time);//Set the time interval for initiating a location request to 1000ms option.setisneedaddress (true);//decompile to obtain a specific location, only network positioning can be  locationclient.setlocoption (option);    Option initialization information added to the client//registered location for listener events    Locationclient.registerlocationlistener (New Bdlocationlistener () {@Override public                    void Onreceivelocation (bdlocation location) {//Position callback information if (Location==null) {                Return                } stringbuffer sb=new StringBuffer ();                Sb.append ("Time:");                  Sb.append (Location.gettime ());                Sb.append ("\nerror Code:");  Sb.append (Location.getloctype ());                Gets the location type Sb.append ("\nlatitude:");  Sb.append (Location.getlatitude ());                Gets the latitude coordinate sb.append ("\nlontitude:"); Sb.append (Location.getlongitude ());                Gets the precision coordinate sb.append ("\nradius:"); Sb.append (Location.getradius ()); Gets the positional precision if (location.getloctype () = = bdlocation.typegpslocation) {sb.append ("\nspeed:                    ");  Sb.append (Location.getspeed ()); GetSpeed, only GPS positioning results when there is speed information sb.append ("\nsatellite:");                Sb.append (Location.getsatellitenumber ());                    } else if (location.getloctype () = = bdlocation.typenetworklocation) {sb.append ("\naddress:");  Sb.append (Location.getaddrstr ());                Get detailed address information} location_counts + +;                Sb.append ("\ n Check location update number:");                Sb.append (string.valueof (location_counts));            Show_lbs.settext (Sb.tostring ());        }        }); Btn_lbs.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View ar                G0) {if (locationclient==null) {return;                    } if (locationclient.isstarted ()) {Btn_lbs.settext ("open positioning");                Locationclient.stop ();                    }else{Btn_lbs.settext ("close positioning"); Locationclient.sTart ();                     /* * When the integer value set is greater than or equal to (MS), the positioning SDK uses the timed positioning mode internally.                     * Once the requestlocation () is called, the Location SDK will be positioned once every set time.                     * If the location of the SDK based on location based on the discovery has not changed, will not initiate network requests, * Return the results of the last location, if the location changes, the network request to locate, to obtain new positioning results.                     * When the timing is fixed, the requestlocation is called once, and the locating result is heard regularly.                */locationclient.requestlocation ();    }                            }        });        } @Override protected void OnDestroy () {Super.ondestroy ();            if (locationclient!=null&&locationclient.isstarted ()) {locationclient.stop ();        Locationclient=null; }    }}

Summary

Because the small series is clumsy, so study for a long time to solve, the above analysis belongs to their own understanding

locationclient belongs to the client, the client by setting the location of the listener event to achieve the location of the acquisition, should not note that when the program ends, you locate the client:locationclient Should be in the off state

Appendix

1.Android SDK official website :http://developer.baidu.com/map/index.php?title=android-locsdk

2. Baidu Map Help document (V5.0):http://developer.baidu.com/map/loc_refer/index.html

3.Android Baidu positioning to get the current location :http://www.2cto.com/kf/201501/373620.html

4. Baidu Android positioning SDK to achieve the current latitude and longitude and location:http://www.open-open.com/lib/view/ Open1346982366162.html

5. Android location SDK FAQ:http://developer.baidu.com/map/index.php?title= Android-locsdk/qa

Baidu Map Learning (II) positioning of the-android end

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.