Source code introduction Implementation of three positioning methods for Android: GPS, Baidu Positioning, and Amap Positioning. Source code running screenshot Code snippet: - public class MainActivity extends Activity implements OnClickListener{
-
- private TextView mTextView;
- private Button gpsBtn, baiduBtn, amapBtn;
-
-
- private LocationManager gpsManager;
-
- private LocationClient baduduManager;
-
- private LocationManagerProxy aMapManager;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super .onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mTextView = (TextView) findViewById(R.id.text);
- gpsBtn = (Button) findViewById(R.id.gps);
- baiduBtn = (Button) findViewById(R.id.baidu);
- amapBtn = (Button) findViewById(R.id.amap);
-
- gpsBtn.setOnClickListener( this );
- baiduBtn.setOnClickListener( this );
- amapBtn.setOnClickListener( this );
- }
-
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.gps:
- if (gpsBtn.getText().toString().equals( "Open GPS positioning" )) {
- startGps();
- gpsBtn.setText( "Stop GPS positioning" );
- } else {
- stopGps();
- gpsBtn.setText( "Turn on GPS positioning" );
- }
- break ;
- case R.id.baidu:
- if (baiduBtn.getText().toString().equals( "Open Baidu Positioning" )) {
- startBaidu();
- baiduBtn.setText( "Stop Baidu positioning" );
- } else {
- stopBaidu();
- baiduBtn.setText( "Open Baidu Positioning" );
- }
- break ;
- case R.id.amap:
- if (amapBtn.getText().toString().equals( "Open Amap positioning" )) {
- startAmap();
- amapBtn.setText( "Stop Amap positioning" );
- } else {
- stopAmap();
- amapBtn.setText( "Open Amap positioning" );
- }
- break ;
-
- default :
- break ;
- }
- }
-
- private void startAmap() {
- aMapManager = LocationManagerProxy.getInstance( this );
-
-
-
-
-
- aMapManager.requestLocationUpdates(LocationProviderProxy.AMapNetwork, 2000 , 10 , mAMapLocationListener);
- }
-
- private void stopAmap() {
- if (aMapManager != null ) {
- aMapManager.removeUpdates(mAMapLocationListener);
- aMapManager.destroy();
- }
- aMapManager = null ;
- }
-
- private void startBaidu() {
- if (baduduManager == null ) {
- baduduManager = new LocationClient( this );
-
- LocationClientOption option = new LocationClientOption();
-
- option.setLocationMode(LocationMode.Hight_Accuracy);
-
- option.setCoorType( "gcj02" );
-
- option.setScanSpan( 1000 );
-
- option.setIsNeedAddress( true );
- baduduManager.setLocOption(option);
-
- baduduManager.registerLocationListener(mBdLocationListener);
- }
- baduduManager.start();
- }
-
- private void stopBaidu() {
- baduduManager.stop();
- }
-
-
- private void startGps() {
-
- gpsManager = (LocationManager) getSystemService(LOCATION_SERVICE);
-
-
- String provider = gpsManager.getProvider(LocationManager.GPS_PROVIDER).getName();
-
-
- gpsManager.requestLocationUpdates(provider, 3000 , 10 , gpsListener);
- }
-
- private void stopGps() {
- gpsManager.removeUpdates(gpsListener);
- }
-
-
- private LocationListener gpsListener = new LocationListener() {
-
-
- @Override
- public void onLocationChanged(Location location) {
- Log.e( "Location" , "onLocationChanged" );
- double latitude = location.getLatitude();
- double longitude = location.getLongitude();
- float speed = location.getSpeed();
- long time = location.getTime();
- String s = "latitude--->" + latitude
- + " longitude--->" + longitude
- + " speed--->" + speed
- + " time--->" + new Date(time).toLocaleString();
- mTextView.setText( "GPS location\n" + s);
- }
-
-
- @Override
- public void onProviderDisabled(String provider) {
- Log.e( "Location" , "onProviderDisabled" );
- }
-
-
- @Override
- public void onProviderEnabled(String provider) {
- Log.e( "Location" , "onProviderEnabled" );
- }
-
-
- @Override
- public void onStatusChanged(String provider, int status, Bundle extras) {
- Log.e( "Location" , "onStatusChanged" );
- }
- };
-
- private BDLocationListener mBdLocationListener = new BDLocationListener() {
-
- @Override
- public void onReceiveLocation(BDLocation location) {
-
- 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.getLongitude());
- 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());
- sb.append( "\ndirection : " );
- sb.append( "\naddr : " );
- sb.append(location.getAddrStr());
- sb.append(location.getDirection());
- } else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
- sb.append( "\naddr : " );
- sb.append(location.getAddrStr());
- sb.append( "\noperationers : " );
- sb.append(location.getOperators());
- }
- mTextView.setText( "Baidu Positioning\n" + sb.toString());
- }
- };
-
- private AMapLocationListener mAMapLocationListener = new AMapLocationListener() {
-
- @Override
- public void onStatusChanged(String provider, int status, Bundle extras) {
-
- }
-
- @Override
- public void onProviderEnabled(String provider) {
-
- }
-
- @Override
- public void onProviderDisabled(String provider) {
-
- }
-
- @Override
- public void onLocationChanged(Location location) {
-
- }
-
- @Override
- public void onLocationChanged(AMapLocation location) {
- if (location != null ) {
- Double geoLat = location.getLatitude();
- Double geoLng = location.getLongitude();
- String cityCode = "" ;
- String desc = "" ;
- Bundle locBundle = location.getExtras();
- if (locBundle != null ) {
- cityCode = locBundle.getString( "citycode" );
- desc = locBundle.getString( "desc" );
- }
- String str = ( "Positioning successful: (" + geoLng + "," + geoLat + ")"
- + "\nAccuracy:" + location.getAccuracy() + "meters"
- + "\nLocation method:" + location.getProvider() + "\nLocation time:"
- + new Date(location.getTime()).toLocaleString() + "\nCity code:"
- + cityCode + "\nLocation Description:" + desc + "\nProvince:"
- + location.getProvince() + "\nCity:" + location.getCity()
- + "\nDistrict (county):" + location.getDistrict() + "\nRegion code:" + location
- .getAdCode());
- mTextView.setText( "Gaode positioning\n" + str);
- }
- }
- };
-
- }
Source code link: http://download..com/data/1968757
|