Android Technology Summary (016) -- use Baidu map api to get the current location

Source: Internet
Author: User

Previous: http://www.bkjia.com/kf/201208/151974.html
[Java]
Package location. demo;
 
Import com. baidu. location. BDLocation;
Import com. baidu. location. BDLocationListener;
Import com. baidu. location. LocationClient;
Import com. baidu. location. LocationClientOption;
 
Import android. app. Activity;
Import android. OS. Bundle;
Import android. util. Log;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. TextView;
 
Public class main extends Activity {

Private LocationClient mLocationClient = null;
Private Button mStartBtn = null;
Private TextView mTextView = null;

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );

SetContentView (R. layout. main );

MStartBtn = (Button) findViewById (R. id. button1 );
MTextView = (TextView) findViewById (R. id. textview1 );

MLocationClient = new LocationClient (this );
LocationClientOption option = new LocationClientOption ();
Option. setOpenGps (true); // enable gps
Option. setCoorType ("bd09ll"); // set the coordinate type to bd09ll.
Option. setPriority (LocationClientOption. NetWorkFirst); // sets the network priority.
Option. setProdName ("locSDKDemo2"); // set the product line name
Option. setScanSpan (5000); // timed location, which is located every 5 seconds.
MLocationClient. setLocOption (option );
MLocationClient. registerLocationListener (new BDLocationListener (){
@ Override
Public void onReceiveLocation (BDLocation location ){
If (location = null)
Return;
StringBuffer sb = new StringBuffer (256 );
Sb. append ("time :");
Sb. append (location. getTime ());
Sb. append ("\ nerror code :");
Sb. append (location. getLocType ());
Sb. append ("\ nlatitude :");
Sb. append (location. getLatitude ());
Sb. append ("\ nlontitude :");
Sb. append (location. getlongpolling ());
Sb. append ("\ nradius :");
Sb. append (location. getRadius ());
If (location. getLocType () = BDLocation. TypeGpsLocation ){
Sb. append ("\ nspeed :");
Sb. append (location. getSpeed ());
Sb. append ("\ nsatellite :");
Sb. append (location. getSatelliteNumber ());
} Else if (location. getLocType () = BDLocation. TypeNetWorkLocation ){
Sb. append ("\ naddr :");
Sb. append (location. getAddrStr ());
}
Sb. append ("\ nsdk version :");
Sb. append (mLocationClient. getVersion ());
MTextView. setText (sb. toString ());
}

Public void onReceivePoi (BDLocation location ){
// Return;
}
});


MStartBtn. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
If (mLocationClient = null) return;
If (mLocationClient. isStarted ()){
MLocationClient. stop ();
MStartBtn. setText ("START ");
} Else {
MLocationClient. start ();
MStartBtn. setText ("close ");
}
}
});

}

@ Override
Public void onDestroy (){
If (mLocationClient! = Null & mLocationClient. isStarted ()){
MLocationClient. stop ();
MLocationClient = null;
}
Super. onDestroy ();
}




}

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.