Android Open Source - android-component-location My Location Manager






From Project

Back to project page android-component-location.

License

The source code is released under:

MIT License

If you think the Android project android-component-location listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.yingchn.android.location;
//from  w  w w. j  av a  2  s  .  c o m
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;
import com.yingchn.android.AppContext;

public class MyLocationManager implements AMapLocationListener,
    BDLocationListener {
  public static final String BIF_GOT_LOCATION          = "got_location";
  public static final String INTENT_PARAMS_LATITUDE    = "loc_latitude";
  public static final String INTENT_PARAMS_LONGITUDE   = "loc_longitude";
  public static final String INTENT_PARAMS_LOC_TYPE    = "loc_type";
  public static final String INTENT_PARAMS_LOC_FROM    = "loc_from";
  public static final String INTENT_PARAMS_LOC_LOG     = "loc_log";

  enum LocFrom {
    BAIDU, AMAP, GOOGLE, NONE
  }
  
  enum LocType {
    gcj02, bd09, bd09ll
  }
  
  private static MyLocationManager sInstance = new MyLocationManager();
  private LocationManagerProxy mAMapLocationManager;
  private LocationClient mLocationClient = null;

  private BroadcastReceiver mLocationReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

    }
  };

  private MyLocationManager() {
    // register location broadcast receiver
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(BIF_GOT_LOCATION);
    
    Log.i("yingchn", "context : " + AppContext.getContext());
    Log.i("yingchn", "context : " + AppContext.getContext().getApplicationContext());
    
    LocalBroadcastManager.getInstance(AppContext.getContext()).registerReceiver(mLocationReceiver, intentFilter);
  }
  
  public static MyLocationManager getInstance() {
    if(sInstance == null) {
      sInstance = new MyLocationManager();
    }
    return sInstance;
  }
  
  public void initBaiduLocation() {
    mLocationClient = new LocationClient(AppContext.getContext());
    LocationClientOption option = new LocationClientOption();
    option.setLocationMode(LocationMode.Hight_Accuracy);// ????????
    option.setCoorType("gcj02");// ???????????????????gcj02
    // option.setCoorType("bd09ll");//???????????????????gcj02
    option.setIsNeedAddress(true);// ????????????????????
    option.setNeedDeviceDirect(true);// ?????????????????????
    mLocationClient.setLocOption(option);
    mLocationClient.start();
    mLocationClient.registerLocationListener(this);
    if (mLocationClient != null && mLocationClient.isStarted()) {
      mLocationClient.requestLocation();
      mLocationClient.requestPoi();
    } else {
      Log.i("yingchn", "locClient is null or not started");
    }
  }
  
  public void initAmapLocation() {
    mAMapLocationManager = LocationManagerProxy.getInstance(AppContext.getContext());
    mAMapLocationManager.requestLocationUpdates(LocationProviderProxy.AMapNetwork, 5000, 10, this);
  }
  
  public void init() {
    // init AMAP location manager
    initAmapLocation();

    // init Baidu location manager
    initBaiduLocation();
  }
  
  public void destory() {
    LocalBroadcastManager.getInstance(AppContext.getContext()).unregisterReceiver(mLocationReceiver);
  }

  @Override
  public void onLocationChanged(Location location) {

  }

  @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(AMapLocation location) {
    Intent intent = new Intent(BIF_GOT_LOCATION);
    intent.putExtra(INTENT_PARAMS_LOC_LOG, location.toString());
    LocalBroadcastManager.getInstance(AppContext.getContext()).sendBroadcast(intent);
  }

  @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.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());
    } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
      sb.append("\naddr : ");
      sb.append(location.getAddrStr());
    }

    Intent intent = new Intent(BIF_GOT_LOCATION);
    intent.putExtra(INTENT_PARAMS_LOC_LOG, sb.toString());
    LocalBroadcastManager.getInstance(AppContext.getContext()).sendBroadcast(intent);
    Log.i("yingchn", "baidu location : " + sb.toString());
  }

  @Override
  public void onReceivePoi(BDLocation poiLocation) {
    // ?????????poi??
    if (poiLocation == null) {
      return;
    }
    StringBuffer sb = new StringBuffer(256);
    sb.append("Poi time : ");
    sb.append(poiLocation.getTime());
    sb.append("\nerror code : ");
    sb.append(poiLocation.getLocType());
    sb.append("\nlatitude : ");
    sb.append(poiLocation.getLatitude());
    sb.append("\nlontitude : ");
    sb.append(poiLocation.getLongitude());
    sb.append("\nradius : ");
    sb.append(poiLocation.getRadius());
    if (poiLocation.getLocType() == BDLocation.TypeNetWorkLocation) {
      sb.append("\naddr : ");
      sb.append(poiLocation.getAddrStr());
    }
    if (poiLocation.hasPoi()) {
      sb.append("\nPoi:");
      sb.append(poiLocation.getPoi());
    } else {
      sb.append("noPoi information");
    }
    Log.i("yingchn", "baidu location : " + sb.toString());
  }
}




Java Source Code List

android.support.v4.preference.PreferenceFragment.java
android.support.v4.preference.PreferenceManagerCompat.java
com.sothree.slidinguppanel.SlidingUpPanelLayout.java
com.yingchn.android.AppContext.java
com.yingchn.android.MyApplication.java
com.yingchn.android.activity.ArbitraryFragmentActivity.java
com.yingchn.android.activity.MainActivity.java
com.yingchn.android.activity.WLocation.java
com.yingchn.android.fragment.LogFragment.java
com.yingchn.android.fragment.SettingFragment.java
com.yingchn.android.location.MyLocationManager.java
com.yingchn.android.util.FragmentUtil.java