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 ();
}
}